import * as API from "api/virtual/properties";
export default {
    props: {
        show: {
            type: Boolean,
            default: false
        },
        title: {//弹窗标题
            type: String,
            default: ''
        },
        id: {//企业id
            type: [String, Number],
            default: ''
        },
        name: {//企业名称
            type: String,
            default: ''
        },
        packagetype: {//企业所属套餐类型
            type: String,
            default: '',
        },
        applyid: {//平台应用ID用来获取车型
            type: [String, Number],
            default: ''
        },
        propertyId: {//配置ID
            type: [String, Number],
            default: ''
        },
        parentId: {//父级ID
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            my_show: false,
            loading: false,
            agentList: [],//机构列表
            agentSetList: [],//机构配置列表
            cartype: {
                show: false,
                title: '',
                applyid: '',//平台应用id
                id: '',//企业id
                name: '',//企业名称
                packagetype: '',//企业所属套餐类型
                propertyId: '',//配置ID
                parentId: '',//父级ID
                level: '',//层级标志
            },
        };
    },
    components: {
        cartypeSet: resolve => require(['views/virtual/properties/cartype_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.getAgentsList();
                this.getPlatformSetList();
            }
        },
    },
    methods: {
        getAgentsList() {
            let data = new Object;
            data.id = this.id;
            // data.id = 21;
            API.getAgentsList(data)
                .then(res => {
                    if (res.code == 0) {
                        this.agentList = res.data;
                    }
                })
        },
        getPlatformSetList() {
            let data = new Object;
            data.propertyId = this.propertyId;
            data.parentId = this.parentId;
            API.getPlatformSetList(data)
                .then(res => {
                    if (res.code == 0) {
                        this.agentSetList = res.data;
                    }
                })
        },
        addSet() {
            const index = this.agentSetList.length;
            const level = `#0#${this.parentId}`
            const data = {
                parentId: this.parentId,
                propertyId: this.propertyId,
                type: 1,
                level: level,
                info: {
                    agentId: '',//机构id
                    agentName: '',//机构名称
                    percent: '',//占比
                }
            }
            this.$set(this.agentSetList, index, data);
        },
        handleSelect(data, index) {
            this.agentSetList.forEach((item, num) => {
                if (index != num) {
                    if (item.info.agentId == data.value) {
                        this.$Message.error('机构不能重复');
                        this.$nextTick(() => {
                            this.$set(this.agentSetList[index].info, 'agentId', '')
                        })
                    }
                }
            })
            this.agentSetList[index].info.agentName = data.label;
        },
        cartypeSet(data, index) {
            const agentSetList = JSON.parse(JSON.stringify(this.agentSetList))
            let percentTotal = 0;
            let isSave = true;
            let params = agentSetList.filter((item) => {
                if (item.info.agentId) {
                    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.agentSetList = res.data;
                        const title = this.title.split("-")[0];
                        this.cartype.title = `${title}-${data.info.agentName}-车型配置`;
                        this.cartype.id = this.id;
                        this.cartype.name = this.name;
                        this.cartype.packagetype = this.packagetype;
                        this.cartype.applyid = this.applyid;
                        this.cartype.propertyId = this.propertyId;
                        this.cartype.parentId = res.data[index].id;
                        this.cartype.level = res.data[index].level;
                        this.cartype.show = true;
                    }
                })
        },
        delSet(data, index) {
            const _this = this;
            if (data.id) {
                this.$Modal.confirm({
                    title: '警告',
                    content: '删除后，会将后续配置全部删除！',
                    onOk: function () {
                        const agentSetList = JSON.parse(JSON.stringify(_this.agentSetList));
                        if (agentSetList.length != 1) {
                            let percentTotal = 0;
                            let isSave = true;
                            agentSetList.forEach((item, num) => {
                                if (item.info.agentId && 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.agentSetList.splice(index, 1);
                                    _this.delSave();
                                }
                            })
                    }
                })
            } else {
                this.agentSetList.splice(data, 1)
            }
        },
        delSave() {
            const agentSetList = JSON.parse(JSON.stringify(this.agentSetList));
            let params = agentSetList.filter((item) => {
                if (item.info.agentId) {
                    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.agentSetList = res.data;
                    }
                })
        },
        visibleChange(bool) {
            if (!bool) {
                this.$emit('update:show', false);
            }
            this.agentList = [];//机构列表
            this.agentSetList = [];
            this.cartype = {
                show: false,
                title: '',
                applyid: '',//平台应用id
                id: '',//企业id
                name: '',//企业名称
                packagetype: '',//企业所属套餐类型
                propertyId: '',//配置ID
                parentId: '',//父级ID
                level: '',//层级标志
            };
        },
    }
};
