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, NbDialogRef, NbDialogService } from '@nebular/theme';

@Component({
  selector: 'ngx-customer-form',
  templateUrl: './customer-form.component.html',
  styleUrls: ['./customer-form.component.scss']
})
export class CustomerFormComponent implements OnInit {
  data:any =[];
  userForm: FormGroup | any;  formData:any =[]
  heading:any =[];
  constructor(private dialogRef: NbDialogRef<CustomerFormComponent> ,public _api:PageapiService,private dialogService: NbDialogService) {

  
   }

  ngOnInit() {
    console.log('dialogRef', this.dialogRef.componentRef.instance);
    let details:any = this.dialogRef.componentRef.instance
            
   this.heading =  details.title

   this.data =  details.details
    this.userForm = new FormGroup({

     



       
      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}$'
        )]),
        address_1:  new FormControl('', [Validators.required]),
        state:  new FormControl('', [Validators.required]),
        city:  new FormControl('', [Validators.required]),
        pincode: new FormControl('', [
          Validators.required,
          Validators.pattern('^[0-9]{6}$'), // Allow only 6 digits
        ]),
        cusType:  new FormControl('', [Validators.required]),
      
    });
    this.data.address_1 = this.data['CusDetails.address_1']
    this.data.state = this.data['CusDetails.state']
    this.data.city = this.data['CusDetails.city']
    this.data.pincode = this.data['CusDetails.pincode']
    this.data.cusType = this.data['CusDetails.cusType']
    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.dialogRef.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);
     
    }  

  let cus:any = {  cusName : tmp.userName,
    email  : tmp.email,
    phoneNo  : tmp.mobileNo,
    address_1 : tmp.address_1,
    state  : tmp.state,
    city  : tmp.city,
    pincode:tmp.pincode,
    cusType:tmp.cusType,

    
  }

   if(this.data.hasOwnProperty('customerId')){
     cus.id = this.data.customerId
  }else{
    
  }
  console.log(cus)

    this._api.CreateCustomersDetails (cus).subscribe(res => {
      if (res.status == 200) {
        console.log(res)

        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', 'CUSTOMER');
        this.formData.append('mobileNo', tmp.mobileNo);  

        if(this.data.hasOwnProperty('customerId')){
          //cus.id = this.data.customerId
          this.formData.append('customerId',this.data.customerId);
       }else{
        this.formData.append('customerId', res.data.id);
       }

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

}
}
