import { Component, Inject, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material";
import { ActivatedRoute, Router } from '@angular/router';
import { EOPNOTSUPP } from 'constants';
import { NgxSpinnerService } from 'ngx-spinner';
import { ToastrService } from 'ngx-toastr';
import { empty } from 'rxjs';
import { Useremailmapping } from 'src/app/usermanagement/useremailmapping/useremailmapping';
import { UserMailFolderMapping } from '../usermailfoldermapping/usermailfoldermapping';
import { UsermailfoldermappingService } from '../usermailfoldermapping/usermailfoldermapping.service';

@Component({
  selector: 'app-mailfolderpopup',
  templateUrl: './mailfolderpopup.component.html',
  styleUrls: ['./mailfolderpopup.component.css']
})
export class MailfolderpopupComponent implements OnInit {
  _userId: number;
  actionRights: any;
  useridlogin:any;
  mailfolderForm: FormGroup;
  folderid:any;
  foldername:any;
  button_type:any ;
  constructor(
    private router: Router,
    private toastr: ToastrService,
    private route: ActivatedRoute,
    private formBuilder: FormBuilder,
    private usermailfolderserivces: UsermailfoldermappingService,
    private SpinnerService: NgxSpinnerService,
    private usermailfoldermapping_service:UsermailfoldermappingService,
    private dialogRef: MatDialogRef<MailfolderpopupComponent>,
    @Inject(MAT_DIALOG_DATA) data
    ) {
      debugger;
      //console.log(data);
      if(data.button_type == 'edit'){
        this.folderid =data.usrmailfolderid;
        this.foldername=data.usrmailfoldername;
        this.button_type=data.button_type;
      }else{
        this.button_type=data.button_type;
      }

    
    // this._userId = +this.route.snapshot.queryParamMap.get('userid');

    // // get rights for the logged in user for the particular component
    // if (this.router.getCurrentNavigation().extras.state !== undefined) {
    //   localStorage.setItem('UserAdd_menuid', this.router.getCurrentNavigation().extras.state.menuid);
    //   localStorage.setItem('UserAdd_userRights', this.router.getCurrentNavigation().extras.state.action);
    //   this.actionRights = [];
    //   this.actionRights = this.router.getCurrentNavigation().extras.state.action;
    // } else {
    //   this.actionRights = [];
    //   this.actionRights = localStorage.getItem('UserAdd_userRights');
    // }
   }

  ngOnInit(): void {
    this.mailfolderForm = this.formBuilder.group({
      usrmailfolderid: [''],
      usrmailfoldername: ['', [Validators.required]],
    });
      if(this.button_type==='edit'){
        this.mailfolderForm.controls['usrmailfolderid'].setValue(this.folderid);
        this.mailfolderForm.controls['usrmailfoldername'].setValue(this.foldername);
      }else{
        this.mailfolderForm.reset();
        this.mailfolderForm.controls['usrmailfoldername'].setValue(0);
      }
  }
  close(){
    this.dialogRef.close();
  }

  Postmailfolder(mailfolder: UserMailFolderMapping){
if(this.button_type==='add')
{
  if (this.mailfolderForm.valid) {
    this.useridlogin = +localStorage.getItem('userid');
    if (+mailfolder.usrmailfoldername === null || +mailfolder.usrmailfoldername === undefined || +mailfolder.usrmailfoldername ===0) {
      this.toastr.error('Please enter the Folder Name');
      return;
    }
    mailfolder.active =1;
    mailfolder.createdby =this.useridlogin;
    // const formData = new FormData();
    //   for (let key in mailfolder) {
    //     formData.set(key, mailfolder[key]);
    //   }
    var inputData = [];
    inputData.push({
      usrmailfoldername: mailfolder.usrmailfoldername,
      active: 1,
      createdby: this.useridlogin
    });
  this.usermailfoldermapping_service.Add_mailfoldername(inputData[0]).subscribe(res => {
debugger;
if (res.body !== 0) {
this.toastr.success('Successfully Folder name is added');
this.dialogRef.close();
}else{
this.toastr.error('Folder name is already exists...');
}
   // var Users = JSON.parse(res[0]);
  
  }, error => {
    this.SpinnerService.hide();
    this.toastr.error(error.error);
  });
   
  }else{
    if (+mailfolder.usrmailfoldername === null || +mailfolder.usrmailfoldername === undefined || +mailfolder.usrmailfoldername ===0) {
      this.toastr.error('Please enter the Folder Name');
      return;
    }

  }
}else if(this.button_type==='edit')
{
  debugger;
  if (this.mailfolderForm.valid) {
    this.useridlogin = +localStorage.getItem('userid');
    if (+mailfolder.usrmailfoldername === null || +mailfolder.usrmailfoldername === undefined || +mailfolder.usrmailfoldername ===0) {
      this.toastr.error('Please enter the Folder Name');
      return;
    }
    mailfolder.active =1;
    mailfolder.createdby =this.useridlogin;
    // const formData = new FormData();
    //   for (let key in mailfolder) {
    //     formData.set(key, mailfolder[key]);
    //   }
    var inputData = [];
    inputData.push({
      usrmailfoldername: mailfolder.usrmailfoldername,
      active: 1,
      usrmailfolderid:mailfolder.usrmailfolderid,
      updatedby: this.useridlogin
    });
  this.usermailfoldermapping_service.Update_foldername(inputData[0]).subscribe(res => {
debugger;
if (res.body !== 0) {
this.toastr.success('Folder name is updated successfully');
this.dialogRef.close();
}else{
this.toastr.error('Folder name is already exists...');
}
   // var Users = JSON.parse(res[0]);
  
  }, error => {
    this.SpinnerService.hide();
    this.toastr.error(error.error);
  });
   
  }else{
    if (+mailfolder.usrmailfoldername === null || +mailfolder.usrmailfoldername === undefined || +mailfolder.usrmailfoldername ===0) {
      this.toastr.error('Please enter the Folder Name');
      return;
    }

  }
}

   // this.toastr.error('Error in saving lot details');
    // this.usermailfoldermapping_service.Get_allusermailmapping().subscribe(res => {

    //   var Users = JSON.parse(res[0]);
     
    // }, error => {
    //   this.SpinnerService.hide();
    //   this.toastr.error('Error while getting users data');
    // });
    
  }
}
