<template>
  <div class="maketesterauto-wrapper">
    <div class="maketesterauto-form-wrapper">
      <el-form
        ref="addform"
        :model="formInfo"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="试卷名称" prop="name">
          <el-input
            v-model="formInfo.name"
            :style="{ width: '70%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="试卷描述" prop="description">
          <el-input
            type="textarea"
            autosize
            :style="{ width: '70%' }"
            v-model="formInfo.description"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="生成方式">
          <el-radio v-model="formInfo.mode" label="随机"></el-radio>
          <el-radio v-model="formInfo.mode" label="简单"></el-radio>
          <el-radio v-model="formInfo.mode" label="一般"></el-radio>
          <el-radio v-model="formInfo.mode" label="困难"></el-radio>
        </el-form-item>
        <el-form-item label="选择题库">
          <el-select v-model="curChosenLibrary">
            <el-option
              v-for="item in curExamLibraries"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单选题数量">
          <el-input-number v-model="formInfo.singleChoiceNum"></el-input-number>
        </el-form-item>
        <el-form-item label="单选题单项分值">
          <el-input-number
            v-model="formInfo.singleChoiceScore"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="多选题数量">
          <el-input-number
            v-model="formInfo.multipleChoiceNum"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="多选题单项分值">
          <el-input-number
            v-model="formInfo.multipleChoiceScore"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="判断题数量">
          <el-input-number v-model="formInfo.judgmentNum"></el-input-number>
        </el-form-item>
        <el-form-item label="判断题单项分值">
          <el-input-number v-model="formInfo.judgmentScore"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveNext">保存, 下一步</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getExamLibrary, savePaperAutomatically } from "@/api/test.js";
export default {
  data() {
    return {
      formInfo: {
        name: "",
        description: "",
        mode: "随机",
        libraryIds: [],
        singleChoiceNum: 0,
        singleChoiceScore: 0,
        multipleChoiceNum: 0,
        multipleChoiceScore: 0,
        judgmentNum: 0,
        judgmentScore: 0
      },
      curChosenLibrary: -1,
      rules: {
        name: [
          {
            required: true,
            message: "请输入试卷名称",
            trigger: "blur"
          }
        ],
        description: [
          {
            required: true,
            message: "请输入试卷描述",
            trigger: "blur"
          }
        ]
      },
      curExamLibraries: []
    };
  },
  async mounted() {
    await this.initAllExamLibrary();
  },
  methods: {
    async initAllExamLibrary() {
      const params = {
        page: 0,
        size: 999
      };
      await getExamLibrary(params).then(res => {
        console.log(res, "getExamLibrary getExamLibrary");
        this.curExamLibraries = [...res.content];
        this.curExamLibraries.splice(0, 0, {
          name: "全部",
          id: -1
        });
      });
    },
    saveNext() {
      /**
       * singleChoiceNum: 0,
          singleChoiceScore: 0,
          multipleChoiceNum: 0,
          multipleChoiceScore: 0,
          judgmentNum: 0,
          judgmentScore: 0
       */
      if (
        this.formInfo.singleChoiceNum * this.formInfo.singleChoiceScore +
          this.formInfo.multipleChoiceNum * this.formInfo.multipleChoiceScore +
          this.formInfo.judgmentNum * this.formInfo.judgmentScore !==
        100
      ) {
        this.$message.error("试卷总分数不为100");
        return;
      }
      let libraryIds = [];
      if (this.curChosenLibrary === -1) {
        libraryIds = [...this.curExamLibraries.map(item => item.id)];
      } else {
        libraryIds = [this.curChosenLibrary];
      }
      const data = {
        name: this.formInfo.name,
        description: this.formInfo.description,
        mode: this.formInfo.mode,
        libraryIds: libraryIds,
        judgmentNum: this.formInfo.judgmentNum,
        judgmentScore: this.formInfo.judgmentScore,
        multipleChoiceNum: this.formInfo.multipleChoiceNum,
        multipleChoiceScore: this.formInfo.multipleChoiceScore,
        singleChoiceNum: this.formInfo.singleChoiceNum,
        singleChoiceScore: this.formInfo.singleChoiceScore,
        status: "已关闭"
      };
      savePaperAutomatically(data).then(res => {
        console.log(res, "savePaperAutomatically");
        this.$router.push("/exam/maketestersave/" + res);
      });
    }
  }
  // watch: {
  //   curChosenLibrary(newVal, oldVal) {
  //     console.log(newVal, oldVal, 'curChosenLibrary')
  //   }
  // }
};
</script>

<style lang="scss" scoped>
.maketesterauto-wrapper {
  margin: 10px 10px;
  padding: 10px;
  border-radius: 5px;
  max-width: 777px;
}
</style>
