<template>
  <el-dialog
    ref="dialog"
    title="编辑绑卷"
    width="800px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
      inline
    >
      <el-row>
        <el-form-item label="批次名称">
          <ExamSelect v-model="course.examId" disabled />
        </el-form-item>
        <el-form-item label="科目名称">
          <CourseSelect
            :examId="course.examId"
            v-model="course.courseCode"
            disabled
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-table :data="papers" stripe style="width: 100%;">
          <el-table-column width="42" />
          <el-table-column width="100" label="ID">
            <span slot-scope="scope">{{ scope.row.id }}</span>
          </el-table-column>
          <el-table-column label="试卷名称">
            <span slot-scope="scope">{{ scope.row.name }}</span>
          </el-table-column>
          <el-table-column width="100" label="分值">
            <span slot-scope="scope">{{ scope.row.totalScore }}</span>
          </el-table-column>
          <el-table-column width="170" label="抽卷几率">
            <span slot-scope="scope">
              <el-input-number
                v-model.trim="scope.row.weight"
                :min="0"
                :step="1"
                step-strictly
                :max="100"
                style="width: 50px;"
                :controls="false"
              >
              </el-input-number>
              %
            </span>
          </el-table-column>
          <el-table-column width="170" label="音频播放次数">
            <span slot-scope="scope">
              <el-input-number
                :min="1"
                :max="1000"
                v-model.trim="scope.row.audioPlayCount"
              ></el-input-number>
            </span>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-form-item label="客观题小题乱序" prop="objectiveShuffle">
          <el-radio-group
            class="pull_right_sm"
            v-model="refreshCourse.objectiveShuffle"
          >
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="客观题选项乱序" prop="optionShuffle">
          <el-radio-group
            class="pull_right_sm"
            v-model="refreshCourse.optionShuffle"
          >
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm" :loading="loading">
          保 存
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  searchCourses,
  searchPapers,
  saveCourse,
  savePapers,
} from "@/api/examwork-course";

export default {
  name: "CoursePaperDialog",
  props: {
    course: Object,
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {},
      refreshCourse: {},
      papers: [],
      loading: false,
    };
  },
  watch: {
    course: {
      immediate: true,
      handler() {
        this.refreshCourse = {};
        this.papers = [];
        this.initData();
      },
    },
  },
  methods: {
    async initData() {
      if (!this.course?.examId) return;
      const courseRes = await searchCourses({
        id: this.course.id,
        examId: this.course.examId,
        pageNumber: 1,
        pageSize: 1,
      });
      this.refreshCourse = courseRes?.data.data.records[0];

      const res = await searchPapers({
        examId: this.refreshCourse.examId,
        courseCode: this.refreshCourse.courseCode,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.papers = res?.data.data;
    },
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      try {
        const totalWieght = this.papers
          .map((v) => v.weight)
          .reduce((p, c) => p + c);
        if (totalWieght !== 100) {
          this.$notify({
            type: "warning",
            title: `抽卷几率之和必须等于100，当前之和为${totalWieght}`,
          });
          return;
        }
        this.loading = true;
        await saveCourse({
          examId: this.refreshCourse.examId,
          courseCode: this.refreshCourse.courseCode,
          objectiveShuffle: this.refreshCourse.objectiveShuffle,
          optionShuffle: this.refreshCourse.optionShuffle,
        });
        const ps = [];
        for (const paper of this.papers) {
          ps.push({
            id: paper.id,
            weight: paper.weight,
            audioPlayCount: paper.audioPlayCount,
          });
        }
        await savePapers(ps);
        this.$emit("reload");
        this.$notify({ title: "保存成功", type: "success" });
        this.closeDialog();
      } catch (error) {
        console.log(error);
        this.initData();
        this.$notify({ title: "保存失败", type: "warning" });
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
