<template>
  <div>
    <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="POSTTYPE">
              <layout-learning-train-type
                v-model="searchForm.POSTTYPE"
                type="post"
                placeholder="请选择岗位培训类型"
              />
            </el-form-item>
          </el-col>
          <el-col :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-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="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="COURSEWARENAME"
          label="课件名称"
          show-overflow-tooltip
        />
        <el-table-column
          prop="post_type_name"
          label="岗位培训类型"
          show-overflow-tooltip
        />
        <el-table-column
          show-overflow-tooltip
          prop="train_type_name"
          label="培训行业类型"
        >
          <template v-slot="{ row }">
            <div v-if="row.CURRENCY === '1'">通用</div>
            <div v-else>{{ row.train_type_name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="teacherName" label="讲师名称" />
        <el-table-column prop="CLASSHOUR" 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 label="操作" width="200">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              v-if="interceptTheSuffix(row.COURSEWAREFILES, '.pdf')"
              @click="fnPreviewPdf(row.COURSEWAREFILES)"
            >
              预览
            </el-button>
            <el-button
              type="primary"
              text
              link
              :disabled="row.recordID !== '--'"
              @click="fnAdd(row.DATACOURSEWARE_ID)"
            >
              添加
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/platform_resource_library/courseware/data/view',
                  query: {
                    COURSE_ID: row.DATACOURSEWARE_ID, // 课件ID
                    COURSE_TYPE: '2', // 课件类型(1:视频课件、2:资料课件)
                  },
                })
              "
            >
              课件习题
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <layout-pdf
      :src="data.pdfDialog.src"
      v-model:visible="data.pdfDialog.visible"
    />
    <layout-txt
      :src="data.txtDialog.src"
      v-model:visible="data.txtDialog.visible"
    />
  </div>
</template>

<script setup>
import LayoutCard from "@/components/card/index.vue";
import LayoutLearningTrainType from "@/components/learning_train_type/index.vue";
import {
  getDataCoursewareList,
  joinMyDataCourseware,
} from "@/request/platform_resource_library.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { interceptTheSuffix, serialNumber } from "@/assets/js/utils.js";
import LayoutTable from "@/components/table/index.vue";
// import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import LayoutPdf from "@/components/pdf/index.vue";
import LayoutTxt from "@/components/txt/index.vue";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getDataCoursewareList);

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

// const buttonJurisdiction = await useButtonJurisdiction("courseware"); 原项目没找到在哪用

const fnAdd = debounce(1000, async (DATACOURSEWARE_ID) => {
  await ElMessageBox.confirm("确定要添加吗？", { type: "warning" });
  await joinMyDataCourseware({ COURSEWARE_ID: DATACOURSEWARE_ID });
  ElMessage.success("添加成功");
  fnResetPagination();
});

const fnPreviewPdf = (FILEPATH) => {
  data.pdfDialog.visible = true;
  data.pdfDialog.src = FILEPATH;
};
</script>

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