<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="80px"
        @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="POSTTYPE"
              label-width="120px"
            >
              <layout-learning-train-type
                v-model="searchForm.POSTTYPE"
                type="post"
              />
            </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="CURRICULUMNAME"
          label="课程名称"
          show-overflow-tooltip
        />
        <el-table-column show-overflow-tooltip label="培训行业类型">
          <template v-slot="{ row }">
            <span v-if="row.CURRENCY === '1'">通用</span>
            <span v-else>{{ row.train_type_name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="post_type_name"
          label="岗位培训类型"
          show-overflow-tooltip
        />
        <el-table-column prop="CLASSHOUR" label="学时（分钟）" width="100" />
        <el-table-column prop="videoCount" label="视频课件数" width="100" />
        <el-table-column prop="dataCount" label="资料课件数" width="100" />
        <el-table-column label="操作" width="150">
          <template v-slot="{ row }">
            <el-button
              v-if="!row.COURSEPAPERS_ID"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/online_learn_exam/curriculum/view',
                  query: { CURRICULUM_ID: row.CURRICULUM_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit && !row.COURSEPAPERS_ID"
              type="primary"
              text
              link
            >
              编辑
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del && !row.COURSEPAPERS_ID"
              type="primary"
              text
              link
              @click="fnDelete(row.CURRICULUM_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button v-if="buttonJurisdiction.add" type="primary">
            新建课程
          </el-button>
          <el-button v-if="buttonJurisdiction.add" type="primary">
            继承
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

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

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getCurriculumList);
const buttonJurisdiction = await useButtonJurisdiction("curriculum");
const fnDelete = debounce(
  1000,
  async (CURRICULUM_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setCurriculumDelete({ CURRICULUM_ID });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
</script>

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