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-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  public loginForm !: FormGroup;
  loginStatus: any;
  loginvalidflag: boolean = false;
  loginvalidpassword:boolean=false;
  validationmsg: any;
  validationmsg1:any;
  constructor(private router: Router, private http: HttpClient, private commonservice: CommonFunctionService) {
    console.log("in login");
  }

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

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

  loginUser(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 + "/loginuser", logindata).subscribe(response => {
   //   console.log("in response", response);
      console.log("in response", response.errors);
      

      // console.log("in response---->",response.data[0]["loginStatus"],response.data[0]["jwttoken"]);


      if (response.errors) {
        console.log("check condition",response.errors.search("Username"));
        if(response.errors.search("Username") > 0){
          console.log("in if condition");
          this.loginvalidflag = true;
          this.validationmsg = response.errors;

        }else{
          console.log("in else condition")
          this.loginvalidpassword=true;
          this.loginvalidflag = false;
          this.validationmsg1 = response.errors;

        }

      } else {
        this.loginStatus = response.data[0]["loginStatus"];
        this.loginvalidflag = false;
        this.loginvalidpassword=false;

        if (this.loginStatus == '0') {
          console.log("valid user");
          localStorage.setItem('tokenkey', response.data[0]["jwttoken"]);
          var logindata = response.data[0]["logindata"];

          const loginData = {
            user_id: logindata["user_id"],
            user_name: logindata["user_name"],
            last_loginDate: logindata["last_loginDate"],
            role:logindata["role"]
          }
      //    console.log("response", loginData);
          localStorage.setItem('loginData', JSON.stringify(loginData));


          this.router.navigate(['/home']);

        } else {
          console.log("in invalid user");
          this.loginvalidpassword=true;
          this.loginvalidflag = false;
          this.validationmsg1 = "Please enter valid credentials";

        }
      }
      //  console.log("res", response["loginStatus"]);



    })




  }

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


  }

  onKeypressEvent(event: any){

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

 onKeypressEvent1(event: any){

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

}

}





