import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,MenuController } from 'ionic-angular';
import { Myservice } from "../../providers/myservice";
import { Storage } from "@ionic/storage";
import { user } from "../../models/mymodel";
import { Toast } from "@ionic-native/toast";
import { CallNumber } from '@ionic-native/call-number';
import { TranslateService } from "@ngx-translate/core";

@IonicPage()
@Component({
  selector: 'page-tripdetails',
  templateUrl: 'tripdetails.html',
})
export class TripdetailsPage {
  details = this.navParams.get('data')
  local:user
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, private myservice: Myservice, private storage: Storage, private toast: Toast, private callNumber: CallNumber, private translate: TranslateService, private menu:MenuController) {
  }

  ionViewDidEnter() {
    this.menu.swipeEnable(false)
    this.storage.get('lang').then(lang => {
      if (lang != null) {
        this.translate.use(lang)
        this.lang = lang;
      }
      else {
        this.translate.use('en')
      }
    })

    this.storage.get('user_data').then(data=>{
      if(data){
        this.local = data
      }
    })
    console.log(this.details);
  }

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

  rating(event){
    if (Number(event.srcElement.value)){
      this.myservice.show_loader()
      var data = { 'rating': Number(event.srcElement.value), 'username': this.local.id, 'driver_id': this.details.assigned_for, 'booking_id': this.details.bookingid}
      this.myservice.load_post(data,'setRating').subscribe(response=>{
        this.myservice.hide_loader()
        if(response.status=='success'){
          this.details.rating = Number(event.srcElement.value)

          this.translate.get(['trip.' + response.message]).subscribe(value => {
            this.toast.show(value['mytrips.' + response.message], '2000', 'center').subscribe(
              toast => {
                console.log(toast);
              });
            })
        }
        else{
          this.translate.get(['trip.' + response.message]).subscribe(value => {
            this.myservice.show_alert('', value['trip.' + response.message])
          })
        }
      })
    }
  }

  call(){
    this.callNumber.callNumber(this.details.phone, true)
      .then(res => console.log('Launched dialer!', res))
      .catch(err => console.log('Error launching dialer', err));
  }

  cancel(){
    this.myservice.show_loader()
    this.myservice.load_post({id:this.details.bookingid},'cancelRide').subscribe(response=>{
      this.myservice.hide_loader()
      if(response.status == 'success'){
        this.details.status = 'Cancelled'
        this.translate.get(['trip.' + response.message]).subscribe(value => {
          this.toast.show(value['mytrips.' + response.message], '2000', 'center').subscribe(
            toast => {
              console.log(toast);
            });
        })
      }
      else{
        this.translate.get(['trip.' + response.message]).subscribe(value => {
          this.myservice.show_alert('', value['trip.' + response.message])
        })
      }
    })
  }

  ionViewWillLeave() {
    this.menu.swipeEnable(true)
  }
}
