<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入考试名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="1公开2部门3定员" prop="openType">-->
<!--        <el-select v-model="queryParams.openType" placeholder="请选择1公开2部门3定员" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examList" :height="clientHeight" border>
      <el-table-column
        label="考试名称"
        prop="title"
        show-overflow-tooltip
      />

<!--      <el-table-column
        label="考试类型"
        align="center"
      >
        <template v-slot="scope">
          {{ scope.row.openType | examOpenType }}
        </template>

      </el-table-column>-->

      <el-table-column
        label="考试时间"
        width="220px"
        align="center"
      >

        <template v-slot="scope">
            <span v-if="scope.row.timeLimit">
              {{ scope.row.startTime }} ~ {{ scope.row.endTime }}
            </span>
          <span v-else>不限时</span>
        </template>

      </el-table-column>

      <el-table-column
        label="考试时长"
        align="center"
      >

        <template v-slot="scope">
          {{ scope.row.totalTime }}分钟
        </template>

      </el-table-column>

      <el-table-column
        label="考试总分"
        prop="totalScore"
        align="center"
      />

      <el-table-column
        label="及格线"
        prop="qualifyScore"
        align="center"
      />

      <el-table-column
        label="操作"
        align="center"
      >
        <template v-slot="scope">
          <el-button v-if="scope.row.state===0" icon="el-icon-caret-right" type="primary" size="mini" @click="handlePre(scope.row.examId)">去考试</el-button>
          <el-button v-if="scope.row.state===1" icon="el-icon-s-release" size="mini" disabled>已禁用</el-button>
          <el-button v-if="scope.row.state===2" icon="el-icon-s-fold" size="mini" disabled>待开始</el-button>
          <el-button v-if="scope.row.state===3" icon="el-icon-s-unfold" size="mini" disabled>已结束</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 { listExam, getExam, delExam, addExam, updateExam, exportExam, importTemplate} from "@/api/training/elearn/exam";
import { treeselect } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import RepoSelect from '@/components/RepoSelect'

export default {
  name: "ExamList",
  components: { Treeselect,RepoSelect },
  // components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试表格数据
      examList: [],
      // 已选择的题库
      excludes: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      clientHeight:300,
      // 是否显示弹出层
      open: false,
        // 用户导入参数
        upload: {
            // 是否显示弹出层（用户导入）
            open: false,
            // 弹出层标题（用户导入）
            title: "",
            // 是否禁用上传
            isUploading: false,
            // 是否更新已经存在的用户数据
            updateSupport: 0,
            // 设置上传的请求头部
            headers: { Authorization: "Bearer " + getToken() },
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + "/elearn/exam/importData"
        },
      // 查询参数
      queryParams: {
        examId: null,
        pageNum: 1,
        pageSize: 20,
        title: null,
        content: null,
        openType: null,
        state: null,
        timeLimit: null,
        examStartTime: null,
        examEndTime: null,
        totalScore: null,
        totalTime: null,
        qualifyScore: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: null,
        remarks: null
      },
      // 题库
      repoList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch: {

    filterText(val) {
      this.$refs.tree.filter(val)
    },

    dateValues: {

      handler() {
        this.form.examStartTime = this.dateValues[0]
        this.form.examEndTime = this.dateValues[1]
      }
    },

    // 题库变换
    repoList: {

      handler(val) {
        let totalScore = 0
        this.excludes = []
        for (let i = 0; i<val.length; i++) {
          const item = val[i]
          if (item.radioCount > 0 && item.radioScore>0) {
            totalScore += item.radioCount * item.radioScore
          }

          if (item.multiCount>0 && item.multiScore>0) {
            totalScore += item.multiCount * item.multiScore
          }

          if (item.judgeCount>0 && item.judgeScore>0) {
            totalScore += item.judgeCount * item.judgeScore
          }
          this.excludes.push(item.id)
        }

        // 赋值
        this.form.totalScore = totalScore
        this.form.repoList = val
        this.$forceUpdate()
      },
      deep: true
    }

  },
  created() {
    this.queryParams.examId = this.$route.params.examId
      //设置表格高度对应屏幕高度
      this.$nextTick(() => {
          this.clientHeight = document.body.clientHeight -250
      })
    this.getList();
  },
  methods: {
    /** 查询考试列表 */
    getList() {
      this.loading = true;
      listExam(this.queryParams).then(response => {
        this.examList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        examId: null,
        title: null,
        content: null,
        openType: null,
        state: null,
        timeLimit: null,
        examStartTime: null,
        examEndTime: null,
        totalScore: null,
        totalTime: null,
        qualifyScore: null,
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: null,
        remarks: null,
        repoList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 开始考试
    handlePre(examId) {
      this.$router.push({ name: 'PreExam', params: { examId: examId }})
    },
  }
};
</script>
