<template>
  <div class="edit-fill-question">
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <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="questionCountPerGroup" label="单列小题数：">
        <el-input-number
          v-model="modalForm.questionCountPerGroup"
          style="width: 125px"
          :min="1"
          :max="5"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item
        v-if="!modalForm.isBoolean"
        prop="optionCount"
        label="选项个数："
      >
        <el-input-number
          v-model="modalForm.optionCount"
          style="width: 125px"
          :min="2"
          :max="20"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="题型：">
        <el-tag v-if="modalForm.isMultiply" effect="dark">多选</el-tag>
        <el-tag v-else-if="modalForm.isBoolean" effect="dark">判断</el-tag>
        <el-tag v-else effect="dark">单选</el-tag>
      </el-form-item>
      <el-form-item v-if="modalForm.isBoolean">
        <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"
        label="是否配置："
      >
        <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 } from "../../enumerate";

const initModalForm = {
  id: "",
  topicName: "",
  questionCountPerGroup: 5,
  isBoolean: false,
  booleanType: BOOLEAN_TYPE[0],
  isMultiply: false,
  optionCount: null,
};

export default {
  name: "EditFillQuestion",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    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,
      booleanTypes: {
        yes: "",
        no: "",
      },
      rules: {
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
        questionCountPerGroup: [
          {
            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;
      this.modalForm = this.$objAssign(initModalForm, valInfo);
      if (!valInfo.optionCount) this.modalForm.optionCount = undefined;
      this.booleanTypeChange();
    },
    selectTypeChange(val) {
      if (val) {
        this.modalForm.booleanType = BOOLEAN_TYPE[0];
        this.booleanTypeChange();
      }
    },
    booleanTypeChange() {
      const [yes, no] = this.modalForm.booleanType.split(",");
      this.booleanTypes.yes = yes;
      this.booleanTypes.no = no;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.modalForm.booleanType = [
        this.booleanTypes.yes,
        this.booleanTypes.no,
      ].join();
      this.modalForm.topicName = this.modalForm.topicName.trim();
      const data = this.$objAssign(this.instance.parent, this.modalForm);
      if (!data.optionCount) data.optionCount = null;
      this.$emit("modified", data);
    },
  },
};
</script>
