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-email',
  templateUrl: './email.component.html',
  styleUrls: ['./email.component.scss']
})
export class EmailComponent implements OnInit {
  userForm: FormGroup | any;  formData:any =[]

   emailDetails: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({

     


      port: new FormControl('', [Validators.required]),
       secure: new FormControl('', [Validators.required]),
      hostname: new FormControl('', [Validators.required]),
      // contactNo: new FormControl('', [Validators.required, Validators.maxLength(20)]),
   
      // 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]),
    
        
    });
    let userRole = localStorage.getItem('roleId');

   this. getemaillistApi(userRole)
  }
  close(){

  }

  onSubmit(){
    this.formData =[]
    this.formData= new FormData();
    let tmp = this.userForm .value
    console.log(tmp)

    this.emailDetails
    let param ;
    if( this.emailDetails.length>0){
     param = {host:tmp.hostname, user:tmp.email,pass:tmp.password,port:tmp.port,secure:tmp.secure,id: this.emailDetails[0].id}
    }else{
      param = {host:tmp.hostname, user:tmp.email,pass:tmp.password,port:tmp.port,secure:tmp.secure}
    }

 
   console.log(param)
     
  
  

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

  getemaillistApi(id){
    let param={}
    this._api.getSmtpList(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
        this.emailDetails = res.data
       let passData =  res.data[0]
       passData.email =  res.data[0].user
       passData.password =  res.data[0].pass
       passData.hostname =  res.data[0].host
     
       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);
  }
}
