import { Component, OnInit } from '@angular/core';
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { DocumentModel } from 'src/app/model/document.model';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
import { DocumentService } from 'src/app/service/document.service';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { OpenImageComponent } from '../open-image/open-image.component';
declare var require: any
const FileSaver = require('file-saver');
@Component({
  selector: 'app-approve-doc',
  templateUrl: './approve-doc.component.html',
  styleUrls: ['./approve-doc.component.scss']
})
export class ApproveDocComponent implements OnInit {
  page = 1;
  pageSize = 10;
  listDoc:DocumentModel[]=[]
  search:string = ''
  constructor(private modalService: NgbModal,private documentService:DocumentService) {
  }
  openEmployeeModal(image:string) {
    const modalRef = this.modalService.open(OpenImageComponent, {
      centered: true,
      windowClass: 'my-dialog-img-preview'
    })
    modalRef.componentInstance.linkImage = image
    modalRef.result.then(result => {
    }, reason => {
      this.modalService.dismissAll()
    })
  }
  async downloadFile(logId:string,lang:string){
    try {
      const data = await this.documentService.downloadFile(logId,lang).toPromise();
      if(data){
        FileSaver.saveAs(new Blob([data]), "file_download.doc");
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  openLink(url:string){
    window.open(url, "_blank");
  }
  filterListDoc(){
    return this.listDoc.filter(x => x.thName.toLowerCase().includes(this.search.toLowerCase())||x.engName.toLowerCase().includes(this.search.toLowerCase()))
  }
  async getListDoc(){
    try {
      const data = await this.documentService.getListDoc('0').toPromise();
      this.listDoc = data.map(x => new DocumentModel(x))
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  onApprove(item:DocumentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการอนุมัติข้อมูลหรือไม่'
    modalRef.result.then(result => {
      item.status = 1
      this.documentService.approve(item).subscribe(result => {
        if (result) {
          this.modalService.dismissAll()
          this.openAlertModal('บันทึกข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถบันทึกข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  onCancelApprove(item:DocumentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการไม่อนุมัติข้อมูลหรือไม่'
    modalRef.result.then(result => {
      item.status = 2
      this.documentService.approve(item).subscribe(result => {
        if (result) {
          this.openAlertModal('บันทึกข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถบันทึกข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  ngOnInit() {
    this.getListDoc();
  }
  deleteFile(item:DocumentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.documentService.deleteExcel(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถลบข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }

  openAlertModal(message?: string) {
    const modalRef = this.modalService.open(AlertModalComponent, {
      centered: true,
      backdrop: 'static'
    })
    modalRef.componentInstance.message = message ? message : ""
    modalRef.result.then(result => {
      // this.modalService.dismissAll()
    }, reason => {
      // this.modalService.dismissAll()
    })
  }

}
