<template>
  <el-dialog
    :title="dTitle"
    :visible.sync="modalIsShow"
    width="520px"
    :modal="true"
    append-to-body
    custom-class="side-dialog"
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="90px"
    >
      <el-form-item label="大题名称" prop="detailName">
        <el-input
          v-model="modalForm.detailName"
          class="dialog-input-width"
          placeholder="请输入题型名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="大题描述" prop="description">
        <v-editor
          v-model="modalForm.description"
          placeholder="请输入大题描述"
          :enable-formula="false"
        ></v-editor>
      </el-form-item>
      <template v-if="!onlyName">
        <el-form-item label="题型" prop="sourceDetailId">
          <source-detail-select
            v-model="modalForm"
            :course-id="modalForm.courseId"
          ></source-detail-select>
        </el-form-item>
        <el-form-item label="每题分值" prop="scorePerQuestion">
          <el-input-number
            v-model="modalForm.scorePerQuestion"
            class="align-left"
            placeholder="请输入每题分值"
            :step="0.1"
            :min="0.1"
            :max="999"
            :controls="false"
            :precision="1"
            step-strictly
          ></el-input-number>
        </el-form-item>
      </template>
      <template v-if="showSelective && !isAdd">
        <el-form-item label="是否选做题">
          <el-radio-group
            v-model="modalForm.selective"
            @change="selectiveChange"
          >
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="modalForm.selective"
          label="选做题数"
          prop="selectiveCount"
        >
          <el-input-number
            v-model="modalForm.selectiveCount"
            class="align-left"
            placeholder="请输入选做题数"
            :step="1"
            :min="1"
            :max="999"
            :controls="false"
            :precision="0"
            step-strictly
          ></el-input-number>
        </el-form-item>
        <el-form-item
          v-if="modalForm.selective"
          label="取分规则"
          prop="selectiveRule"
        >
          <el-select v-model="modalForm.selectiveRule">
            <el-option
              v-for="(val, key) in SELECTIVE_RULE_TYPE"
              :key="key"
              :label="val"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="confirm">保存</el-button>
      <el-button type="danger" plain @click="cancel">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { randomCode } from "@/plugins/utils";
import { debounce } from "lodash";
import { SELECTIVE_RULE_TYPE } from "@/constants/constants";

const initModalForm = {
  id: null,
  detailName: "",
  courseId: "",
  description: { sections: [] },
  questionType: null,
  sourceDetailId: null,
  scorePerQuestion: 1,
  selective: false,
  selectiveCount: null,
  selectiveRule: "",
};
export default {
  name: "ModifyDetailStruct",
  props: {
    detail: {
      type: Object,
      default() {
        return {};
      },
    },
    onlyName: {
      type: Boolean,
      default: false,
    },
    showSelective: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    isAdd() {
      return !Object.keys(this.detail).length;
    },
    dTitle() {
      return this.isAdd ? "新增大题" : "大题信息";
    },
  },
  data() {
    return {
      modalIsShow: false,
      SELECTIVE_RULE_TYPE,
      modalForm: { ...initModalForm },
      rules: {
        detailName: [
          {
            required: true,
            message: "请输入大题名称",
            trigger: "change",
          },
        ],
        sourceDetailId: [
          {
            required: true,
            message: "请选择题型",
            trigger: "change",
          },
        ],
        scorePerQuestion: [
          {
            required: true,
            message: "请输入每题分值",
            trigger: "change",
          },
        ],
        selectiveCount: [
          {
            required: true,
            message: "请输入选做题数",
            trigger: "blur",
          },
        ],
        selectiveRule: [
          {
            required: true,
            message: "请选择取分规则",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = this.$objAssign(initModalForm, this.detail);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    selectiveChange() {
      this.modalForm.selectiveRule = "";
      this.modalForm.selectiveCount = undefined;
    },
    confirm: debounce(
      async function () {
        const valid = await this.$refs.modalFormComp.validate().catch(() => {});
        if (!valid) return;

        let data = { ...this.modalForm };
        if (!data.id) data.id = randomCode();
        this.$emit("modified", data, this.isAdd);
        this.cancel();
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
