import { Component, OnInit } from '@angular/core';

import { BsModalRef, ModalOptions, BsModalService } from 'ngx-bootstrap/modal';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { DepartmentService } from '../department/department.service';
import { CommondialogComponent } from '../../Shared/commondialog/commondialog.component';
import { DialogService } from 'ng2-bootstrap-modal';
import { NgxSpinnerService } from 'ngx-spinner';
import { UserMailFolderMapping } from './usermailfoldermapping';
import { UsermailfoldermappingService } from './usermailfoldermapping.service';
import {MatDialog, MatDialogConfig} from "@angular/material";
import { MailfolderpopupComponent } from '../mailfolderpopup/mailfolderpopup.component';
import { DashboardService } from 'src/app/dashboard/dashboard.service';
@Component({
  selector: 'app-usermailfoldermapping',
  templateUrl: './usermailfoldermapping.component.html',
  styleUrls: ['./usermailfoldermapping.component.css']
})
export class UsermailfoldermappingComponent implements OnInit {
  actionRights: any; _menuId: any; readOnly: boolean; add: boolean; edit: boolean; delete: boolean; submitShow: boolean;
  useridnew: string = localStorage.getItem('userid');
  key = 'usrmailfoldername';
  reverse = false;
  showall = true;
  showallstatus = false;
  mailfolderlst :UserMailFolderMapping[] =[];
  sam:UserMailFolderMapping[] =[];
  itemsPerPage = 10;
  p = 1;
  useridlogin:any;
  selected_user:any;
  user_option:boolean = false;
  public searchText: string;
  originallst : any [];
  testing:any[];
  user_list:any[];
  foldermappingactive:UserMailFolderMapping[]=[];
  constructor(
    private usermailfoldermapping_service:UsermailfoldermappingService,
    private router: Router,
    private dialog: MatDialog,
    private SpinnerService: NgxSpinnerService,
    private dashboardservice: DashboardService,
    private dialogService: DialogService,
    private toastr: ToastrService) {
    // get rights for the logged in user for the particular component
    if (this.router.getCurrentNavigation().extras.state !== undefined) {
      localStorage.setItem('Usermailfoldermapping_menuid', this.router.getCurrentNavigation().extras.state.menuid);
      localStorage.setItem('Usermailfoldermapping_userRights', this.router.getCurrentNavigation().extras.state.action);
      this.actionRights = [];
      this.actionRights = this.router.getCurrentNavigation().extras.state.action;
    } else {
      this.actionRights = [];
      this.actionRights = localStorage.getItem('Usermailfoldermapping_userRights');
    }

  }

  ngOnInit(): void {
    this.onload();
    this.SpinnerService.show();
  }

  onload(){
    this.usermailfoldermapping_service.Get_allusermailmapping().subscribe(res => {
      debugger;
     // var demo = JSON.parse(res[0]);
    //  console.log(demo);

      this.mailfolderlst = res.body;
      console.log(this.mailfolderlst.length);
      this.SpinnerService.hide();
     // this.originallst =JSON.parse(res.body);
    }, error => {
     
      this.toastr.error('Error while getting user folder mapping data');
    });

  
  }
  sort(key) {
    this.key = key;
    this.reverse = !this.reverse;
  }

  edit_userfoldername(folder_details){
    debugger;
    this.router.navigate(['/masters/addusermailfoldermapping'], 
    { queryParams: { usrmailfolderid: folder_details.usrmailfolderid,userid:folder_details.userid,usrmailfoldermappingid:folder_details.usrmailfoldermappingid,usrmailfoldername:folder_details.usrmailfoldername,button_type:'edit'} });
    //{ queryParams: { data: folder_details} });

  }

  edit_foldername(user){
    // this.router.navigate(['/masters/mailfolderpopup'], 
    // { queryParams: { usrmailfolderid: user.usrmailfolderid,usrmailfoldername:user.usrmailfoldername,button_type:'edit'} });
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = true;
    dialogConfig.autoFocus = true;
    dialogConfig.data = {
      usrmailfolderid: user.usrmailfolderid,
      usrmailfoldername:user.usrmailfoldername,
      button_type:'edit'
  };
    const dialogRef = this.dialog.open(MailfolderpopupComponent,dialogConfig);
    dialogRef.afterClosed().subscribe(
      //data => this.fn_LoadProjectJobDetails(this.projectid, this.projectname) 
     data => this.onload()
  );
  }

  

  getactiveinactive(showallp) {
    this.SpinnerService.show();
    this.foldermappingactive = [];
    this.showall = showallp;
    this.showallstatus = showallp;
    if (this.showall === true) {
      this.showall = false;
      this.mailfolderlst.forEach(data => {
        if (data.active === 1) {
          this.foldermappingactive.push(data);
        }
      });
      this.mailfolderlst = [];
      this.mailfolderlst = this.foldermappingactive;
    } else {
      this.showall = true;
      this.mailfolderlst = [];
      this.onload();
    }
    this.SpinnerService.hide();
  }

  add_userfoldername(folder_details){
    debugger;

    this.router.navigate(['/masters/addusermailfoldermapping'], 
    { queryParams: { usrmailfolderid: folder_details.usrmailfolderid,userid:folder_details.userid,usrmailfoldermappingid:folder_details.usrmailfoldermappingid,usrmailfoldername:folder_details.usrmailfoldername.toString(),button_type:'add'} });

  }

  turn_onuserdropdow(){
    this.useridlogin = +localStorage.getItem('userid');
    debugger;
    if(this.user_option == false){
      this.user_option =true;
     
    }else{
      this.user_option = false;
    }
    //console.log(user);
  }

  Add_mailfolder(){
   // this.toastr.success('Error while getting users data');
    const dialogConfig = new MatDialogConfig();
      dialogConfig.disableClose = true;
      dialogConfig.autoFocus = true;
      dialogConfig.data = {
        button_type:'add'
    };
      const dialogRef = this.dialog.open(MailfolderpopupComponent,dialogConfig);
      dialogRef.afterClosed().subscribe(
        //data => this.fn_LoadProjectJobDetails(this.projectid, this.projectname) 
       data => this.onload()
    );
  }

  OpenConfirmTemplate(user: UserMailFolderMapping) {
    let confmsg;

    if (user.active === 0) {
      confmsg = 'activate';
    } else {
      confmsg = 'deactivate';
    }
    this.dialogService.addDialog(CommondialogComponent,
      {
        title: 'Please Confirm',
        message: 'Are you sure you want to ' + confmsg + ' this record ?',
        hclass: 'modelsuccess',
        oktext: 'Yes',
        canceltext: 'No',
        okshow: true,
        cancelshow: true
      }).subscribe((isConfirmed) => {
        if (isConfirmed) {
          this.Delete(user);
        }

      });
  }

  Delete(user:UserMailFolderMapping){
    let msg;
    this.useridlogin = +localStorage.getItem('userid');
    debugger;
    this.SpinnerService.show();
    if (user.active === 0) {
      user.active = 1;
      msg = 'User activated successfully';
    } else {
      user.active = 0;
      msg = 'User deactivated successfully';
    }
    var inputData = [];
    inputData.push({
      usrmailfoldermappingid: user.usrmailfoldermappingid,
      active: user.active,
      updatedby: this.useridlogin
    });
    this.usermailfoldermapping_service.Delete_mailfoldermapping(inputData[0]).subscribe(res => {
      this.toastr.success(msg);
      this.SpinnerService.hide();
      this.onload();
    }, error => {
      this.SpinnerService.hide();
      this.toastr.error('Operation is not successfull');
    });
  }
}
