import { Component, OnInit } from '@angular/core';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { DocumentModel } from 'src/app/model/document.model';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DocumentService } from 'src/app/service/document.service';

@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:DocumentModel[]=[]
  search:string = ''
  constructor(private modalService: NgbModal,private documentService:DocumentService) {
  }
  getStatus(status:string){
    if(status == '0'){
      return 'รออนุมัติ'
    } else if(status == '1'){
      return 'เปิดใช้งาน'
    } else if(status == '2'){
      return 'ไม่อนุมัติ'
    }
  }
  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
    } 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()
    })
  }

}
