<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" inline v-show="showSearch">
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择考试状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="考试开始时间" prop="timeRange" label-width="80">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          clearable
          style="width: 220px"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务创建时间" prop="createTime" label-width="80">
        <el-date-picker
          v-model="queryParams.createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          clearable
          style="width: 220px"
        />
      </el-form-item>
      <el-row>
        <el-form-item class="align-right">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleCreate">新建考试</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table :data="tableData" fit highlight-current-row style="width: 100%" @selection-change="selectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="column in tableColumns"
        show-overflow-tooltip
        :key="column.prop"
        :label="column.label"
        :prop="column.prop"
        align="center"
      >
        <!-- 状态插槽 -->
        <template v-if="column.prop === 'examStatus' || column.prop === 'examTarget'" v-slot="scope">
          <el-tag v-if="column.prop === 'examTarget'" :type="getExamTargetType(scope.row.examTarget)" size="mini">
            {{ getExamTargetTypeLabel(scope.row.examTarget) }}
          </el-tag>
          <el-tag v-else-if="column.prop === 'examStatus'" :type="getStatusType(scope.row.examStatus)" size="mini">
            {{ getStatusLabel(scope.row.examStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.examStatus === '0'"
            type="text"
            size="mini"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.row.examStatus === '0'"
            type="text"
            size="mini"
            icon="el-icon-delete"
            style="color: #e47470"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <el-button
            v-if="scope.row.examStatus !== '0'"
            type="text"
            size="mini"
            @click="handleExamData(scope.row)"
            icon="el-icon-s-data"
          >
            数据
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { delExamTask, delTrainingTaskById, getExamList } from '@/api/educationalTraining/resourceAdministration'
import { indexContinuous } from '@/utils/bonus'

export default {
  data() {
    return {
      total: 0,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '',
        // 考试状态
        status: '',
        // 考试开始时间范围
        timeRange: '',
        createTime: '',
        examStatus: '',
        createUserName: '',
      },
      // 考试状态-下拉框
      statusOptions: [
        { label: '未开始', value: '0' },
        { label: '进行中', value: '1' },
        { label: '已完成', value: '2' },
      ],
      // 表头
      tableColumns: [
        { label: '考试名称', prop: 'examName' },
        { label: '试卷名称', prop: 'templateName' },
        { label: '题目数', prop: 'questionNum' },
        { label: '考试对象', prop: 'examTarget' },
        { label: '参与人数', prop: 'num' },
        { label: '考试开始时间', prop: 'startTime' },
        { label: '考试结束时间', prop: 'endTime' },
        { label: '考试时长', prop: 'examLduration' },
        { label: '考试状态', prop: 'examStatus' },
        { label: '创建人', prop: 'createUser' },
      ],
      // 表格数据
      tableData: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    indexContinuous,
    // 查询
    handleQuery() {
      this.getList()
      console.log('🚀 ~ handleQuery ~ handleQuery:', this.queryParams)
    },
    // 重置
    handleReset() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    getExamTargetType(status) {
      switch (status) {
        case '0':
          return 'info'
        case '1':
          return 'success'
        case '2':
          return 'danger'
        default:
          return ''
      }
    },
    getExamTargetTypeLabel(status) {
      switch (status) {
        case '0':
          return '无'
        case '1':
          return '内部人员'
        case '2':
          return '外委人员'
        default:
          return ''
      }
    },
    getStatusType(status) {
      switch (status) {
        case '0':
          return 'info'
        case '1':
          return 'success'
        case '2':
          return 'danger'
        default:
          return ''
      }
    },
    getStatusLabel(status) {
      switch (status) {
        case '0':
          return '未开始'
        case '1':
          return '进行中'
        case '2':
          return '已结束'
        default:
          return ''
      }
    },
    // 列表数据
    getList() {
      console.log('获取列表')
      this.queryParams.startDate = this.queryParams.timeRange[0] || ''
      this.queryParams.endDate = this.queryParams.timeRange[1] || ''
      this.queryParams.examStartTime = this.queryParams.createTime[0] || ''
      this.queryParams.examEndTime = this.queryParams.createTime[1] || ''

      const params = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        keyWord: this.queryParams.keyWord,
        status: this.queryParams.status,
        examStatus: this.queryParams.status,
        startDate: this.queryParams.startDate,
        endDate: this.queryParams.endDate,
        examStartTime: this.queryParams.examStartTime,
        examEndTime: this.queryParams.examEndTime,
      }
      console.log('🚀 ~ getList ~ params:', params)
      getExamList(params).then(res => {
        console.log('🚀 ~ getList ~ res:', res)
        this.tableData = res.rows
        this.total = res.total
      })
    },
    // 新建考试
    handleCreate() {
      console.log('新建考试')
      // examination/newExamination
      this.$router.push({
        path: '/educationalTraining/learningManagement/examination/newExamination',
      })
    },
    // 导出数据
    handleExport() {
      try {
        this.queryParams.startDate = this.queryParams.timeRange[0] || ''
        this.queryParams.endDate = this.queryParams.timeRange[1] || ''
        this.queryParams.examStartTime = this.queryParams.createTime[0] || ''
        this.queryParams.examEndTime = this.queryParams.createTime[1] || ''

        const params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
          keyWord: this.queryParams.keyWord,
          status: this.queryParams.status,
          examStatus: this.queryParams.status,
          startDate: this.queryParams.startDate,
          endDate: this.queryParams.endDate,
          examStartTime: this.queryParams.examStartTime,
          examEndTime: this.queryParams.examEndTime,
        }
        const url = '/exam/examTask/export'
        const fileName = `考试_${new Date().getTime()}.xLsx`
        console.log('🚀 ~ 导出 ~ params:', params)
        this.derive(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    },
    // 选择
    selectionChange(val) {
      console.log('选择', val)
    },
    // 编辑
    handleEdit(row) {
      console.log('编辑', row)
      this.$router.push({
        path: '/educationalTraining/learningManagement/examination/editExamination',
        query: {
          examTaskId: row.examTaskId,
        },
      })
    },
    // 删除
    handleDelete(row) {
      console.log('删除', row)
      this.$modal
        .confirm('<strong>是否确定删除数据？</strong>')
        .then(function () {
          return delExamTask({ examTaskId: row.examTaskId })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    // 数据
    handleExamData(row) {
      console.log('数据', row)
      this.$router.push({
        path: '/educationalTraining/learningManagement/examination/dataAnalysis',
        query: {
          examPaperId: row.examPaperId,
          examTaskId: row.examTaskId,
        },
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.align-right {
  display: flex;
  justify-content: flex-end;
}
</style>
