<template>
    <el-dialog :title="title" :visible.sync="visible" width="450px" :before-close="onClose">
        <el-form :model="queryForm" ref="setSession" :rules="rules">
            <el-form-item label="存档Secret" prop="msgAuditSecret">
                <el-input v-model="queryForm.msgAuditSecret" placeholder="请输入会话存档Secret" clearable size="small" />
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="onClose">取 消</el-button>
            <el-button type="primary" @click="onSubmit()">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { setMsgSession } from "@/api/accounts/corpWeChat";
export default {
    name: 'setSession',
    data() {
        return {
            title: '配置通会话存档',
            rules: {
                msgAuditSecret: [
                    { required: true, message: '请输入会话存档Secret', trigger: 'blur' }
                ]
            },
            queryForm: { msgAuditSecret: null }
        }
    },
    props: {
        corpId: {
            type: String,
            default: null,
        },
        msgAuditSecret: {
            type: String,
            default: null,
        },
        visible: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        msgAuditSecret: {
            handler(val) {
                this.queryForm.msgAuditSecret = val
            },
            immediate: true,
        }
    },
    methods: {
        onClose() {
            this.$emit("close");
        },
        onSubmit() {
            if (!this.corpId) {
                this.$message.error('没有企业ID');
                return
            }
            this.$refs['setSession'].validate(async (valid) => {
                if (valid) {
                    const loading = this.$loading({
                        lock: true,
                        text: 'Loading',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                    const params = { corpId: this.corpId, ...this.queryForm }
                    console.log(params)
                    setMsgSession(params).then(res => {
                        this.$message({
                            message: '配置成功',
                            type: 'success'
                        });
                        loading.close()
                        this.$emit("change");
                    }).catch(() => loading.close())
                }
            })
        }
    }
}
</script>