const _Http = getApp().globalData.http;
Page({
    data: {
        loading: true,
        isLeader: false, //是否为负责人
        tabsActive: 1, //tabs 选中项
        sColors: getApp().globalData.sColors,
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '活动详情');
        getApp().globalData.handleClue = null;
        const appAuth = wx.getStorageSync('auth').wmarketing_activity;
        let tabsList = [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "活动线索",
            icon: "icon-tabxiansuo",
            model: "#Clue"
        }, {
            label: "操作",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }]
        if (appAuth.isdatafollowup) tabsList.push({
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        })
        if (appAuth.istask) tabsList.push({
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        })
        this.setData({
            sat_campaignid: options.id,
            isAdmin: appAuth.options.some(v => v == "admin"), //是否具有管理权限
            options: appAuth.options, //权限列表
            tabsList,
            appAuth
        });
        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.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                loading: false,
                detail: res.data,
                briefs: [{
                    label: "活动类型",
                    value: res.data.type
                }, {
                    label: "线索数",
                    value: res.data.ordercluecount || '0'
                }, {
                    label: "活动负责人",
                    value: res.data.director
                }, {
                    label: "状态",
                    value: getApp().globalData.Language.getMapText(res.data.status),
                    style:`color:${this.data.sColors[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: getApp().globalData.Language.getMapText(res.data.status),
                    style:`color:${this.data.sColors[res.data.status]}`
                }, {
                    label: "开始时间",
                    value: res.data.begdate
                }, {
                    label: "结束时间",
                    value: res.data.enddate
                }, {
                    label: "活动负责人",
                    value: res.data.director
                }, {
                    label: "负责人",
                    value: res.data.leader[0].name
                }],
                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
                }]
            });
            let appAuth = this.data.appAuth;
            if (appAuth.isdatatag) this.getTags();
            if (appAuth.isdatateam) this.getGroup();
            this.partialRenewal(true);
            this.setTabbarList()
        })
    },
    async setTabbarList() {
        let tabbarList = [],
            detail = this.data.detail,
            isLeader = detail.leader.length ? detail.leader.some(v => v.userid == wx.getStorageSync('userMsg').userid) : false,
            status = this.data.detail.status,
            editdataleader = isLeader ? 1 : 0;
        if (!isLeader) {
            let res = await getApp().agentOrNot("sat_campaign", this.data.sat_campaignid),
                data = res.code == '1' ? res.data : {
                    editable: 0,
                    editdataleader: 0
                }
            if (this.data.appAuth.isdatateam) isLeader = this.selectComponent("#Group").data.editable == 1;
            if (!isLeader) isLeader = data.editable == 1;
            editdataleader = data.editdataleader;
        }
        if (isLeader || this.data.isAdmin) {
            if (status == '新建') {
                tabbarList = [{
                    icon: "color-bianji",
                    label: "编辑"
                }, {
                    icon: "icon-dibu-chengjiao",
                    label: "发布"
                }]
            } else if (status == '发布') {
                tabbarList = [{
                    icon: "icon-dibu-chengjiao",
                    label: "取消发布"
                }, {
                    icon: "icon-dibu-jieshu",
                    label: "结束"
                }]
            };
            getApp().globalData.handleClue = this.isDelete.bind(this);
        }
        this.setData({
            tabbarList,
            isLeader
        })
    },
    /**
     * 是否可以删除活动
     * @param {number} total 数据数量
     */
    isDelete(total) {
        if (this.data.detail.status == '新建' && this.data.isLeader && total == 0) this.setData({
            tabbarList: this.data.tabbarList.concat([{
                icon: "icon-guanlian-shanchu",
                label: "删除"
            }])
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //更新标签
    getTags() {
        if (this.data.appAuth.isdatatag) this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList().then(this.setTabbarList)
    },
    //局部数据更新 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 (model == "#Files") init = true;
            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: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确认发布该市场活动？'),
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    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.code == '1' ? getApp().globalData.Language.getMapText('活动发布成功') : res.msg,
                                icon: "none"
                            });
                            if (res.code == '1') this.getDetail()
                        })
                    }
                })
                break;
            case "结束":
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确认结束该市场活动？'),
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    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.code == '1' ? getApp().globalData.Language.getMapText('已结束该活动') : res.msg,
                                icon: "none"
                            });
                            if (res.code == '1') this.getDetail()
                        })
                    }
                })
                break;
            case "取消发布":
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确认取消发布该市场活动') + `？`,
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    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.code == '1' ? getApp().globalData.Language.getMapText('已取消该活动') : res.msg,
                                icon: "none"
                            });
                            if (res.code == '1') this.getDetail()
                        })
                    }
                })
                break;
            case "删除":
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确认删除该市场活动') + `？`,
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            id: "20230329163504",
                            "content": {
                                "sat_campaignid": this.data.detail.sat_campaignid
                            }
                        }).then(res => {
                            wx.showToast({
                                title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') : res.msg,
                                icon: "none"
                            });
                            if (res.code == '1') setTimeout(() => {
                                wx.navigateBack()
                            }, 300)
                        })
                    }
                })
                break;
        }
    },
    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.code == '1') page.setData({
                list: res.data,
                "content.total": res.total
            })
        })
    }
})