<template>
  <el-dialog
    class="warning-text-message-dialog"
    :visible.sync="dialogVisible"
    width="540px"
    title="文字提醒"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    @open="opened"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="0"
    >
      <el-form-item prop="content">
        <el-input
          type="textarea"
          v-model.trim="modalForm.content"
          placeholder="请输入"
          :maxlength="200"
          :rows="5"
          show-word-limit
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel" plain>取消</el-button>
      <el-button type="primary" :disabled="isSubmit" @click="confirm"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { sendWarningMsg } from "@/api/invigilation";

export default {
  name: "warning-text-message-dialog",
  props: {
    recordId: {
      type: String,
    },
  },
  data() {
    return {
      dialogVisible: false,
      isSubmit: false,
      modalForm: { content: "" },
      rules: {
        content: [
          {
            required: true,
            message: "请输入文字提醒内容",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    opened() {
      this.modalForm.content = "";
    },
    cancel() {
      this.dialogVisible = false;
    },
    open() {
      this.dialogVisible = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const result = await sendWarningMsg({
        content: this.modalForm.content,
        type: "text",
        examRecordId: this.recordId,
      }).catch(() => {});

      this.isSubmit = false;
      if (!result) return;
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
