<template>
  <div class="course-management">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>调卷规则</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form ref="form" :model="form" :rules="rules" inline>
          <el-form-item label="批次名称" prop="examId">
            <ExamSelect v-model="form.examId" />
          </el-form-item>
          <el-form-item label="科目名称">
            <CourseSelect :examId="form.examId" v-model="form.code" />
          </el-form-item>
          <el-form-item label="状态">
            <StateSelect
              :options="[
                { code: 0, name: '未绑卷' },
                { code: 1, name: '已绑卷' },
              ]"
              v-model="form.hasPaper"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleCurrentChange(0)"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-upload" @click="importPaper"
            >导入试卷</el-button
          >
        </div>
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column type="selection" width="42" />
      <el-table-column width="100" 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.courseCode }}</span>
      </el-table-column>
      <el-table-column label="科目名称">
        <span slot-scope="scope">{{ scope.row.courseName }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.hasPaper === 1 ? "已绑卷" : "未绑卷"
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="音频题">
        <span slot-scope="scope">{{
          scope.row.hasAudio === 1 ? "是" : "否"
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="提交答案">
        <span slot-scope="scope">{{
          scope.row.hasAnswer | zeroOneYesNoFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column sortable width="170" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210" fixed="right">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @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>

    <PaperImportDialog
      ref="theDialog"
      :examId="form.examId"
      @reload="searchForm"
    />

    <CoursePaperDialog
      ref="theDialog2"
      :course="selectedCourse"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import { searchCourses } from "@/api/examwork-course";
import PaperImportDialog from "./PaperImportDialog";
import CoursePaperDialog from "./CoursePaperDialog";

export default {
  name: "CourseManagement",
  components: {
    PaperImportDialog,
    CoursePaperDialog,
  },
  computed: {},
  data() {
    return {
      form: {
        examId: "",
        code: "",
        hasPaper: null,
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedCourse: {},
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      const res = await searchCourses({
        examId: this.form.examId,
        code: this.form.code,
        hasPaper: this.form.hasPaper,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    async importPaper() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      this.$refs.theDialog.openDialog();
    },
    edit(course) {
      this.selectedCourse = course;
      this.$refs.theDialog2.openDialog();
    },
  },
};
</script>

<style></style>
