import { Component, OnInit } from '@angular/core';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DocumentService } from 'src/app/service/document.service';
import { DocumentContentModel } from 'src/app/model/document-content.model';
import { ActivatedRoute } from '@angular/router';
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-view-list-doc',
  templateUrl: './view-list-doc.component.html',
  styleUrls: ['./view-list-doc.component.scss']
})
export class ViewListDocComponent implements OnInit {
  page = 1;
  pageSize = 10;
  listDoc:DocumentContentModel[]=[]
  search:string = ''
   checkType:string = '0'
  constructor(private modalService: NgbModal,private documentService:DocumentService,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:DocumentContentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.documentService.deleteExcelContent(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถลบข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }

  async downloadFile(logId:string,lang:string){
    try {
      const data = await this.documentService.downloadFileContent(logId,lang).toPromise();
      if(data){
        FileSaver.saveAs(new Blob([data]), "file_download.doc");
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  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.getListExcelContent().toPromise();
      this.listDoc = data.map(x => new DocumentContentModel(x))
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  ngOnInit() {
    this.getListDoc();
  }

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