<template>
    <div v-loading="loading" element-loading-text="拼命加载中">
        <el-row :gutter="20">
            <el-col :span="14">
                <message-form
                    ref="refMessageForm"
                    @msgTypeChange="handleMsgTypeChange"
                    @submit="submit"
                ></message-form>
            </el-col>
            <el-col :span="10">
                <el-card v-if="showMsgType4">
                    <select-enterprises
                        ref="refSelectEnterprises"
                    ></select-enterprises>
                </el-card>
                <el-card v-if="showMsgType6">
                    <select-users ref="refSelectUsers"></select-users>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import MessageForm from "./components/MessageForm";
import SelectEnterprises from "./components/SelectEnterprises";
import SelectUsers from "./components/SelectUsers";

import USER_MANAGEMENT from "@/api/user-management";

import cache from "@/utils/cache";

export default {
    name: "MessageSend",
    components: {
        "message-form": MessageForm,
        "select-enterprises": SelectEnterprises,
        "select-users": SelectUsers,
    },
    data() {
        return {
            loading: false,
            showMsgType4: false, // 显示 企业个体消息 组件
            showMsgType6: false, // 显示 用户个人消息 组件
        };
    },

    methods: {
        handleMsgTypeChange(msgType) {
            this.showMsgType4 = msgType === 4;
            this.showMsgType6 = msgType === 6;
        },
        submit(args) {
            let argsSelectEnts = [];
            let argsSelectUsers = [];
            if (this.showMsgType4) {
                let selectEnts = this.$refs.refSelectEnterprises.getSelectEnts();
                if (Array.isArray(selectEnts) && selectEnts.length > 0) {
                    for (const item of selectEnts) {
                        argsSelectEnts.push(item.entTaxId);
                    }
                } else {
                    this.$message.error("没请选择至少一个接收消息企业！");
                    return false;
                }
            } else if (this.showMsgType6) {
                let selectUsers = this.$refs.refSelectUsers.getSelectUsers();
                if (Array.isArray(selectUsers) && selectUsers.length > 0) {
                    for (const item of selectUsers) {
                        argsSelectUsers.push(item.id);
                    }
                } else {
                    this.$message.error("请选择至少一个接收消息用户！");
                    return false;
                }
            }

            let _this = this;
            this.loading = true;
            USER_MANAGEMENT.msgSaveOrUpdate(args)
                .then((val) => {
                    let args2 = {
                        msgId: val.data.msgId,
                        msgType: args.msgType,
                    };
                    if (_this.showMsgType4) {
                        args2.receiveKeys = argsSelectEnts;
                    } else if (_this.showMsgType6) {
                        args2.receiveKeys = argsSelectUsers;
                    } else if (args.msgType === 7) {
                        // 服务商下企业
                        let ispId = JSON.parse(cache.getLocal("ISP")).ispId;
                        args2.receiveKeys = [ispId];
                    } else {
                        _this.$message.success(val.msg);
                        return;
                    }

                    _this.loading = true;
                    USER_MANAGEMENT.msgSend(args2)
                        .then((val2) => {
                            _this.$message.success(val2.msg);
                        })
                        .catch((err2) => {
                            _this.$message.error(err2);
                        })
                        .finally(() => {
                            _this.loading = false;
                        });
                })
                .catch((err) => {
                    _this.$message.error(err);
                })
                .finally(() => {
                    _this.loading = false;
                });
        },
    },
};
</script>

<style scoped>
</style>