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

@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) {
  }
  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.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.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();
  }

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

}
