import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Events } from 'ionic-angular';
import { Storage } from "@ionic/storage";
import { Myservice } from "../../providers/myservice";
import { dataService } from "../../providers/common.service";
import { TranslateService } from "@ngx-translate/core";

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})
export class HomePage {
  tab: any;
  upcoming: any;
  completed: any;
  baseurl = this.myservice.base_url
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, private myservice: Myservice, private data: dataService, public events: Events, private translate: TranslateService) {
    this.events.subscribe('driver:lang',data=>{
      setTimeout(() => {
        this.langTrans();
      }, 500);
    })
  }

  ionViewDidEnter() {

    this.events.publish('driver:locOn', '');
    this.langTrans()

    if (this.navParams.get('id')){
      this.navCtrl.push('QuickridePage', { id: this.navParams.get('id'), from: this.navParams.get('from') })
    }
      else {
      this.myservice.show_loader()
      this.storage.get('driver_data').then(data => {
        if (data) {
          this.myservice.load_post({ id: data.id }, 'getMyRides').subscribe(response => {
            this.myservice.hide_loader()
            if (response.status == 'success') {
              this.upcoming = response.data.upcoming
              this.completed = response.data.completed
            }
            else{
              this.translate.get(['home.' + response.message]).subscribe(value => {
                this.myservice.show_alert('', value['home.' + response.message])
              })
            }
          })
        }
      })
    }

  }

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

  ionViewDidLoad() {
    this.tab = "active";
  }


  tab_swap(type) {
    this.tab = type;
  }

  rideDetails(data) {
    this.data.setrideData(data)
    this.navCtrl.push('RidedetailsPage')
    // this.navCtrl.setRoot('QuickridePage', { id: '38'})
    // this.events.publish('driver:locOn','')
  }
}
