<template>
  <el-dialog
    class="edit-undertaking edit-dialog"
    :visible.sync="modalIsShow"
    title="编辑承诺书"
    top="10vh"
    width="640px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="cancel"
    append-to-body
    @open="initData"
  >
    <h3 class="ut-title">考生承诺书</h3>
    <p>本人郑重承诺:</p>
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="0"
    >
      <el-form-item prop="undertakingBody">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="modalForm.undertakingBody"
          maxlength="90"
          show-word-limit
          class="ut-textarea"
          @input="contentChange"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <p class="ut-std">承诺人(签名):</p>

    <div slot="footer">
      <el-button type="primary" @click="submit">确认</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapMutations } from "vuex";

export default {
  name: "edit-undertaking",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { undertakingBody: "" },
      modalIsShow: false,
      rules: {
        undertakingBody: [
          {
            required: true,
            message: "请输入文本内容",
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {
    ...mapState("card", ["cardConfig"]),
  },
  methods: {
    ...mapMutations("card", ["setCardConfig"]),
    initData() {
      this.modalForm = {
        undertakingBody: this.cardConfig.undertakingBody,
      };
    },
    contentChange() {
      this.modalForm.undertakingBody = this.modalForm.undertakingBody.replace(
        "\n",
        ""
      );
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;
      this.setCardConfig(this.modalForm);
      this.cancel();
    },
  },
};
</script>

<style scoped>
.ut-title {
  text-align: center;
}
.ut-std {
  text-align: right;
  margin-right: 100px;
}
.ut-textarea >>> .el-textarea__inner {
  padding: 5px;
  text-indent: 28px;
  font-size: 14px;
  margin: 0 -5px;
}
</style>