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

  deleteFile(item:CourseModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.courseService.deleteCourse(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListCourse();
        } 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");
  }
}
