<template>
  <el-card>
    <el-form
      :model="searchForm"
      label-width="110px"
      @submit.prevent="fnResetPagination"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="课程名称" prop="KEYWORDS">
            <el-input v-model="searchForm.KEYWORDS" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="教师名称" prop="SPEAKER">
            <el-input v-model="searchForm.SPEAKER" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="培训板块类型" prop="trainingSectionKey">
            <layout-learning-train-type
              v-model="searchForm.trainingSectionKey"
              type="plate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="培训行业类型" prop="TRAINTYPE">
            <layout-learning-train-type
              v-model="searchForm.TRAINTYPE"
              type="industry"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="岗位培训类型" prop="POSTTYPE">
            <layout-learning-train-type
              v-model="searchForm.POSTTYPE"
              type="post"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="课件状态" prop="STATE">
            <el-select v-model="searchForm.STATE">
              <el-option
                v-for="item in stateList"
                :key="item.ID"
                :label="item.NAME"
                :value="item.ID"
              />
            </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-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="课件学时（分钟）" width="120" />
      <el-table-column
        show-overflow-tooltip
        prop="trainingSectionName"
        label="培训板块"
      />
      <el-table-column prop="CREATTIME" label="上传时间" width="150" />
      <el-table-column prop="STATE" label="课件状态" width="80">
        <template v-slot="{ row }">
          {{ translationStatus(row.STATE, stateList) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            text
            link
            @click="fnPreviewPdf(row.COURSEWAREFILES)"
          >
            预览
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="buttonJurisdiction.edit && row.STATE === 1"
            @click="fnEditState(row.DATACOURSEWARE_ID, 0)"
          >
            启用
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="buttonJurisdiction.edit && row.STATE === 0"
            @click="fnEditState(row.DATACOURSEWARE_ID, 1)"
          >
            禁用
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="
              buttonJurisdiction.edit &&
              row.CURRICULUMCOUNT === 0 &&
              row.STAGECOUNT === 0 &&
              row.ISPLATFORM === '0'
            "
            @click="
              router.push({
                path: '/online_learn_exam/courseware/data/edit',
                query: {
                  DATACOURSEWARE_ID: row.DATACOURSEWARE_ID,
                },
              })
            "
          >
            修改
          </el-button>
          <el-button
            type="primary"
            text
            link
            v-if="
              buttonJurisdiction.del &&
              row.CURRICULUMCOUNT === 0 &&
              row.STAGECOUNT === 0 &&
              row.ISPLATFORM === '0'
            "
            @click="fnDelete(row.DATACOURSEWARE_ID)"
          >
            删除
          </el-button>
          <el-button
            type="primary"
            text
            link
            @click="
              router.push({
                path: '/online_learn_exam/courseware/data/exercises',
                query: {
                  COURSEWAREID: row.DATACOURSEWARE_ID,
                  COURSEWARETYPE: '2',
                },
              })
            "
          >
            课件习题
          </el-button>
        </template>
      </el-table-column>
      <template #button>
        <el-button
          v-if="buttonJurisdiction.add"
          type="primary"
          @click="
            router.push({
              path: '/online_learn_exam/courseware/data/add',
            })
          "
        >
          新增
        </el-button>
      </template>
    </layout-table>
  </layout-card>
  <layout-pdf
    :src="data.pdfDialog.src"
    v-model:visible="data.pdfDialog.visible"
  />
</template>

<script setup>
import LayoutLearningTrainType from "@/components/learning_train_type/index.vue";
import {
  getDataCoursewareList,
  setDataCoursewareDelete,
  setDataCoursewareState,
} from "@/request/online_learn_exam.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { serialNumber, translationStatus } from "@/assets/js/utils.js";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import LayoutPdf from "@/components/pdf/index.vue";
import { debounce } from "throttle-debounce";

const router = useRouter();
const stateList = [
  { ID: 0, NAME: "启用" },
  { ID: 1, NAME: "禁用" },
];
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getDataCoursewareList);
const buttonJurisdiction = await useButtonJurisdiction("courseware");
const data = reactive({
  pdfDialog: {
    src: "",
    visible: false,
  },
});
const fnPreviewPdf = (FILEPATH) => {
  data.pdfDialog.visible = true;
  data.pdfDialog.src = FILEPATH;
};
const fnEditState = debounce(
  1000,
  async (DATACOURSEWARE_ID, STATE) => {
    const message = STATE === 1 ? "禁用" : "启用";
    await ElMessageBox.confirm(`确定要${message}吗？`, { type: "warning" });
    await setDataCoursewareState({
      DATACOURSEWARE_ID,
      STATE,
    });
    ElMessage.success(`${message}成功`);
    fnResetPagination();
  },
  { atBegin: true }
);
const fnDelete = debounce(
  1000,
  async (DATACOURSEWARE_ID) => {
    if (DATACOURSEWARE_ID) {
      await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
      await setDataCoursewareDelete({ DATACOURSEWARE_ID });
      ElMessage.success("删除成功");
      fnResetPagination();
    }
  },
  { atBegin: true }
);
</script>

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