import { ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core';
import { DocumentManagerModel, MyDocumentManagerModel } from 'src/app/shared/model/document-manager.model';
import { DocumentManagerService } from 'src/app/shared/services/documents.service';
import { FileService } from 'src/app/shared/services/file.service';
import Swal from 'sweetalert2';
interface SubModule {
  title: SafeHtml;
  route?: string;
  file?: string;
}

@Component({
  selector: 'app-training-roadmap',
  templateUrl: './training-roadmap.component.html',
  styleUrls: ['./training-roadmap.component.scss']
})
export class TrainingrRoadmapComponent {
  menuList = [
    [{ text: "ยุทธศาสตร์ในการพัฒนาพนักงาน", description: "" },
    { text: "Master Blueprint สำหรับการพัฒนาพนักงาน", description: "" },
    { text: "ประเภทหลักสูตร", description: "" }],
    [{ text: "เงื่อนไขในการเข้าอบรมในแต่ละหลักสูตร", description: "" },
    { text: "Course outline", description: "" },
    { text: "ข้อมูล E-learning", description: "" }],
    [{ text: "ข้อมูลวิทยากร", description: "" },
    { text: "เอกสารแนบอื่น ๆ", description: "" },
    { text: "ข้อมูลงบประมาณ", description: "" }],
    [{ text: "ข้อมูลประวัติฝึกอบรม", description: "" }],
  ];

  documentManager: { loading: boolean, dataList: DocumentManagerModel[] } = { loading: false, dataList: [] }

  constructor(private sanitizer: DomSanitizer,
    private fileService: FileService,
    private cdr: ChangeDetectorRef,
    private translateService: TranslateService,
    private documentManagerService: DocumentManagerService
  ) {

  }

  ngOnInit(): void {
    this.getDocumentManagerList()
  }

  getDocumentManagerList() {
    this.documentManager.loading = true
    this.documentManagerService.getList().subscribe({
      next: response => {
        this.documentManager.dataList = response.map(x => {
          return new MyDocumentManagerModel(x)
        })
        this.documentManager.loading = false
        this.cdr.detectChanges();
      }, error: error => {
        this.documentManager.loading = false
        console.error('Error fetching employee types:', error);
        this.cdr.detectChanges()
      }
    })
  }
  openReport(file: string) {
    const url = 'assets/reports/' + file;
    window.open(url, '_blank');
  }

  downloadFile(fileName: string) {
    this.fileService.downloadFiles(fileName).subscribe({
      next: response => {
        const url = window.URL.createObjectURL(response);
        const a = document.createElement("a");
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
        this.cdr.detectChanges()
      }, error: error => {
        this.showAlert(error.message, 'error')
        this.cdr.detectChanges()
      }
    })
  }

  showAlert(text: string, type: 'success' | 'error') {
    Swal.fire({
      icon: type,
      title: 'แจ้งเตือน',
      text: text,
      showCancelButton: false,
      confirmButtonText: 'ยืนยัน',
    })
  }


  translateText(th?: string, en?: string) {
    return this.translateService.getCurrentLang() == 'th' ? (th || '') : (en || '')
  }


}


