<template>
  <el-card>
    <el-form
      :model="searchForm"
      label-width="130px"
      @submit.prevent="fnResetPagination"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="课程名称" prop="KEYWORDS">
            <el-input
              v-model="searchForm.KEYWORDS"
              placeholder="请输入课程名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="教师名称" prop="SPEAKER">
            <el-input
              v-model="searchForm.SPEAKER"
              placeholder="请输入教师名称"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="data.searchFromUnfolded" :span="6">
          <el-form-item label="培训板块类型" prop="trainingSectionKey">
            <layout-learning-train-type
              v-model="searchForm.trainingSectionKey"
              type="post"
              placeholder="请选择培训板块类型"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="data.searchFromUnfolded" :span="6">
          <el-form-item label="培训行业类型" prop="TRAINTYPE">
            <layout-learning-train-type
              v-model="searchForm.TRAINTYPE"
              type="post"
              placeholder="请选择培训行业类型"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="data.searchFromUnfolded" :span="6">
          <el-form-item label="岗位培训类型" prop="POSTTYPE">
            <layout-learning-train-type
              v-model="searchForm.POSTTYPE"
              type="post"
              placeholder="请选择岗位培训类型"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="data.searchFromUnfolded" :span="6">
          <el-form-item label="课件状态" prop="STATE">
            <el-select v-model="searchForm.STATE" placeholder="请选择状态">
              <el-option
                v-for="item in data.courseStates"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="10px">
            <el-button type="primary" native-type="submit">搜索</el-button>
            <el-button native-type="reset" @click="fnResetPagination">
              重置
            </el-button>
            <el-button
              v-if="!data.searchFromUnfolded"
              type="text"
              @click="data.searchFromUnfolded = true"
            >
              展开<el-icon><arrow-down /></el-icon>
            </el-button>
            <el-button
              v-else
              type="text"
              @click="data.searchFromUnfolded = false"
            >
              合并<el-icon><arrow-up /></el-icon>
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <layout-card>
    <layout-table
      :data="list"
      @get-data="fnGetData"
      v-model:pagination="pagination"
    >
      <el-table-column label="序号" width="50">
        <template #default="{ $index }">
          {{ serialNumber(pagination, $index) }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="COURSEWARENAME"
        label="课件名称"
      />
      <el-table-column
        show-overflow-tooltip
        prop="post_type_name"
        label="岗位培训类型"
      />
      <el-table-column
        show-overflow-tooltip
        prop="train_type_name"
        label="培训行业类型"
      />
      <el-table-column prop="SPEAKER" label="讲师名称" />
      <el-table-column prop="CLASSHOUR" label="课件学时（分钟）" />
      <el-table-column
        show-overflow-tooltip
        prop="trainingSectionName"
        label="培训板块"
      />
      <el-table-column prop="CREATTIME" label="上传时间" />
      <el-table-column prop="STATE" label="课件状态">
        <template v-slot="{ row }">
          <template v-if="row.STATE === 0">
            <div>启用</div>
          </template>
          <template v-else-if="row.STATE === 1">
            <div>禁用</div>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="CURRICULUM_ID_REMOTE" label="课件来源">
        <template v-slot="{ row }">
          <template v-if="row.CURRICULUM_ID_REMOTE">
            <div>商城</div>
          </template>
          <template v-else>
            <div>自传</div>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="NUMEXAMPLE" label="习题数" />
      <el-table-column label="操作" width="300">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            text
            link
            v-if="
              (row.VIDEOCOURSEWARE_ID_REMOTE &&
                row.VIDEOCOURSEWARE_ID_REMOTE.length > 0) ||
              (row.VIDEOFILES && row.VIDEOFILES.length > 0)
            "
            @click="fnPreviewVideo(row)"
          >
            预览
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="buttonJurisdiction.edit && row.STATE === 1"
            @click="fnEditState(row.VIDEOCOURSEWARE_ID, 0)"
          >
            启用
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="buttonJurisdiction.edit && row.STATE === 0"
            @click="fnEditState(row.VIDEOCOURSEWARE_ID, 1)"
          >
            禁用
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="
              buttonJurisdiction.edit &&
              row.CURRICULUMCOUNT === 0 &&
              row.STAGECOUNT === 0 &&
              row.ISPLATFORM === 0
            "
          >
            修改<!-- todo -->
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="
              buttonJurisdiction.del &&
              row.CURRICULUMCOUNT === 0 &&
              row.STAGECOUNT === 0 &&
              row.ISPLATFORM === 0
            "
            @click="fnDelete(row.VIDEOCOURSEWARE_ID)"
          >
            删除
          </el-button>
          <el-button
            type="primary"
            text
            link
            @click="
              router.push({
                path: '/online_learn_exam/courseware/video/question_list',
                query: {
                  COURSE_ID: row.VIDEOCOURSEWARE_ID, // 课件ID
                  COURSE_TYPE: '1', // 课件类型(1:视频课件、2:资料课件)
                },
              })
            "
          >
            课件习题
          </el-button>
        </template>
      </el-table-column>
      <template #button>
        <el-button v-if="buttonJurisdiction.add" type="primary">
          新增<!-- todo -->
        </el-button>
      </template>
    </layout-table>
  </layout-card>
  <layout-video
    :src="data.videoDialog.src"
    :vid="data.videoDialog.vid"
    :play-auth="data.videoDialog.playAuth"
    v-model:visible="data.videoDialog.visible"
  />
</template>

<script setup>
import LayoutCard from "@/components/card/index.vue";
import LayoutLearningTrainType from "@/components/learning_train_type/index.vue";
import {
  getVideoCoursewareList,
  editVideoCoursewareState,
  deleteVideoCourseware,
} from "@/request/online_learn_exam.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { serialNumber } from "@/assets/js/utils.js";
import LayoutTable from "@/components/table/index.vue";
import LayoutVideo from "@/components/video/index.vue";
import { getVideoAuth, getVideoSource } from "@/request/api.js";
import { ArrowDown, ArrowUp } from "@element-plus/icons-vue";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { debounce } from "throttle-debounce";

const router = useRouter();
const buttonJurisdiction = await useButtonJurisdiction("courseware");

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getVideoCoursewareList);

const data = reactive({
  courseStates: [
    { value: "0", label: "启用" },
    { value: "1", label: "禁用" },
  ],
  searchFromUnfolded: false,
  videoDialog: {
    src: "",
    vid: "",
    playAuth: "",
    visible: false,
  },
});

const fnPreviewVideo = async (row) => {
  data.videoDialog.src = "";
  data.videoDialog.vid = "";
  data.videoDialog.playAuth = "";
  if (
    row.VIDEOCOURSEWARE_ID_REMOTE &&
    row.VIDEOCOURSEWARE_ID_REMOTE.length > 0
  ) {
    const respData = await getVideoSource({
      VIDEOCOURSEWARE_ID: row.VIDEOCOURSEWARE_ID_REMOTE,
      CURRICULUM_ID: row.CURRICULUM_ID_REMOTE,
    });
    if (respData && respData.type === "success") {
      const video = {};
      respData.videoList.forEach((item) => {
        video[item.definition] = item.playURL;
      });
      data.videoDialog.src = JSON.stringify(video);
      data.videoDialog.visible = true;
    }
  } else if (row.VIDEOFILES && row.VIDEOFILES.length > 0) {
    const respData = await getVideoAuth({
      videoId: row.VIDEOFILES,
    });
    if (respData && respData.type === "success") {
      data.videoDialog.playAuth = respData.playAuth;
      data.videoDialog.vid = row.VIDEOFILES;
      data.videoDialog.visible = true;
    }
  }
};

const fnEditState = debounce(
  1000,
  async (VIDEOCOURSEWARE_ID, STATE) => {
    const respData = await editVideoCoursewareState({
      VIDEOCOURSEWARE_ID,
      STATE,
    });
    if (respData && respData.result === "success") {
      ElMessage.success("设置成功");
      await fnGetData();
    }
  },
  { atBegin: true }
);

const fnDelete = debounce(
  1000,
  async (VIDEOCOURSEWARE_ID) => {
    if (VIDEOCOURSEWARE_ID) {
      await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
      const respData = await deleteVideoCourseware({ VIDEOCOURSEWARE_ID });
      if (respData && respData.result === "success") {
        ElMessage.success("删除成功");
        await fnGetData();
      }
    }
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
