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';

@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:CourseModel[]=[]
  search:string = ''
  constructor(private modalService: NgbModal,private courseService:CourseService) {
  }
  getStatus(status:string){
    if(status == '0'){
      return 'รออนุมัติ'
    } else if(status == '1'){
      return 'เปิดใช้งาน'
    } else if(status == '2'){
      return 'ไม่อนุมัติ'
    }
  }
  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
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }

  ngOnInit() {
    this.getListCourse();
  }

  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()
    })
  }
}
