<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="5">
            <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="5">
            <el-form-item label="试卷类型" prop="SOURCETYPE">
              <el-select v-model="searchForm.SOURCETYPE">
                <el-option
                  v-for="item in sourceTypeList"
                  :key="item.ID"
                  :label="item.NAME"
                  :value="item.ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="合格分数" prop="PASSSCORE">
              <el-input v-model="searchForm.PASSSCORE" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <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="EXAMNAME"
          show-overflow-tooltip
        />
        <el-table-column label="上传时间" prop="CREATTIME" width="150" />
        <el-table-column label="满分" prop="EXAMSCORE" width="150" />
        <el-table-column label="合格分数" prop="PASSSCORE" width="150" />
        <el-table-column label="试卷类型" width="150">
          <template #default="{ row }">
            {{ translationStatus(row.SOURCETYPE, sourceTypeList) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/training_process_management/exam_paper_management/view',
                  query: { STAGEEXAMPAPERINPUT_ID: row.STAGEEXAMPAPERINPUT_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="row.SOURCETYPE === '1' || row.PAPERUSERCOUNT > 0"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/training_process_management/exam_paper_management/edit',
                  query: {
                    STAGEEXAMPAPERINPUT_ID: row.STAGEEXAMPAPERINPUT_ID,
                    type: 'edit',
                  },
                })
              "
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/training_process_management/exam_paper_management/inherit',
                  query: {
                    STAGEEXAMPAPERINPUT_ID: row.STAGEEXAMPAPERINPUT_ID,
                    type: 'inherit',
                  },
                })
              "
            >
              继承
            </el-button>
            <el-button
              v-show="row.SOURCETYPE === '1' || row.PAPERUSERCOUNT > 0"
              type="primary"
              text
              link
              @click="fnDelete(row.STAGEEXAMPAPERINPUT_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="
              router.push({
                path: '/training_process_management/exam_paper_management/add',
                query: { type: 'add' },
              })
            "
          >
            新建试卷
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import useListData from "@/assets/js/useListData.js";
import { getExamPaperManagementList } from "@/request/training_resource_management.js";
import { serialNumber, translationStatus } from "@/assets/js/utils.js";
import { useRouter } from "vue-router";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { setExamPaperManagementDelete } from "@/request/training_process_management.js";

const sourceTypeList = [
  { ID: "1", NAME: "平台试卷" },
  { ID: "2", NAME: "自建试卷" },
];
const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getExamPaperManagementList);
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 (STAGEEXAMPAPERINPUT_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", {
      type: "warning",
    });
    await setExamPaperManagementDelete({ STAGEEXAMPAPERINPUT_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

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