import { Component, ViewChild, ElementRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CommonProvider } from '../../providers/common/common';
import { Storage } from '@ionic/storage';

import {TranslateService} from '@ngx-translate/core';
import { SubjectService } from '../../providers/subject.service';
import { WebService_lang } from '../../providers/web.service';
import * as $ from 'jquery';
declare var google;


@IonicPage()
@Component({
  selector: 'page-mybooking',
  templateUrl: 'mybooking.html',
})
export class MybookingPage {
  @ViewChild('language') language: ElementRef;
	lang =  'en';
  shownGroup = null;
  count:any;
  booking:any;
  result:any;

  constructor(
    public translate: TranslateService,
    public subjectService: SubjectService,
    public service_lang: WebService_lang,
    public navCtrl: NavController,
     public navParams: NavParams,
      public service: CommonProvider,
      public storage: Storage) {
    this.count = 0;
    this.shownGroup = 0;
    this.booking = "upcoming";
  	console.log(this.shownGroup);
    this.storage.get('token').then(data => {
      console.log(data);
        if(data){
          var post_data = {'token':data};
          var This = this;
          this.service.show_loader(); 
          var timer = setInterval(function(){
            console.log(post_data);
            if(data!=''){
              clearInterval(timer)
              This.service.post_data('userbookings',post_data).subscribe(data=>{
                This.result = data;
                This.service.hide_loader();
                console.log(This.result);                         
              })
            } 
          },100);
        }    
    })

    this.subjectService.getLangData().subscribe(langData => {
      console.log(langData);
      this.lang = this.service_lang.getLocalStorageItem('lang');
      console.log(this.lang);
      if (this.lang) {
        translate.setDefaultLang(this.lang);
        translate.use(this.lang);
      }
    });

    
    const lang  = this.service_lang.getLocalStorageItem('lang');
    console.log(lang);

    if(lang !== undefined){
      this.lang = lang;
    }

    if(this.lang === 'ru') {
      $('body').addClass('ru');
    } else {
      $('body').removeClass('ru');
    }

   // this.lang = 'en';

    translate.setDefaultLang(this.lang);

    // the lang to use, if the lang isn't available, it will use the current loader to get them

    translate.use(this.lang);
    
  }

  isGroupShown(group) {
    return this.shownGroup === group;
  }

  toggleGroup(group) {
    if (this.isGroupShown(group)) {
        this.shownGroup = null;
    } else {
        this.shownGroup = group;
    }
  }

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

}
