import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ModalController,Events } from 'ionic-angular';
import {Myservice} from '../../providers/myservice';
import { Storage } from '@ionic/storage';

/**
 * Generated class for the NotificationPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-Request_quote',
  templateUrl: 'Request_quote.html',
})
export class RequestQuote {

	user_data:any;
	request_list:any;
	
	constructor(public navCtrl: NavController, public navParams: NavParams,public storage:Storage, private myservice: Myservice,public modalCtrl: ModalController,public events: Events) {
		
		events.subscribe('user:pool_requests', userdetails => {	
						this.get_pool_requests();
		});
		
		setTimeout(() => {
			this.storage.get('user_data').then((user_data) => {
			console.log(user_data);
			if(user_data){
				console.log("in user")
				this.user_data= user_data;
				this.get_pool_requests()
			}else{
		
		
		let modal = this.modalCtrl.create('Loginbook');
		modal.onDidDismiss(data => {
			 console.log(data);
			 if(data){
			if(data.user_data.id) {
				this.user_data = data.user_data;
				this.get_pool_requests()
			}
			  }else{
					  
					 this.navCtrl.setRoot('TabsPage');
				 }
		});
		modal.present();
		}
		})
		}, 1000);
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Request_quote');
	}
	get_details(id){
		this.navCtrl.push('QuoteDetailsPage',{'pool_id' :id});
	}
	  
	get_pool_requests(){
		this.myservice.show_loader();
		this.myservice.load_get('get_pool_requests/id/'+this.user_data.id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.request_list = data.result;
				console.log(this.request_list);
			}
			else
				this.request_list =''
		});
	}	
	
	delete_noti(id,evt){
		evt.stopPropagation();
		this.myservice.show_loader();
		this.myservice.load_get('delete_noti/id/'+id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.myservice.show_alert("Success",data.message);
				this.events.publish('user:notifications', '');
				this.get_pool_requests();
				
			}
		});
	}
	
	notification_clear(evt){
		evt.stopPropagation();
		this.myservice.show_loader();
		this.myservice.load_get('notification_clear/id/'+this.user_data.id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.myservice.show_alert("Success",data.message);
				this.events.publish('user:notifications', '');
				this.get_pool_requests();
				
			}
		});
	}

}
