const _Http = getApp().globalData.http;
Page({
    data: {
        isLeader: false, //是否为负责人
        tabsActive: 0, //tabs 选中项
        sat_campaignid: "",
        detail: '',
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "活动线索",
            icon: "icon-tabxiansuo",
            model: "#clue"
        }, {
            label: "操作",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        }, {
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        }],
        tabbarList: [{
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-dibu-chengjiao",
            label: "发布"
        }, {
            icon: "icon-dibu-jieshu",
            label: "结束"
        }, ],
        oldtabbarList: [{
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-dibu-chengjiao",
            label: "发布"
        }, {
            icon: "icon-dibu-jieshu",
            label: "结束"
        }],
    },
    onLoad(options) {
        this.setData({
            sat_campaignid: options.id,
            isAdmin: wx.getStorageSync('auth').wmarketing_activity.options.some(v => v == "admin"), //是否具有管理权限
            options: wx.getStorageSync('auth').wmarketing_activity.options, //权限列表
        });
        this.getDetail();
    },
    getDetail() {
        _Http.basic({
            "classname": "webmanage.saletool.orderclue.ordercluecampaign",
            "method": "queryCampaignMain",
            "version": 1,
            "content": {
                "nocache": true,
                "sat_campaignid": this.data.sat_campaignid
            },
        }).then(res => {
            console.log("市场活动详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                detail: res.data,
                briefs: [{
                    label: "活动类型",
                    value: res.data.type
                }, {
                    label: "线索数",
                    value: res.data.ordercluecount || '0'
                }, {
                    label: "活动负责人",
                    value: res.data.director
                }, {
                    label: "状态",
                    value: res.data.status
                }],
                list1: [{
                    label: "市场活动名称",
                    value: res.data.name
                }, {
                    label: "活动类型",
                    value: res.data.type
                }, {
                    label: "线索数",
                    value: res.data.ordercluecount || '0'
                }, {
                    label: "活动地点",
                    value: res.data.address
                }, {
                    label: "状态",
                    value: res.data.status
                }, {
                    label: "开始时间",
                    value: res.data.begdate
                }, {
                    label: "结束时间",
                    value: res.data.enddate
                }, {
                    label: "活动负责人",
                    value: res.data.director
                }],
                list2: [{
                    label: "创建人",
                    value: res.data.createby
                }, {
                    label: "创建时间",
                    value: res.data.createdate
                }, {
                    label: "最近编辑人",
                    value: res.data.changeby
                }, {
                    label: "最近编辑时间",
                    value: res.data.changedate
                }, {
                    label: "发布人",
                    value: res.data.changeby
                }, {
                    label: "发布时间",
                    value: res.data.followdate
                }, {
                    label: "结束人",
                    value: res.data.followdate
                }, {
                    label: "转手次数",
                    value: res.data.leader[0].leadernum
                }],
                tabsList: this.data.tabsList
            });
            let arr = this.data.oldtabbarList;
            if (res.data.status == '发布') {
                arr = arr.filter(item => item.label == '发布' || item.label == '结束')
                arr[0].label = '取消发布'
            } else if (res.data.status == '结束') {
                arr = []
            } else {
                arr[1].label = '发布'
            }
            this.setData({
                tabbarList: arr,
                isLeader: res.data.createuserid == wx.getStorageSync('userMsg').userid
            })
            this.getTags();
            this.getGroup();
            this.partialRenewal();
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let model = this.data.tabsList[this.data.tabsActive].model;
        if (model) {
            let Component = this.selectComponent(model),
                {
                    total,
                    pageNumber,
                    pageTotal
                } = Component.data.content,
                id = this.data.detail.sat_campaignid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber < pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail;
        switch (detail.label) {
            case "编辑":
                wx.navigateTo({
                    url: `/packageA/activity/addActivity?rowData=${JSON.stringify(this.data.detail)}`,
                })
                break;
            case "发布":
                wx.showModal({
                    title: '提示',
                    content: `是否确认发布该市场活动？`,
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "classname": "webmanage.saletool.orderclue.ordercluecampaign",
                            method: 'release',
                            "content": {
                                "sat_campaignids": [this.data.detail.sat_campaignid]
                            }
                        }).then(res => {
                            wx.showToast({
                                title: res.msg == '成功' ? `已发布该活动` : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') this.getDetail()
                        })
                    }
                })
                break;
            case "结束":
                wx.showModal({
                    title: '提示',
                    content: `是否确认结束该市场活动？`,
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "classname": "webmanage.saletool.orderclue.ordercluecampaign",
                            method: 'endcampaign',
                            "content": {
                                "sat_campaignids": [this.data.detail.sat_campaignid]
                            }
                        }).then(res => {
                            wx.showToast({
                                title: res.msg == '成功' ? `已结束该活动` : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') {
                                this.getDetail()
                                this.setData({
                                    tabbarList: []
                                })
                            }
                        })
                    }
                })
                break;
            case "取消发布":
                wx.showModal({
                    title: '提示',
                    content: `是否确认取消发布该市场活动？`,
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "classname": "webmanage.saletool.orderclue.ordercluecampaign",
                            method: "undercarriage",
                            "content": {
                                "sat_campaignids": [this.data.detail.sat_campaignid]
                            }
                        }).then(res => {
                            wx.showToast({
                                title: res.msg == '成功' ? `已取消该活动` : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') this.getDetail()
                        })
                    }
                })
                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/pages/group/select?data=${JSON.stringify({
                             ownertable:"sat_campaign",
                             ownerid:data.sat_campaignid,
                        })}&radio=true&principal=true`,
                })
                break;
        }
    },
    /* 更换负责人 */
    handelSubmit(arr) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认更换负责人',
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220930103701,
                    "content": {
                        ownertable: "sat_campaign",
                        ownerid: that.data.detail.sat_campaignid,
                        userid: arr[0]
                    }
                }).then(res => {
                    console.log("更换负责人", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    wx.showToast({
                        title: '更换成功！',
                        icon: "none"
                    });
                    setTimeout(() => {
                        that.getDetail();
                        wx.navigateBack();
                    }, 300)
                })
            }
        })
    },
    onUnload() {
        const page = getCurrentPages().find(v => v.__route__ == 'packageA/activity/index');
        if (!page) return;
        let content = JSON.parse(JSON.stringify(page.data.content));
        content.pageSize = (content.pageNumber - 1) * content.pageSize;
        content.pageNumber = 1;
        _Http.basic({
            id: '20221101095102',
            content
        }).then(res => {
            console.log("更新市场活动列表", res);
            if (res.msg == '成功') page.setData({
                list: res.data,
                "content.total": res.total
            })
        })
    }
})