<template>
  <el-dialog title="编辑备注" :visible.sync="visible" width="680px" @close="dialogClose()">
    <RichText
      v-model="richData.note"
      toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
      :wordlimit-num="false"
      :height="240"
      placeholder="输入内容"
      page-from="audit"
    />
    <template slot="footer">
      <el-button size="medium" @click="deleteNote">删除</el-button>
      <el-button type="primary" size="medium" @click="confirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import RichText from './RichText';
export default {
  name: 'ExplanatoryNoteDialog',
  components: {
    RichText,
  },
  props: {
    open: {
      type: Boolean,
      default: false,
      required: true,
    },
    initData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      visible: false,
      richData: this.initData,
    };
  },
  watch: {
    open(newVal) {
      this.visible = newVal;
      this.richData = this.initData;
    },
    visible(newVal) {
      if (!newVal) {
        this.$emit('update:open', false);
        this.richData = {};
      }
    },
  },
  methods: {
    // 关闭弹窗，调用富文本组件中的
    dialogClose() {
      this.visible = false;
    },
    deleteNote() {
      this.$confirm('确定要删除此条注释吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.visible = false;
          this.$emit('cancel');
        })
        .catch(() => {});
    },
    confirm() {
      this.$emit('confirm', this.richData);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-dialog {
  :deep &__body {
    display: flex;
    flex-direction: column;
    row-gap: 8px;
    padding: 8px 8px 0;

    .el-textarea__inner {
      height: 108px;
    }
  }

  :deep &__footer {
    display: flex;
    padding: 8px;

    .el-button {
      flex: 1;
    }
  }
}
</style>
