import { getCourseScheduleDetail_gyt } from "@/api/curriculum.api"
import { getRecentCourseSchedule_gym } from "@/api/homePage.api"
import { httpAjaxLoadingErrMsg } from "@/plugin/httpAjax"
import useDialogConfirm from "@/hooks/useDialogConfirm"
import { format } from "@/libs/tools"
import router from "@/router"

/* 管乐迷 开始上课 */
export function handleStartClass_gym(id: string) {
   httpAjaxLoadingErrMsg(getRecentCourseSchedule_gym, id).then(res => {
      if (res.code === 200) {
         const { signInStatusEnum, isCallNames, coursewareDetailId, startClassTime, endClassTime, status, coursewareEnable } = res.data
         if (status === "OVER") {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts4.png"),
               text: `该课程已结束`,
               btnShow: [true]
            })
            return
         }
         if (signInStatusEnum === 3) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts2.png"),
               text: `您当前${format(startClassTime, "hh:ii")}-${format(endClassTime, "hh:ii")}的课程暂未签到，为避免考勤异常，请到APP进行签到。`,
               btnShow: [true]
            })
            return
         }
         if (!isCallNames) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts1.png"),
               text: `您当前${format(startClassTime, "hh:ii")}-${format(endClassTime, "hh:ii")}的课程暂未点名，请到APP进行点名。`,
               btnShow: [true]
            })
            return
         }
         if (!coursewareDetailId) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts3.png"),
               text: `该课程未配置课件，请配置课件。`,
               btnShow: [true]
            })
            return
         }

         if (!coursewareEnable) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts4.png"),
               text: `该资源已失效`,
               btnShow: [true]
            })
            return
         }
         handlePaly(coursewareDetailId, id)
      }
   })
}
// 管乐迷是否为线上课
export function isONLINE_gym(teachMode: string) {
   if (teachMode === "ONLINE") {
      useDialogConfirm({
         headImg: require("@/img/curriculum/ts4.png"),
         text: `线上课请在管乐迷老师端完成教学。`,
         btnShow: [true]
      })
      return true
   }
   return false
}
/* 管乐团 开始上课 */
export function handleStartClass_gyt(id: string) {
   httpAjaxLoadingErrMsg(getCourseScheduleDetail_gyt, id).then(res => {
      if (res.code === 200) {
         const { signIn, rollCall, lessonCoursewareDetailId, startTime, endTime, status, needSignIn, applyStatus } = res.data
         if (status === "COMPLETE") {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts4.png"),
               text: `该课程已结束`,
               btnShow: [true]
            })
            return
         }
         if (!signIn && needSignIn) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts2.png"),
               text: `您当前${format(startTime, "hh:ii")}-${format(endTime, "hh:ii")}的课程暂未签到，为避免考勤异常，请到APP进行签到。`,
               btnShow: [true]
            })
            return
         }
         if (!rollCall) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts1.png"),
               text: `您当前${format(startTime, "hh:ii")}-${format(endTime, "hh:ii")}的课程暂未点名，请到APP进行点名。`,
               btnShow: [true]
            })
            return
         }
         if (!lessonCoursewareDetailId) {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts3.png"),
               text: `该课程未配置课件，请配置课件。`,
               btnShow: [true]
            })
            return
         }

         if (!applyStatus && status !== "ING") {
            useDialogConfirm({
               headImg: require("@/img/curriculum/ts4.png"),
               text: `该资源已失效`,
               btnShow: [true]
            })
            return
         }

         handlePaly(lessonCoursewareDetailId, id)
      }
   })
}

function handlePaly(coursewareDetailId: string, coursewareId: string) {
   // const url = router.resolve({
   //    name: "coursewarePlay",
   //    params: { id: coursewareDetailId },
   //    query: {
   //       modeId: coursewareId
   //    }
   // }).href
   // window.open(url, "_blank")
   router.push({
      name: "coursewarePlay",
      params: {
         id: coursewareDetailId
      },
      query: {
         modeId: coursewareId
      }
   })
}
