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 , AbstractControl} from '@angular/forms';
import { Storage } from '@ionic/storage';

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

	private changepswform : FormGroup;
	user_data:any;
	
	constructor(public navCtrl: NavController, public navParams: NavParams,private myservice: Myservice, public formBuilder : FormBuilder,public storage:Storage) {
		 
						
		this.changepswform = this.formBuilder.group({ 
			password: ['', Validators.required ],
			cpassword: ['', Validators.required],
		}, {
			validator: this.Match('password', 'cpassword')
		});
	
		this.storage.get('user_data').then((user_data) => {
				console.log(user_data);
				if(user_data){
					this.user_data=user_data;
					
				}
			})
	}

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

	login(){
		this.navCtrl.push('LoginPage');
	}
	
  	change_psw(){
		console.log(this.changepswform.value);
		this.myservice.show_loader();
		var obj = {'cpassword': this.changepswform.value.cpassword,'password': this.changepswform.value.password,'id':this.user_data.id}
			this.myservice.load_post(obj,'changepsw').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == "success") {
				 this.myservice.show_alert("Success" , data.message);
				  this.navCtrl.push('AddcarPage');
			}
			else {
			   this.myservice.show_alert("Error" , data.message);
			}
		})
	}
	
	Match(firstControlName, secondControlName) {
    return (AC: AbstractControl) => {
      let firstControlValue = AC.get(firstControlName).value; // to get value in input tag
      let secondControlValue = AC.get(secondControlName).value; // to get value in input tag
      if (firstControlValue != secondControlValue) {
        AC.get(secondControlName).setErrors({mismatch: true});
        console.log(false);
      } else {
        console.log(true);
        return null
      }
    };
  }
	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();
  }

}
