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({ 
			old_password: ['', Validators.compose([Validators.required, Validators.minLength(6),Validators.maxLength(25)])],
			password: ['', Validators.compose([Validators.required, Validators.minLength(6),Validators.maxLength(25)]) ],
			cpassword: ['', Validators.compose([Validators.required, Validators.minLength(6),Validators.maxLength(25)])],
		}, {
			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(){
		 if(this.changepswform.valid)
      	{
		console.log(this.changepswform.value);
		this.myservice.show_loader();
		var obj = {'oldpassword': this.changepswform.value.old_password,'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("Password is Changed!" , data.message);
				  this.navCtrl.push('AddcarPage');
			}
			else {
			   this.myservice.show_alert("Error" , data.message);
			}
		})
		}	else{ // Form is not valid
      
        	this.markFormGroupTouched(this.changepswform); // function to mark fields as touched 
      	}
	}
	
	  	private markFormGroupTouched(formGroup: FormGroup) {
    	(<any>Object).values(formGroup.controls).forEach(control => {
      		control.markAsTouched();
      		if (control.controls) {
        		control.controls.forEach(c => this.markFormGroupTouched(c));
      		}
    	});
  	}
	
	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();
  }  
  
  

}
