<template>
  <div class="app-container">
    <el-form
      :model="Form"
      :rules="rules"
      ref="Form"
      label-width="100px"
      status-icon
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="Form.name" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="单选题">
        选取
        <el-input-number v-model="Form.danxuan_count" :min="0" ></el-input-number>道,每道
        <el-input-number v-model="Form.danxuan_score" :min="0"></el-input-number>分
      </el-form-item>
      <el-form-item label="多选题">
        选取
        <el-input-number v-model="Form.duoxuan_count" :min="0" ></el-input-number>道,每道
        <el-input-number v-model="Form.duoxuan_score" :min="0"></el-input-number>分
      </el-form-item>
      <el-form-item label="判断题">
        选取
        <el-input-number v-model="Form.panduan_count" :min="0" ></el-input-number>道,每道
        <el-input-number v-model="Form.panduan_score" :min="0"></el-input-number>分
      </el-form-item>
      <el-form-item label="时间限制" prop="limit">
        <el-input-number v-model="Form.limit" :min="0"></el-input-number>分钟
      </el-form-item>
      <el-form-item label="当前总分">
        <span style="color:darkred;font-weight:bold">{{Form.total_score}} 分</span>
      </el-form-item>
      <el-form-item label="及格分数" prop="pass_score">
        <el-input-number v-model="Form.pass_score" :min="0"></el-input-number>分
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('Form')" :loading="submitLoding">立即创建</el-button>
        <el-button @click="resetForm('Form')">重置</el-button>
        <el-button type="warning" @click="goBack()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { createTestRule } from "@/api/examtest";
import { getSubjectAll, getQuestioncatAll, getQuestionCount } from "@/api/question";
import { genTree } from "@/utils";
export default {
  data() {
    return {
      Form: {
        name: "",
        danxuan_count:0,
        danxuan_score:0,
        duoxuan_count:0,
        duoxuan_score:0,
        panduan_count:0,
        panduan_score:0,
        limit:0,
        total_score:0,
        pass_score:0
      },
      submitLoding:false,
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        limit: [
          { required: true, message: "时间限制不能为空"},
          { type: "number", message: "时间限制必须是数字"}
        ],
        pass_score: [
          { required: true, message: "及格分数不能为空"},
          { type: "number", message: "及格分数必须是数字"}
        ]

      }
    };
  },
  watch:{
    Form:{
      handler:'calScore',
      deep:true
    },
  },
  created() {
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitLoding = true
              createTestRule(this.Form).then(response => {
                this.submitLoding = false
                this.$message({
            type: "success",
            message: "新建成功!"
          });
                this.goBack()
              });
        } else {
          return false;
        }
      });
    },
    calScore(current,old) {
      let form = this.Form
      let score = form.danxuan_count * form.danxuan_score + form.duoxuan_count * form.duoxuan_score + form.panduan_count * form.panduan_score
      this.Form.total_score = score
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.typecount.danxuan = 0;
      this.typecount.duoxuan = 0;
      this.typecount.panduan = 0;
    },
    goBack() {
      this.$router.replace('/sjmanage/testrule/')
    }
  }
};
</script>