import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {Myservice} from '../../providers/myservice'
import { ValidationService } from '../../providers/validation_service';
import {Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { Storage } from '@ionic/storage';

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

	private forgot : FormGroup;
	user_data:any;
	
	constructor(public navCtrl: NavController, public navParams: NavParams,private myservice: Myservice, public formBuilder : FormBuilder,public storage:Storage) {
		this.forgot = this.formBuilder.group({ 
							email: ['', [Validators.required, this.emailValidator.bind(this)]],
						});
	}

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

	login(){
		this.navCtrl.push('LoginPage');
	}
	
  	on_submit(){
		console.log(this.forgot.value);
		this.myservice.show_loader();
		var obj = {'email': this.forgot.value.email}
			this.myservice.load_post(obj,'forgotpassword').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == "success") {
				 this.myservice.show_alert("Success" , data.message);
			}
			else {
			   this.myservice.show_alert("Error" , data.message);
			}
		})
	}
	
	emailValidator(control: FormControl): {[s: string]: boolean} {
		if (!(control.value.toLowerCase().match('^[a-z0-9.]+( \ [_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)\\.(\.[a-z0-9-]{1,15})$'))) {
		return {invalidEmail: true};
		}
	}
  back()
  {
    this.navCtrl.pop();
  }

}
