<template>
  <div class="edit-fill-question">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="120px"
    >
      <el-form-item prop="topicType" label="题型：">
        <el-radio-group v-model="topicType" @change="topicTypeChange">
          <el-radio-button label="single">单选</el-radio-button>
          <el-radio-button label="multiply">多选</el-radio-button>
          <el-radio-button label="boolean">判断</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="topicNo" label="大题序号：">
        <el-input-number
          v-model="modalForm.topicNo"
          :min="1"
          :max="99"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="topicName" label="题目名称：">
        <el-input
          v-model="modalForm.topicName"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          resize="none"
          placeholder="请输入题目名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="endNumber" label="起止题号：">
        <el-input-number
          style="width: 40px"
          v-model="modalForm.startNumber"
          :min="0"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-split"></span>
        <el-input-number
          style="width: 40px"
          v-model="modalForm.endNumber"
          :min="modalForm.startNumber"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="optionCount" label="选项个数：">
        <el-input-number
          style="width: 125px"
          v-model="modalForm.optionCount"
          :min="2"
          :max="22"
          :step="1"
          step-strictly
          :controls="false"
          :disabled="modalForm.isBoolean"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="选项排列方向：" required>
        <el-radio-group v-model="modalForm.optionDirection" size="small">
          <el-radio-button
            v-for="(val, key) in DIRECTION_TYPE"
            :key="key"
            :label="key"
            >{{ val }}</el-radio-button
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="modalForm.isBoolean" label="是否配置：">
        <el-select
          v-model="modalForm.booleanType"
          style="width: 125px"
          placeholder="请选择"
          @change="booleanTypeChange"
        >
          <el-option
            v-for="item in BOOLEAN_TYPE"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
        <span>（备选是否配置）</span>
      </el-form-item>
      <el-form-item v-if="modalForm.isBoolean" prop="booleanType">
        <span>是：</span>
        <el-input
          v-model.trim="booleanTypes.yes"
          :maxlength="1"
          placeholder="是"
          style="margin-right: 20px; width: 60px"
        ></el-input>
        <span>否：</span>
        <el-input
          v-model.trim="booleanTypes.no"
          :maxlength="1"
          placeholder="否"
          style="margin-right: 20px; width: 60px"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { BOOLEAN_TYPE, DIRECTION_TYPE } from "../../enumerate";

const initModalForm = {
  id: "",
  topicNo: null,
  topicName: "",
  startNumber: 1,
  endNumber: 5,
  questionsCount: 10,
  optionCount: 5,
  optionDirection: "horizontal",
  questionDirection: "vertical",
  isBoolean: false,
  booleanType: BOOLEAN_TYPE[0],
  isMultiply: false,
};

export default {
  name: "edit-fill-question",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    const topicTypeValidater = (rule, value, callback) => {
      if (!this.topicType) {
        callback(new Error("请选择题型"));
      } else {
        callback();
      }
    };
    const numberRangeValidater = (rule, value, callback) => {
      if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
        return callback(new Error("请输入起止题号"));
      }
      if (this.modalForm.startNumber > this.modalForm.endNumber) {
        callback(new Error("开始题号不能大于结束题号"));
      } else {
        callback();
      }
    };

    const booleanTypeValidater = (rule, value, callback) => {
      if (this.modalForm.isBoolean) {
        if (
          this.booleanTypes.yes &&
          this.booleanTypes.no &&
          this.booleanTypes.yes.length <= 2 &&
          this.booleanTypes.no.length <= 2
        ) {
          callback();
        } else {
          callback(new Error("请设置是否配置，单个设置最多两个字符。"));
        }
      } else {
        callback();
      }
    };

    return {
      modalForm: { ...initModalForm },
      BOOLEAN_TYPE,
      DIRECTION_TYPE,
      topicType: null,
      booleanTypes: {
        yes: "",
        no: "",
      },
      rules: {
        topicType: [
          {
            required: true,
            validator: topicTypeValidater,
            trigger: "change",
          },
        ],
        topicNo: [
          {
            required: true,
            message: "请输入大题序号",
            trigger: "change",
          },
        ],
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
        endNumber: [
          {
            required: true,
            message: "请输入起止题号",
            trigger: "change",
          },
          {
            type: "number",
            validator: numberRangeValidater,
            trigger: "change",
          },
        ],
        optionCount: [
          {
            required: true,
            type: "number",
            message: "请输入选项个数",
            trigger: "change",
          },
        ],
        booleanType: [
          {
            required: true,
            validator: booleanTypeValidater,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const valInfo = val.parent || val;
      if (val["_edit"]) this.topicType = this.getTopicType(valInfo);
      this.modalForm = Object.assign({}, initModalForm, valInfo);
      this.modalForm.endNumber =
        this.modalForm.startNumber + this.modalForm.questionsCount - 1;
      this.booleanTypeChange();
    },
    booleanTypeChange() {
      const [yes, no] = this.modalForm.booleanType.split(",");
      this.booleanTypes.yes = yes;
      this.booleanTypes.no = no;
    },
    topicTypeChange() {
      if (this.topicType === "single") {
        this.modalForm.isMultiply = false;
        this.modalForm.isBoolean = false;
        this.modalForm.optionCount = 4;
      } else if (this.topicType === "multiply") {
        this.modalForm.isMultiply = true;
        this.modalForm.isBoolean = false;
        this.modalForm.optionCount = 4;
      } else {
        this.modalForm.isMultiply = false;
        this.modalForm.isBoolean = true;
        this.modalForm.optionCount = 2;
        this.modalForm.booleanType = BOOLEAN_TYPE[0];
        this.booleanTypeChange();
      }
    },
    getTopicType(info) {
      if (info.isMultiply) return "multiply";
      if (info.isBoolean) return "boolean";
      return "single";
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.modalForm.questionsCount =
        this.modalForm.endNumber - this.modalForm.startNumber + 1;
      this.modalForm.booleanType = [
        this.booleanTypes.yes,
        this.booleanTypes.no,
      ].join();
      this.modalForm.topicName = this.modalForm.topicName.trim();
      this.$emit("modified", this.modalForm);
    },
  },
};
</script>
