<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <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="dates">
              <el-date-picker
                v-model="searchForm.dates"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="培训类型" prop="TRAINTYPE">
              <layout-learning-train-type
                v-model="searchForm.TRAINTYPE"
                type="trainingType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="行业类型" prop="INDUSTRY_END_ID">
              <layout-learning-train-type
                v-model="searchForm.INDUSTRY_END_ID"
                type="industryType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="岗位类型" prop="POSTTYPE">
              <layout-learning-train-type
                v-model="searchForm.POSTTYPE"
                type="postType"
                :search-value="searchForm.TRAINTYPE"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="培训级别" prop="TRAINLEVEL">
              <layout-learning-train-type
                v-model="searchForm.TRAINLEVEL"
                type="trainingLevel"
                :search-value="searchForm.TRAINTYPE"
              />
            </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="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          label="课程名称"
          prop="CURRICULUMNAME"
          show-overflow-tooltip
        />
        <el-table-column label="上传时间" prop="CREATTIME" width="150" />
        <el-table-column label="培训类型" prop="TRAININGTYPE_NAME" />
        <el-table-column label="行业类型">
          <template #default="{ row }">
            {{ row.INDUSTRY_END_NAME || row.INDUSTRY_ALL_NAME }}
          </template>
        </el-table-column>
        <el-table-column label="岗位类型" prop="POSTTYPE_NAME" />
        <el-table-column label="课件数" prop="COURSEWARECOUNT" width="100" />
        <el-table-column label="课件时长" width="100">
          <template #default="{ row }">
            {{ secondConversion(row.VIDEOTIME) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/training_resource_management/course_management/view',
                  query: { CURRICULUM_ID: row.CURRICULUM_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="row.IS_CORP_CREATE === 1"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/training_resource_management/course_management/add',
                  query: { CURRICULUM_ID: row.CURRICULUM_ID },
                })
              "
            >
              编辑
            </el-button>
            <el-button
              v-if="row.IS_CORP_CREATE === 1"
              type="primary"
              text
              link
              @click="fnDelete(row.CURRICULUM_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="
              router.push({
                path: '/training_resource_management/course_management/add',
              })
            "
          >
            新增
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

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

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getCourseManagementList);
const fnGetDataTransfer = () => {
  fnGetData({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
  });
};
const fnDelete = debounce(
  1000,
  async (CURRICULUM_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", {
      type: "warning",
    });
    await setCourseManagementDelete({ CURRICULUM_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

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