<template>
  <div class="rule-card rule-form">
    <el-form
      ref="ModalFormLine"
      label-width="100px"
      :rules="rulesLine"
      :model="modalForm"
      inline
      style="padding-left: 40px;"
    >
      <el-form-item prop="examNumberStyle" label="考号版式：">
        <el-select
          v-model="modalForm.examNumberStyle"
          style="width: 142px;"
          @change="numStyleChange"
          placeholder="请选择"
        >
          <el-option
            v-for="(val, key) in EXAM_NUMBER_STYLE"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="paperType" label="AB卷版式：">
        <el-select
          v-model="modalForm.paperType"
          style="width: 142px;"
          placeholder="请选择"
        >
          <el-option
            v-for="(val, key) in PAPER_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 30px;">
        <el-checkbox v-model="modalForm.examAbsent">启用“缺考填涂”</el-checkbox>
      </el-form-item>
      <el-form-item style="margin-left: 30px;">
        <el-checkbox
          v-model="modalForm.writeSign"
          :disabled="modalForm.examNumberStyle === '2'"
          >启用“手写签名”</el-checkbox
        >
      </el-form-item>
    </el-form>

    <el-form
      ref="ModalFormRow"
      label-width="140px"
      :rules="rulesRow"
      :model="modalForm"
      style="max-width: 850px;"
    >
      <el-form-item label="考务字段：" style="min-height:200px;">
        <business-fields
          :data="modalForm"
          ref="BusinessFields"
          v-if="dataReady"
        ></business-fields>
      </el-form-item>
      <el-form-item prop="attention" label="注意事项：">
        <el-input
          type="textarea"
          :rows="5"
          v-model="modalForm.attention"
        ></el-input>
        <p class="tips-info">
          提示：换行之后，题卡注意事项会展示为多条内容，内容序号会被自动添加。
        </p>
      </el-form-item>
      <el-form-item prop="objectiveAttention" label="客观题注意事项：">
        <el-input v-model="modalForm.objectiveAttention"></el-input>
      </el-form-item>
      <el-form-item prop="subjectiveAttention" label="主观题注意事项：">
        <el-input v-model="modalForm.subjectiveAttention"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submit"
          :disabled="isSubmit"
          style="width:88px"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import BusinessFields from "./BusinessFields";
import { EXAM_NUMBER_STYLE, PAPER_TYPE } from "@/constants/enumerate";
import { cardRuleDetail, saveCardRule } from "../api";

export default {
  name: "rule-card",
  components: {
    BusinessFields
  },
  data() {
    return {
      modalForm: {
        examNumberStyle: "",
        paperType: "",
        examAbsent: true,
        writeSign: true,
        examMustColumn: "",
        examExtendColumn: "",
        attention: "",
        objectiveAttention: "",
        subjectiveAttention: ""
      },
      EXAM_NUMBER_STYLE,
      PAPER_TYPE,
      isSubmit: false,
      dataReady: false,
      rulesLine: {
        examNumberStyle: [
          {
            required: true,
            message: "请选择考号版式",
            trigger: "change"
          }
        ],
        paperType: [
          {
            required: true,
            message: "请选择AB卷版式",
            trigger: "change"
          }
        ]
      },
      rulesRow: {
        attention: [
          {
            required: true,
            message: "请输入注意事项",
            trigger: "change"
          },
          {
            validator: (rule, value, callback) => {
              const val = value.replace(/\n/g, "");
              if (val.length > 200) {
                callback(new Error("注意事项最多只能输入200个字符"));
              } else {
                callback();
              }
            },
            trigger: "change"
          }
        ],
        objectiveAttention: [
          {
            required: true,
            message: "请输入客观题注意事项",
            trigger: "change"
          },
          {
            max: 26,
            message: "客观题注意事项最多只能输入26个汉字",
            trigger: "change"
          }
        ],
        subjectiveAttention: [
          {
            required: true,
            message: "请输入主观题注意事项",
            trigger: "change"
          },
          {
            max: 26,
            message: "主观题注意事项最多只能输入26个汉字",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.getCardRule();
  },
  methods: {
    async getCardRule() {
      const data = await cardRuleDetail();
      this.modalForm = Object.assign(this.modalForm, data);
      this.modalForm.examNumberStyle += "";
      this.modalForm.paperType += "";
      this.modalForm.examAbsent = Boolean(this.modalForm.examAbsent);
      this.modalForm.writeSign = Boolean(this.modalForm.writeSign);
      this.dataReady = true;
    },
    numStyleChange() {
      this.modalForm.writeSign = this.modalForm.examNumberStyle !== "2";
    },
    async submit() {
      const valid1 = await this.$refs["ModalFormLine"]
        .validate()
        .catch(() => {});
      const valid2 = await this.$refs["ModalFormRow"]
        .validate()
        .catch(() => {});
      if (!valid1 || !valid2) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const modals = {
        ...this.modalForm,
        ...this.$refs.BusinessFields.getSelectColumns()
      };
      modals.examNumberStyle = Number(modals.examNumberStyle);
      modals.paperType = Number(modals.paperType);
      modals.examAbsent = Number(modals.examAbsent);
      modals.writeSign = Number(modals.writeSign);
      const data = await saveCardRule(modals).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success("保存成功！");
    }
  }
};
</script>
