import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { PageapiService } from '../pageapi.service';
import { NbComponentStatus, NbGlobalPhysicalPosition, NbGlobalPosition, NbToastrService } from '@nebular/theme';

@Component({
  selector: 'ngx-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss']
})
export class ProfileComponent implements OnInit {
  userForm: FormGroup | any;  formData:any =[]

 
  index = 1;
  destroyByClick = true;
  duration = 2000;
  hasIcon = true;
  admin:any =[];
  position: NbGlobalPosition = NbGlobalPhysicalPosition.TOP_RIGHT;
  preventDuplicates = false;
  status: NbComponentStatus = 'primary';
  constructor(public _api:PageapiService,private toastrService: NbToastrService) { }

  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}$'
        )]),
        password2: new FormControl('', [Validators.required,Validators.pattern(
          '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,16}$'
        )]),
    });
    let userRole = localStorage.getItem('roleId');

   this. getuserlistApi(userRole)
  }
  close(){

  }

  onSubmit(){
    this.formData =[]
    this.formData= new FormData();
    let tmp = this.userForm .value
    let userRole = localStorage.getItem('roleId');
      this.formData.append('id', userRole);
     
 
    this.formData.append('userName',tmp.userName) 
    this.formData.append('email', tmp.email);
    if(tmp.password != null && tmp.password2 != null){
      if(tmp.password == tmp.password2){
        this.formData.append('password', tmp.password);
      }
    }
   
    
  
    this.formData.append('mobileNo', tmp.mobileNo);  

    this._api.addUserFormDetails (this.formData).subscribe(res => {
      if (res.status == 200) {
console.log(res)
this.showToast('success', 'Data Changed Successfully', 'Profile Update  Completed..!');
      }else{
        this.showToast('warning', 'Data Changed Failed', 'Profile Update Failed..!');
      }
    })
  }

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

       let passData =  res.data[0]
       passData.password = null
       this.userForm.patchValue(passData)
   
      }
    })
  }


  private showToast(type: NbComponentStatus, title: string, body: string) {
    const config = {
      status: type,
      destroyByClick: this.destroyByClick,
      duration: this.duration,
      hasIcon: this.hasIcon,
      position: this.position,
      preventDuplicates: this.preventDuplicates,
    };
    const titleContent = title ? ` ${title}` : '';

    this.index += 1;
    this.toastrService.show(
      body,
      `${titleContent}`,
      config);
  }
}
