import { Component, OnInit } from '@angular/core';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { CourseService } from 'src/app/service/course.service';
import { CourseModel } from 'src/app/model/course.model';
import { CourseContentModel } from 'src/app/model/course-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-course',
  templateUrl: './view-list-course.component.html',
  styleUrls: ['./view-list-course.component.scss']
})
export class ViewListCourseComponent implements OnInit {
  page = 1;
  pageSize = 10;
  listCourse:CourseContentModel[]=[]
  search:string = ''
  checkType:string = '0'
  constructor(private modalService: NgbModal,private courseService:CourseService,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()
    })
  }
  async downloadFile(logId:string){
    try {
      const data = await this.courseService.downloadFileContent(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.getListCourseContent().toPromise();
      this.listCourse = data.map(x => new CourseContentModel(x))
      console.log("🚀 ~ ViewListCourseComponent ~ getListCourse ~ this.listCourse :", this.listCourse )
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  deleteFile(item:CourseContentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.courseService.deleteCourseContent(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListCourse();
        } else {
      this.openAlertModal('ไม่สามารถลบข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  ngOnInit() {
    this.getListCourse();
  }
  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('/')
    }
}
