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


  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()
    })
  }
  coverDate(date:string){
  return  date.split('-').reverse().join('/')
  }
}
