<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="批次名称">
        <el-input v-model.trim="form.name"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <ExamTypeSelect v-model="form.mode"></ExamTypeSelect>
      </el-form-item>
      <el-form-item label="状态">
        <StateSelect v-model="form.enableState"></StateSelect>
      </el-form-item>
      <el-button @click="searchForm">查询</el-button>
      <el-button @click="add">新增</el-button>
    </el-form>

    <el-table :data="tableData" stripe style="width: 100%;">
      <el-table-column type="selection" width="40" />
      <el-table-column width="55" label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column width="200" label="批次编码">
        <span slot-scope="scope">{{ scope.row.code }}</span>
      </el-table-column>
      <el-table-column width="200" label="批次名称">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column label="模式">
        <span slot-scope="scope">{{ scope.row.mode }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="开始时间">
        <span slot-scope="scope">{{
          scope.row.startTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="结束时间">
        <span slot-scope="scope">{{ scope.row.endTime | datetimeFilter }}</span>
      </el-table-column>
      <el-table-column width="100" label="算分进度">
        <span slot-scope="scope">{{
          scope.row.scoreStatus | scoreStatusFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column width="100" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210">
        <div slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableExam(scope.row)"
          >
            {{ scope.row.enable ? "禁用" : "启用" }}
          </el-button>
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </el-button>
          <el-button size="mini" type="primary" plain>
            重新算分
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="page float-right">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>
  </div>
</template>

<script>
import StateSelect from "@/components/StateSelect";
import ExamTypeSelect from "@/components/ExamTypeSelect";
import { searchExams, toggleEnableExam } from "@/api/exam";

export default {
  name: "ExamManagement",
  components: {
    StateSelect,
    ExamTypeSelect,
  },
  data() {
    return {
      form: {
        orgId: "",
        roleCode: "",
        loginName: "",
        name: "",
        enableState: null,
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      const res = await searchExams({
        enable: this.form.enableState,
        name: this.form.name,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records.records;
      this.total = res.data.data.records.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      this.$router.push("/exam/edit/");
    },
    edit(exam) {
      this.$router.push("/exam/edit/" + exam.id);
    },
    async toggleEnableExam(user) {
      await toggleEnableExam({
        id: user.id,
        enable: user.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
  },
};
</script>

<style></style>
