<template>
  <div class="edit-fill-line">
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item v-if="instance.parent" 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="lineSpacing" label="空位上下间距：">
        <el-input-number
          v-model.number="modalForm.lineSpacing"
          style="width: 125px"
          :min="20"
          :max="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="fillCountPerLine" label="每行空数：">
        <el-input-number
          v-model="modalForm.fillCountPerLine"
          style="width: 125px"
          :min="1"
          :max="10"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-tips">*指一行显示空位数量</span>
      </el-form-item>
      <el-form-item label="小题空数类型：">
        <el-radio-group v-model="modalForm.questionLineType" size="small">
          <el-radio-button label="norm">标准</el-radio-button>
          <el-radio-button label="custom">自定义</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-if="modalForm.questionLineType === 'norm'"
        label="小题空数："
      >
        <el-input-number
          v-model="modalForm.fillCount"
          style="width: 125px"
          :min="1"
          :max="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-tips">*指每一小题的空位数量</span>
      </el-form-item>
      <el-form-item v-else prop="questionLineNums" label="各小题空数：">
        <table class="table table-white table-narrow">
          <tr>
            <th>小题号</th>
            <th>空数</th>
          </tr>
          <tr v-for="option in questionLineNumOptions" :key="option.no">
            <td>{{ option.questionNo }}</td>
            <td>
              <el-input-number
                v-model="option.fillCount"
                size="mini"
                :min="1"
                :max="50"
                :step="1"
                step-strictly
                :controls="false"
                style="width: 125px"
              ></el-input-number>
            </td>
          </tr>
        </table>
      </el-form-item>
    </el-form>
  </div>
</template>

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

const initModalForm = {
  id: "",
  topicName: "",
  fillCountPerLine: 1,
  lineSpacing: 40,
  fillCount: null,
  questionLineType: "custom",
};

export default {
  name: "EditFillLine",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      DIRECTION_TYPE,
      questionLineNumOptions: [],
      rules: {
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
        lineSpacing: [
          {
            required: true,
            type: "number",
            message: "请输入空位上下间距",
            trigger: "change",
          },
        ],
        fillCountPerLine: [
          {
            required: true,
            type: "number",
            message: "请输入每行空数",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const valInfo = val.parent;
      this.modalForm = this.$objAssign(initModalForm, valInfo);
      if (!valInfo.fillCount) this.modalForm.fillCount = undefined;
      this.questionLineNumOptions = valInfo.paperStruct.questions.map(
        (item) => {
          return { ...item };
        }
      );
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;
      const data = this.$objAssign(this.instance.parent, this.modalForm);
      if (data.questionLineType === "norm") {
        if (!data.fillCount) data.fillCount = null;
      } else {
        data.fillCount = null;
        const unfinishedQnos = this.questionLineNumOptions
          .filter((item) => !item.fillCount)
          .map((item) => item.questionNo);
        if (unfinishedQnos.length) {
          this.$message.error(`请输入小题${unfinishedQnos.join("、")}的空数`);
          return;
        }
        data.paperStruct.questions = this.questionLineNumOptions;
      }
      this.$emit("modified", data);
    },
  },
};
</script>
