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


@IonicPage()
@Component({
  selector: 'page-booking-detail',
  templateUrl: 'booking-detail.html',
})
export class BookingDetail {

	booking_id:any;
	request_val:any;
 
	
	constructor(public navCtrl: NavController, public navParams: NavParams,public storage:Storage, private myservice: Myservice,public modalCtrl: ModalController,public events:Events) {
		this.booking_id = this.navParams.get('id');
		 
		if(this.booking_id){
			this.getSingleBookings();
			this.update_read_status();
		}
		
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad VehicleDetailPage');
	}

	back(){
		this.navCtrl.pop();
	}

	repair(){
		 
		this.myservice.show_loader();
		this.myservice.load_get('update_book_status/id/'+this.booking_id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.myservice.show_alert("Success",data.message);
				this.getSingleBookings()
				this.update_read_status();
			}
			else
				this.myservice.show_alert("Error",data.message)
		});
		 
	}
	
	getSingleBookings(){
		this.myservice.show_loader();
		this.myservice.load_get('getSingleBookings/id/'+this.booking_id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.request_val = data.data;
				console.log("book details", this.request_val)
			}
		});
	}
	
	update_read_status(){
		this.myservice.show_loader();
		this.myservice.load_get('update_bookread_status/id/'+this.booking_id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
			 
				this.events.publish('user:read_status','');
				console.log(data);
			}
		});
	}
	
	presentPopover(myEvent) {
		let modal = this.modalCtrl.create('AddReview',{'garage_id':this.request_val.garage_id,'booking_id' : this.booking_id});
		modal.onDidDismiss(data => {
			if(data) {
				this.getSingleBookings(); 
			}
		}); 
		modal.present({
			ev: myEvent
		});
	}

}
