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: ''
        },
        packagetype: {//校正过的套餐类型
            type: String,
            default: '',
        },
        propertyId: {//配置ID
            type: [String, Number],
            default: ''
        },
        parentId: {//父级ID
            type: [String, Number],
            default: ''
        },
        level: {//层级标志
            type: String,
            default: '',
        }
    },
    data() {
        return {
            my_show: false,
            loading: false,
            packageList: [],//套餐列表
            packageSetList: [],//套餐配置列表
        };
    },
    mounted() {
    },
    watch: {
        show(bool) {
            this.my_show = bool;
            if (bool) {
                this.getPackageList();
                this.getPlatformSetList();
            }
        },
    },
    methods: {
        getPackageList() {
            let data = new Object;
            data.id = this.id;
            API.getPackageList(data)
                .then(res => {
                    if (res.code == 0) {
                        this.packageList = res.data[this.packagetype]
                    }
                })
        },
        getPlatformSetList() {
            let data = new Object;
            data.propertyId = this.propertyId;
            data.parentId = this.parentId;
            API.getPlatformSetList(data)
                .then(res => {
                    if (res.code == 0) {
                        this.packageSetList = res.data;
                    }
                })
        },
        addSet() {
            const index = this.packageSetList.length;
            const level = `${this.level}#${this.parentId}`
            const data = {
                parentId: this.parentId,
                propertyId: this.propertyId,
                type: 3,
                level: level,
                info: {
                    productName: '',//产品名称
                    percent: '',//占比
                }
            }
            this.$set(this.packageSetList, index, data);
        },
        delSet(data, index) {
            const _this = this;
            if (data.id) {
                this.$Modal.confirm({
                    title: '警告',
                    content: '确认要删除！',
                    onOk: function () {
                        const packageSetList = JSON.parse(JSON.stringify(_this.packageSetList));
                        if (packageSetList.length != 1) {
                            let percentTotal = 0;
                            let isSave = true;
                            packageSetList.forEach((item, num) => {
                                if (item.info.productName && 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.packageSetList.splice(index, 1);
                                    _this.delSave();
                                }
                            })
                    }
                })
            } else {
                this.packageSetList.splice(index, 1);
            }
        },
        delSave() {
            const packageSetList = JSON.parse(JSON.stringify(this.packageSetList));
            let params = packageSetList.filter((item) => {
                if (item.info.productName) {
                    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.packageSetList = res.data;
                    }
                })
        },
        handleSelect(data, index) {
            this.packageSetList.forEach((item, num) => {
                if (index != num) {
                    if (item.info.productName == data) {
                        this.$Message.error('产品不能重复');
                        this.$nextTick(() => {
                            this.$set(this.packageSetList[index].info, 'productName', '')
                        })
                    }
                }
            })
        },
        saveSet(data, index) {
            const packageSetList = JSON.parse(JSON.stringify(this.packageSetList))
            let percentTotal = 0;
            let isSave = true;
            let params = packageSetList.filter((item) => {
                if (item.info.productName) {
                    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.$root.$emit('close')
                    }
                })
        },
        visibleChange(bool) {
            if (!bool) {
                this.$emit('update:show', false);
            }
            this.packageList = [];//套餐列表
            this.packageSetList = [];//套餐配置列表
        },
    }
};
