<template>
  <el-dialog
    class="modify-ability-comment"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="700px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="等级：">
        {{ modalForm.levelCode }}
      </el-form-item>
      <el-form-item label="水平层次：">
        {{ modalForm.levelName }}
      </el-form-item>
      <el-form-item label="划分规则：">
        <p v-if="modalForm.levelCode === 'A'">
          百分位等级TOP≤{{ modalForm.endRate }}%
        </p>
        <p v-else>
          {{ modalForm.startRate }}%≤百分位等级TOP≤{{ modalForm.endRate }}%
        </p>
      </el-form-item>
      <el-form-item prop="result" label="诊断结果：">
        <el-input
          v-model.trim="modalForm.result"
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 5 }"
          placeholder="请输入诊断结果"
          maxlength="9999"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item prop="advice" label="学习建议：">
        <el-input
          v-model.trim="modalForm.advice"
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 5 }"
          placeholder="请输入学习建议"
          maxlength="9999"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
const initModalForm = {
  id: null,
  levelCode: "",
  levelName: "",
  startRate: null,
  endRate: null,
  result: "",
  advice: "",
};

export default {
  name: "modify-ability-comment",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "划分规则";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        result: [
          {
            required: true,
            message: "请输入诊断结果",
            trigger: "change",
          },
        ],
        advice: [
          {
            required: true,
            message: "请输入学习建议",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    visibleChange() {
      this.initData(this.instance);
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.$emit("modified", this.modalForm);
      this.cancel();
    },
  },
};
</script>
