import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material';
import { ActivatedRoute, Router } from '@angular/router';
import { DialogService } from 'ng2-bootstrap-modal';
import { NgxSpinnerService } from 'ngx-spinner/lib/ngx-spinner.service';
import { ToastrService } from 'ngx-toastr';
import { DashboardService } from 'src/app/dashboard/dashboard.service';
import { UserMailFolderMapping } from '../usermailfoldermapping/usermailfoldermapping';
import { UsermailfoldermappingService } from '../usermailfoldermapping/usermailfoldermapping.service';

@Component({
  selector: 'app-addusermailfoldermapping',
  templateUrl: './addusermailfoldermapping.component.html',
  styleUrls: ['./addusermailfoldermapping.component.css']
})
export class AddusermailfoldermappingComponent implements OnInit {
testing :any[];
usermailform: FormGroup;
userid :any;
folderid:any;
mapping:any;
button_type:any;
foldername:any;
useridlogin:any;
  constructor( private usermailfoldermapping_service:UsermailfoldermappingService,
    private router: Router,
    private dialog: MatDialog,
    private formBuilder: FormBuilder,
    private route: ActivatedRoute,
   
    private dashboardservice: DashboardService,
    private dialogService: DialogService,
    private toastr: ToastrService) { }

  ngOnInit(): void {
    this.usermailform = this.formBuilder.group({
      usrmailfolderid: [''],
      usrmailfoldermappingid:[''],
      usrmailfoldername:[''],
      userid: [0]
    });
debugger;
//     this.userid = +this.route.snapshot.queryParamMap.getAll('data');
// this.mapping = +this.route.snapshot.queryParamMap.get('usrmailfoldermappingid');
// this.folderid = +this.route.snapshot.queryParamMap.get('usrmailfolderid');
// this.foldername = +this.route.snapshot.queryParamMap.get('usrmailfoldername');
// this.button_type = +this.route.snapshot.queryParamMap.get('button_type');
this.route.queryParams
      .subscribe(params => {
        console.log(params); 
debugger;
         this.userid = params.userid;
         this.mapping = params.usrmailfoldermappingid;
         this.folderid = params.usrmailfolderid;
         this.foldername = params.usrmailfoldername;
         this.button_type = params.button_type;

 // popular
      });

      this.onload();

  }

  ngAfterViewInit(): void {
    //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
    //Add 'implements AfterViewInit' to the class.
    debugger;
    // this.route.queryParams.subscribe(
    //   params => {
    //     this.folderid =  params['usrmailfoldername'];
    //     this.language=params['language'];
    //   }

if(this.mapping>0){
  this.usermailform.controls['usrmailfolderid'].setValue(this.folderid);
  this.usermailform.controls['usrmailfoldermappingid'].setValue(this.mapping);
  this.usermailform.controls['usrmailfoldername'].setValue(this.foldername);
  this.usermailform.controls['userid'].setValue(this.userid);
}else{
  this.usermailform.controls['usrmailfolderid'].setValue(this.folderid);
  this.usermailform.controls['usrmailfoldername'].setValue(this.foldername);

}
  }



  Backtouserlist(){
    this.router.navigate(['/masters/usermailfoldermapping']);
  }

  Postuser(form:UserMailFolderMapping){
    debugger;
    this.useridlogin = +localStorage.getItem('userid');
if(this.button_type === "add"){
  if (this.usermailform.valid) {
    if(+form.userid === 0 || form.usrmailfoldername ===null || form.usrmailfoldername === undefined ){
      this.toastr.error("Please select the user.");
      return;
    }
var inputData = [];
    inputData.push({
      userid: Number(form.userid),
      createdby: this.useridlogin,
      usrmailfolderid: Number(form.usrmailfolderid)
    });
    this.usermailfoldermapping_service.Add_usermailfolder(inputData[0]).subscribe(res => {
      debugger;

      this.toastr.success('Successfully mapped the user to the mail folder.');


  }, error => {
      this.toastr.error(error.error);
  });

  }else{
    this.toastr.error("Please fill the required field.");
    return;
  }
 }else if(this.button_type === 'edit'){
  
  if (this.usermailform.valid) {

    if(+form.userid === 0 || form.usrmailfoldername ===null || form.usrmailfoldername === undefined ){
      this.toastr.error("Please select the user.");
      return;
    }
var inputData = [];
    inputData.push({
      userid: Number(form.userid),
      updatedby: this.useridlogin,
      active:1,
      usrmailfolderid: Number(form.usrmailfolderid),
      usrmailfoldermappingid:Number(form.usrmailfoldermappingid)
    });
    this.usermailfoldermapping_service.Update_usermailfolder(inputData[0]).subscribe(res => {
      debugger;

      this.toastr.success('Successfully mapped the user to the mail folder.');

     //this.user_option=false;
    
  }, error => {
      this.toastr.error(error.error);
  });
}
else
{
  this.toastr.error("Please fill the required field.");
    return;
}
  }
}
  
onload(){
debugger;
    this.dashboardservice.Getusers(Number(localStorage.getItem('prdefid'))).subscribe(res => {
      if (res !== undefined && res !== null && res.body !== undefined && res.body !== null) {
          debugger;
          console.log(res);
         this.testing = JSON.parse(res.body[0]);
         //this.user_list = this.testing;
      
        // this.toastr.info(this.testing.toString());
      } else {
          console.log(res);
          this.toastr.info('No data found!');
      }
  }, error => {
      this.toastr.error('Error while getting data');
  });


  }
}
