<template>
    <el-dialog v-model="visible" :title="`回复@${form.replyName}`" width="600" align-center @closed="$emit('closed')">
        <el-input v-model="form.messageContent" type="textarea" autosize clearable show-word-limit maxlength="140" placeholder="发布你的回复"></el-input>

        <template #footer>
            <el-button :loading="isSaving" type="primary" :disabled="!form.messageContent" @click="submit">回复</el-button>
        </template>
    </el-dialog>
</template>

<script>
import API from "@/api/policy/message";

export default {
    emits: ["success", "closed"],
    props: {
        refId: { type: String, default: "" },
        refType: { type: String, default: "policy_share" }
    },
    
    data() {
        return {
            visible: false,
            isSaving: false,
            
            form: {
                parentId: null,
                refId: this.refId,
                refType: this.refType,
                replyId: null,
                replyName: null,
                messageContent: null
            }
        }
    },

    methods: {
        open() {
            this.visible = true;
            return this;
        },

        setData(data) {
            this.form.parentId = data.id;
            this.form.replyName = data.createName;

            if (data.parentId != 0) {
                this.form.parentId = data.parentId;
                this.form.replyId = data.createId;
            }
        },

        // 表单提交方法
        submit() {
            const data = Object.assign({}, this.form);
            if (!data.replyId) data["replyName"] = null;

            this.isSaving = true;
            API.add(data).then(res => {
                this.isSaving = false;
                if (res.code === 200) {
                    ElMessage.success("操作成功");
                    this.visible = false;
                    this.$emit("success", this.form.parentId);
                } else ElMessage.error(res.msg);
            }).catch(() => this.isSaving = false);
        }
    }
}
</script>

<style lang="scss" scoped>
.el-textarea {
  margin: 10px 0 18px;
}
</style>