import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { CommonFunctionService } from '../services/commonfunctions.service';
import { response } from 'express';
import { Router } from '@angular/router';


@Component({
  selector: 'dvr-password',
  templateUrl: './password.component.html',
  styleUrls: ['./password.component.css']

})

export class PasswordComponent implements OnInit {

  public passwordForm !: FormGroup;
  loginStatus: any;
  loginvalidflag: boolean = false;
  loginvalidpassword: boolean = false;
  validationmsg: any;
  validationmsg1: any;
  error_msg: any;
  success_msg: any;
  status_msg: any;
  displayStyle = "none";
  checkoperationflag: boolean = false;
  entry_successflag: boolean = false;


  constructor(private router: Router, private commonservice: CommonFunctionService) {
    console.log("in password changes");
  }


  ngOnInit() {
    this.passwordForm = new FormGroup({
      email: new FormControl(null, Validators.required),
      password: new FormControl(null, [Validators.required, Validators.maxLength(8)])
    });
  }

  ngOnChanges() {
    console.log("this.passwordForm------>", this.passwordForm);
  }

  passwordUser(form: FormGroup) {
    console.log('Username', form.value.email);
    //console.log('password', form.value.password);


    const logindata = {
      username: form.value.email,
      password: form.value.password
    }

    //console.log("username and password ", logindata);

    this.validationmsg = "";
    this.validationmsg1 = "";
    this.loginvalidflag = false;
    this.loginvalidpassword = false;
    this.commonservice.getData(this.commonservice.projectpath + "/changepassword", logindata).subscribe(response => {
      //   console.log("in response", response);
      console.log("in response", response);


      if (response.data.length == 0) {
        //warning 
        this.openPopup();
        this.status_msg = "Warning";
        this.error_msg = response.errors;
        this.entry_successflag=true;
      } else if (response.data.length > 0) {
        console.log("inside if(response.message== '') ")
        this.openPopup();
        this.status_msg = "Success";
        this.error_msg = response.data[0].message;
        this.checkoperationflag=true;
        console.log("error_msg", this.error_msg);
      }
      else {
        this.status_msg = "";
        this.error_msg = "";
      }


    })




  }

  onCancle() {
    console.log("in cancel");
    this.validationmsg = "";


  }

  onKeypressEvent(event: any) {

    // console.log("in onkey press",event.target.value);
    this.loginvalidflag = false;

  }

  openPopup() {
    this.displayStyle = "block";
  }
  onKeypressEvent1(event: any) {

    // console.log("in onkey press",event.target.value);
    this.loginvalidpassword = false;

  }

  closePopup() {
    this.displayStyle = "none";

    if (this.checkoperationflag == true) {
      this.router.navigate(['/login'])

    } else {
      if (this.entry_successflag == true) {
        window.location.reload();
      }
    }
  }

}





