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