import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, MenuController } from 'ionic-angular';
import { FormGroup, Validators, FormBuilder } from '@angular/forms'
import { Myservice } from '../../providers/myservice'
import { TranslateService } from "@ngx-translate/core";
import { Storage } from "@ionic/storage";

@IonicPage()
@Component({
  selector: 'page-forgot',
  templateUrl: 'forgot.html',
})
export class ForgotPage {
  forgotForm: FormGroup
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, private formbuilder: FormBuilder, private myservice: Myservice, private translate: TranslateService, public storage:Storage, private menu:MenuController) {
    this.forgotForm = this.formbuilder.group({
      email: ['', Validators.compose([Validators.required, Validators.pattern("[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}")])],
      from:['driver']
    })
  }

  ionViewDidEnter(){
    this.menu.swipeEnable(false)
    this.langTrans()
  }

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

  forgot() {
    this.myservice.show_loader()
    this.myservice.load_post(this.forgotForm.value, 'Forget_password').subscribe(response => {
      this.myservice.hide_loader()
      if (response.status == 'success') {
        this.translate.get(['forgot.' + response.message]).subscribe(value => {
          this.myservice.show_alert('', value['forgot.' + response.message])
        })
        this.navCtrl.pop();
      }
      else {
        this.translate.get(['forgot.' + response.message]).subscribe(value => {
          this.myservice.show_alert('', value['forgot.' + response.message])
        })
      }
    })
  }


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

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