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

@IonicPage()
@Component({
  selector: 'page-uploaddoc',
  templateUrl: 'uploaddoc.html',
})
export class UploaddocPage {
  docs:any;
  local:driver
  lang: string = 'en';

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

  ionViewDidEnter(){
    this.langTrans()

    this.storage.get('driver_data').then(data=>{
      this.local = data

      this.myservice.load_post({id:this.local.id},'getAllDocument').subscribe(resp=>{
        if(resp.status == 'success'){
          this.docs = resp.data
        }
        else{
          this.translate.get(['uploaddoc.' + resp.message]).subscribe(value => {
            this.myservice.show_alert('', value['uploaddoc.' + resp.message])
          })
        }
      })
    })
  }

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

  uploadDoc(data){
    this.comService.setdocData(data);
    this.myservice.show_loader()
    this.navCtrl.push("DocupdatePage", { title: data.document_type});
  }

}
