import { Component, ViewChild, ElementRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CommonProvider } from '../../providers/common/common';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';

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

/**
 * Generated class for the ForgotPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-forgot',
  templateUrl: 'forgot.html',
})
export class ForgotPage {

  @ViewChild('language') language: ElementRef;
  lang =  'en';

	ForgotForm: FormGroup;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private formBuilder: FormBuilder,
    public service: CommonProvider,
    public translate: TranslateService,
    public subjectService: SubjectService,
    public service_lang: WebService_lang, 
    ) {
  	this.ForgotForm = this.formBuilder.group({
     email_id: ['', Validators.compose([Validators.required, Validators.pattern("[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}")])],
    })

    
    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);




  }

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

  forgot(){
  	this.service.show_loader();
   console.log(this.ForgotForm.value);
    this.service.post_data('forgotPwd',this.ForgotForm.value).subscribe(response=>{
      if (response.status == 'success'){        
        var This = this;
        this.service.hide_loader();
        this.service.presentToast(response.message);
        setTimeout(() => {          
          This.navCtrl.setRoot('LoginPage')
        }, 500);
      }
      else{
      	this.service.hide_loader();
        this.service.presentToast("Invalid username and Password");
      }
    }, (err) => {
      var data = JSON.parse(err);
      console.log(data);
      this.service.presentToast(data['message']);
      this.service.hide_loader();
    })
 }

 signup(){
    this.navCtrl.push("SignupPage");
  }

}
