import * as API from "api/virtual/properties";
export default {
    props: {
        show: {
            type: Boolean,
            default: false
        },
        id: {//企业id
            type: [String, Number],
            default: ''
        },
        name: {//企业名称
            type: String,
            default: '',
        },
        packagetype: {//企业所属套餐类型
            type: String,
            default: '',
        },
        propertyId: {//配置ID
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            my_show: false,
            loading: false,
            applyList: [],//应用平台列表
            applySetList: [],//应用平台配置列表
            agent: {
                show: false,
                title: '',
                applyid: '',//平台应用id
                id: '',//企业id
                name: '',//企业名称
                packagetype: '',//企业所属套餐类型
                propertyId: '',//配置ID
                parentId: '',//父级ID
            },
        };
    },
    components: {
        agentSet: resolve => require(['views/virtual/properties/agent_set'], resolve),
    },
    mounted() {
        this.$root.$on("close", data => {
            this.visibleChange();
        });
    },
    beforeDestroy() {
        this.$root.$off("close");
    },
    watch: {
        show(bool) {
            this.my_show = bool;
            if (bool) {
                this.getApplyList();
                this.getPlatformSetList();
            }
        },
    },
    methods: {
        getApplyList() {
            let data = new Object;
            data.name = this.name;
            // data.name = '佛山市兴诺科技有限公司';
            API.getApplyList(data)
                .then(res => {
                    if (res.code == 0) {
                        let applyList = [];
                        res.data.forEach(item => {
                            applyList.push(item.application);
                        })
                        this.applyList = applyList;
                    }
                })
        },
        getPlatformSetList() {
            let data = new Object;
            data.propertyId = this.propertyId;
            data.parentId = 0;
            API.getPlatformSetList(data)
                .then(res => {
                    if (res.code == 0) {
                        this.applySetList = res.data;
                    }
                })
        },
        handleSelect(data, index) {
            this.applySetList.forEach((item, num) => {
                if (index != num) {
                    if (item.info.appId == data.value) {
                        this.$Message.error('平台不能重复');
                        this.$nextTick(() => {
                            this.$set(this.applySetList[index].info, 'appId', '')
                        })
                    }
                }
            })
            this.applyList.forEach(item => {
                if (item.id == data.value) {
                    this.applySetList[index].info.appName = item.name;
                    this.applySetList[index].info.appType = item.appType;
                }
            })
        },
        addSet() {
            const index = this.applySetList.length;
            const data = {
                parentId: 0,
                propertyId: this.propertyId,
                type: 0,
                level: "#0",
                info: {
                    appName: '',
                    appType: '',
                    appId: '',
                    percent: '',
                }
            }
            this.$set(this.applySetList, index, data)
        },
        agentSet(data, index) {
            const applySetList = JSON.parse(JSON.stringify(this.applySetList))
            let percentTotal = 0;
            let isSave = true;
            let params = applySetList.filter((item) => {
                if (item.info.appId) {
                    if (item.info.percent <= 0) {
                        this.$Message.error('每个平台占比必须大于零');
                        isSave = false;
                    } else {
                        percentTotal += Number(item.info.percent);
                    }
                    item.info = JSON.stringify(item.info);
                    return true;
                } else {
                    return false;
                }
            })
            if (percentTotal != 100) {
                this.$Message.error('占比总和必须等于100');
                return false;
            }
            if (!isSave) {
                return false;
            }
            let obj = new Object;
            obj.list = params;
            API.savePlatformSet(obj)
                .then(res => {
                    if (res.code == 0) {
                        this.applySetList = res.data;
                        this.agent.id = this.id;
                        this.agent.name = this.name;
                        this.agent.packagetype = this.packagetype;
                        this.agent.applyid = data.info.appId;
                        this.agent.title = `${data.info.appType}-下级机构配置`;
                        this.agent.propertyId = this.propertyId;
                        this.agent.parentId = res.data[index].id;
                        this.agent.show = true;
                    }
                })
        },
        delSet(data, index) {
            const _this = this;
            if (data.id) {
                this.$Modal.confirm({
                    title: '警告',
                    content: '删除后，会将后续配置全部删除！',
                    onOk: function () {
                        const applySetList = JSON.parse(JSON.stringify(_this.applySetList));
                        if (applySetList.length != 1) {
                            let percentTotal = 0;
                            let isSave = true;
                            applySetList.forEach((item, num) => {
                                if (item.info.appId && index != num) {
                                    if (item.info.percent <= 0) {
                                        _this.$Message.error('删除项除外的每个平台占比必须大于零');
                                        isSave = false;
                                    } else {
                                        percentTotal += Number(item.info.percent);
                                    }
                                }
                            })
                            if (percentTotal != 100) {
                                _this.$Message.error('删除项除外占比总和必须等于100');
                                return false;
                            }
                            if (!isSave) {
                                return false;
                            }
                        }
                        let obj = new Object;
                        obj.id = data.id;
                        obj.level = data.level;
                        API.delPlatformSet(obj)
                            .then(res => {
                                if (res.code == 0) {
                                    _this.applySetList.splice(index, 1);
                                    _this.delSave();
                                }
                            })
                    }
                })
            } else {
                this.applySetList.splice(index, 1);
            }
        },
        delSave() {
            const applySetList = JSON.parse(JSON.stringify(this.applySetList));
            let params = applySetList.filter((item) => {
                if (item.info.appId) {
                    item.info = JSON.stringify(item.info);
                    return true;
                } else {
                    return false;
                }
            })
            let obj = new Object;
            obj.list = params;
            API.savePlatformSet(obj)
                .then(res => {
                    if (res.code == 0) {
                        this.applySetList = res.data;
                    }
                })
        },
        visibleChange(bool) {
            if (!bool) {
                this.$emit('update:show', false);
            }
            this.applyList = [];//应用平台列表
            this.applySetList = [];
            this.agent = {
                show: false,
                title: '',
                applyid: '',//平台应用id
                id: '',//企业id
                name: '',//企业名称
                packagetype: '',//企业所属套餐类型
                propertyId: '',//配置ID
                parentId: '',//父级ID
            };
        },
    }
};
