const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 1, //tabs 选中项
        sa_salesforecastbillid: "",
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "项目产品清单",
            icon: "icon-tabchanpin",
            model: "#Project"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        }, {
            label: "操作记录",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }, {
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        }],
        tabbarList: [{
            icon: "icon-genjin",
            label: "跟进"
        }, {
            icon: "icon-guanlian-bianji",
            label: "添加项目"
        }, {
            icon: "icon-guanlian-fuzhi",
            label: "移除项目"
        }],
    },
    onLoad(options) {
        this.setData({
            sa_salesforecastbillid: options.sa_salesforecastbillid
        });
        this.getDetail(true);
    },
    getDetail(init = false) {
        _Http.basic({
            "id": 20220914104603,
            "version": 1,
            "content": {
                nocache: true,
                "sa_salesforecastbillid": this.data.sa_salesforecastbillid
            },
        }).then(res => {
            console.log("预测详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setPreview(res.data[0])
            if (init) this.getTags()
            this.partialRenewal(init);
        })
    },
    /* 设置显示项 */
    setPreview(data) {
        this.setData({
            detail: data,
            briefs: [{
                label: "提报类型",
                value: data.isrepeat == 1 ? '月度提报' : '单次提报'
            }, {
                label: "提报要求",
                value: data.remarks
            }, {
                label: "提报开始时间",
                value: data.periodstart
            }, {
                label: "提报截止时间",
                value: data.periodend
            }, {
                label: "预测金额",
                value: data.sumamount
            }, {
                label: "提报人",
                value: data.createby
            }, {
                label: "状态",
                value: data.status
            }],
            list1: [{
                label: "提报类型",
                value: data.isrepeat == 1 ? '月度提报' : '单次提报'
            }, {
                label: "提报要求",
                value: data.remarks
            }, {
                label: "提报开始时间",
                value: data.periodstart
            }, {
                label: "提报截止时间",
                value: data.periodend
            }, {
                label: "预测金额(元)",
                value: data.sumamount
            }, {
                label: "状态",
                value: data.status
            }],
            list2: [{
                label: "提报人",
                value: data.createby
            }, {
                label: "提报时间",
                value: data.createdate
            }, {
                label: "最近编辑人",
                value: data.changeby
            }, {
                label: "最近编辑时间",
                value: data.changedate
            }]
        });
        /* 更新列表数据 */
        let page = getCurrentPages().find(v => v.__route__ == 'packageA/salesForecasting/index');
        if (page) {
            let i = page.data.list.findIndex(v => v.sa_salesforecastbillid == this.data.sa_salesforecastbillid);
            if (i != -1) {
                page.data.list[i].status = data.status;
                page.data.list[i].amount = data.sumamount;
                page.setData({
                    [`list[${i}]`]: page.data.list[i]
                })
            }
        }
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    /* 添加项目 */
    addProject({
        list
    }) {
        let that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认添加${list.length}个项目`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    id: 20220913154403,
                    version: 1,
                    content: {
                        nocache: true,
                        sa_salesforecastmodelid: that.data.detail.sa_salesforecastmodelid,
                        sa_salesforecastbillid: that.data.sa_salesforecastbillid,
                        sa_projectids: list.map(v => {
                            return {
                                sa_projectid: v.sa_projectid,
                                discountrate: v.discountrate
                            }
                        })
                    }
                }).then(res => {
                    console.log("添加项目", res)
                    wx.showToast({
                        title: res.msg == '成功' ? '添加成功' : res.msg,
                        icon: "none"
                    });
                    if (res.msg == '成功') {
                        that.setData({
                            tabsActive: that.data.tabsList.findIndex(v => v.label == '项目产品清单')
                        });
                        setTimeout(() => {
                            wx.navigateBack();
                            setTimeout(() => {
                                that.selectComponent('#Project').getList(that.data.sa_salesforecastbillid, false, true);
                            }, 300)
                        }, 300)
                    }
                })
            }
        })
    },
    /* 删除项目 */
    deteleProject({
        item
    }) {
        let that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认移除“${item.projectname}”`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220906155103,
                    "version": 1,
                    "content": {
                        "sa_salesforecastid": 0,
                        "sa_projectid": item.sa_projectid,
                        "sa_salesforecastbillid": item.sa_salesforecastbillid
                    }
                }).then(res => {
                    console.log("移除项目", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    that.selectComponent("#Project").getList(that.data.sa_salesforecastbillid);
                    setTimeout(() => {
                        wx.showToast({
                            title: `已移除“${item.projectname}”`,
                            icon: "none"
                        })
                        let page = getCurrentPages().find(v => v.__route__ == 'packageA/select/project/select');
                        if (page) page.deteleItem(item.sa_projectid);
                        that.getDetail();
                    }, 500)
                })
            }
        })
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        switch (detail.label) {
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_salesforecastbill&ownerid=${this.data.sa_salesforecastbillid}`,
                })
                break;
            case "添加项目":
                wx.navigateTo({
                    url: `/packageA/select/project/select?params=${JSON.stringify({
                            "id": 20220906154803,
                            "version":1,
                            "content": {
                                nocache:true,
                                sa_salesforecastbillid:this.data.sa_salesforecastbillid,
                                "where":{
                                    "condition":""
                                }
                            }
                    })}`,
                });
                getApp().globalData.handleSelect = this.addProject.bind(this);
                break;
            case "移除项目":
                wx.navigateTo({
                    url: `/packageA/select/project/select?params=${JSON.stringify({
                        "id": "20220916115203",
                        "version": 1,
                        "content": {
                            nocache: true,
                            sa_salesforecastbillid:this.data.sa_salesforecastbillid,
                            pageSize: 999,
                            "where":{
                                "condition":""
                            }
                        }
                        })}&radio=true`,
                })
                getApp().globalData.handleSelect = this.deteleProject.bind(this);
                break;
        }
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //局部数据更新 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.sa_salesforecastbillid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber < pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    }
})