import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { PageapiService } from '../../pageapi.service';
import { NbWindowService ,NbWindowRef, NB_WINDOW_CONTEXT } from '@nebular/theme';
@Component({
  selector: 'ngx-driver-form',
  templateUrl: './driver-form.component.html',
  styleUrls: ['./driver-form.component.scss']
})
export class DriverFormComponent implements OnInit {
  driver :any;
  mail_exist :any;
  userForm: FormGroup | any;  formData:any =[]
  constructor(@Inject(NB_WINDOW_CONTEXT) public data: any,public _api:PageapiService,private windowService: NbWindowService, private windowRef: NbWindowRef) {
    this.getuserlistApi()
    console.log(this.data)
   }

  ngOnInit(): void {

    this.userForm = new FormGroup({

     



      // role: new FormControl('', [Validators.required]),
      userName: new FormControl('', [Validators.required]),
      // contactNo: new FormControl('', [Validators.required, Validators.maxLength(20)]),
      mobileNo: new FormControl('', [Validators.required,  Validators.minLength(10),Validators.maxLength(10)]),
      // lastName: new FormControl('', [Validators.required, Validators.maxLength(20)]),
      email: new FormControl('', [Validators.required, Validators.email,Validators.pattern(
        '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,63}$',
      )]),
      // password: new FormControl('', [Validators.required, Validators.minLength(6)]),
        password: new FormControl('', [Validators.required,Validators.pattern(
          '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,16}$'
        )]),
      
    });
    if(this.data.hasOwnProperty('id')){
      delete this.data.password  
      this.userForm.controls['password'].clearValidators();
      this.userForm.controls['password'].updateValueAndValidity();
      }
    this.userForm.patchValue(this.data)
  }

  cancel(){
    this.windowService
    console.log( this.windowService)
    this.windowRef.close();
  }
  onSubmit(){

    console.log(this.userForm.value)
    let tmp = this.userForm.value
    this.formData =[]
    this.formData= new FormData();
    if(this.data.hasOwnProperty('id')){
      this.formData.append('id', this.data.id);
      this.formData.append('userName',tmp.userName) 
      this.formData.append('email', tmp.email);
      if(tmp.password != ""){
        this.formData.append('password', tmp.password);
      }
  
      this.formData.append('role', 'DRIVER');
      this.formData.append('mobileNo', tmp.mobileNo);  
    }  else{
    
      this.formData.append('userName',tmp.userName) 
      this.formData.append('email', tmp.email);
      this.formData.append('password', tmp.password);
      this.formData.append('role', 'DRIVER');
      this.formData.append('mobileNo', tmp.mobileNo);  
    }
 

    this._api.addUserFormDetails (this.formData).subscribe(res => {
      if (res.status == 200) {
console.log(res)
this.windowRef.close(1);
      }else{
        this.windowRef.close(0);
      }
    })

}

onInputChange(event: any) {
  const enteredText = event.target.value;
  const userWithEmail = this.driver.some(user => user.email === enteredText);
  this.mail_exist = userWithEmail;

  // You can perform additional actions with the entered text here
}

getuserlistApi(){
  let param ={}
  this._api.getusers(param).subscribe(res => {
    console.log(res)
    if(res.status == 200){

     let passData =  res.data
     let driver = passData.filter(arr=>{return arr.role == 'DRIVER'}) 
     driver.forEach((element:any, index) => {
      element.index = index       +1  
      
    });
    this.driver = driver
     console.log(driver)
      //this.source.load(driver);
    }
  })
}

}
