import { Component, OnInit, TemplateRef } from '@angular/core';
import { DashboardService } from '../dashboard.service';
import { BsModalRef, ModalOptions, BsModalService } from 'ngx-bootstrap';
import { CommondialogComponent } from 'src/app/Shared/commondialog/commondialog.component';
import { DialogService } from 'ng2-bootstrap-modal';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-mailbox',
  templateUrl: './mailbox.component.html',
  styleUrls: ['./mailbox.component.css']
})
export class MailboxComponent implements OnInit {
  searchMail: string;
  loginuserid: string = localStorage.getItem('userid');
  mailbox = [];
  userid: number;
  omailbox: any;
  rowCollectionm = [];
  mailModalRef: BsModalRef;
  config: ModalOptions = {
    // backdrop: 'static',
    keyboard: false,
    animated: true,
    class: 'modal-dialog-centered modal-lg',
    ignoreBackdropClick: false
  };
  showMailValue = [];
  constructor(private dashboardservice: DashboardService,
    private modalService: BsModalService,
    private dialogService: DialogService,
    private toastr: ToastrService,
  ) { }

  ngOnInit(): void {
    this.userid = +localStorage.getItem('userid');
    this.getMailBoxDetails();
  }
  getMailBoxDetails() {
    const inputvalue = [];
    inputvalue.push({ userid: +this.loginuserid });
    this.dashboardservice.getMailBoxDetails(inputvalue[0]).subscribe(res => {
      if (res !== undefined && res !== null && res.body[0] !== undefined && res.body[0] !== null) {
        if (JSON.parse(res.body[0]).length > 0) {
          // this.userprojectlist = JSON.parse(res.body[0]);
          console.log(JSON.parse(res.body[0]));
          this.mailbox = JSON.parse(res.body[0]);
          this.rowCollectionm = JSON.parse(res.body[0]);
        }
      }
    });
  }
  mailview(template: TemplateRef<any>, mail_id: string) {
    this.showMailValue = this.mailbox.filter(Obj => Obj.mail_id === mail_id);
    console.log('this.showMailValue=', this.showMailValue);
    if (!this.showMailValue[0].readflag) {
      this.dashboardservice.UpdateMailbox(this.showMailValue[0]).subscribe(res => {
        if (res.status === 200) {
          if (res !== undefined) {
            if (res.body !== 0) {
              this.getMailBoxDetails();
            } else { }
          }
        }
      },
        error => {
          this.toastr.error('Error while doing the operation');
        });
    }
    this.mailModalRef = this.modalService.show(template, this.config);
    const aa = document.getElementsByClassName('modal-content');
    aa[0].classList.remove('modal-content');
    this.showMailValue = this.mailbox.filter(Obj => Obj.mail_id === mail_id);
  }
  DeleteMail() {
  }

  OpenConfirmTemplate(skill, template: TemplateRef<any>) {
    this.dialogService.addDialog(CommondialogComponent,
      {
        title: 'Please Confirm',
        message: 'Are you sure you want to delete this record ?',
        hclass: 'modelsuccess',
        oktext: 'Yes',
        canceltext: 'No',
        okshow: true,
        cancelshow: true
      }).subscribe((isConfirmed) => {
        if (isConfirmed) {
          this.omailbox = skill;
          this.Delete();
        }
      });
  }

  Delete() {
    let msg;
    this.omailbox.active = 0;
    msg = 'Record delted successfully';
    this.userid = +localStorage.getItem('userid');
    this.omailbox.updatedby = this.userid;
    this.dashboardservice.Delete(this.omailbox).subscribe(res => {
      this.toastr.success(msg);
      this.getMailBoxDetails();
      if (this.mailModalRef) {
        this.mailModalRef.hide();
      }
    },
      error => { });
  }
}
