const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 0, //tabs 选中项
        sa_projectid: "",
        tabsList: [{
            label: "详细信息",
            num: 132
        }, {
            label: "阶段任务",
            num: 132
        }, {
            label: "跟进动态",
            num: 132
        }, {
            label: "关联客户",
            num: 132
        }, {
            label: "联系人",
            num: 132
        }],
        tabbarList: [{
            icon: "icon-genjin",
            label: "报备"
        }, {
            icon: "icon-genjin",
            label: "跟进"
        }, {
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-zhuanyi",
            label: "更换负责人"
        }, {
            icon: "icon-shanchu",
            label: "作废"
        }],
    },
    onLoad(options) {
        this.setData({
            sa_projectid: options.sa_projectid
        });
        this.getDetail()
    },
    getDetail() {
        _Http.basic({
            "id": 20221021103902,
            "content": {
                "sa_projectid": this.data.sa_projectid
            },
        }).then(res => {
            console.log("项目商机详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                detail: res.data,
                briefs: [{
                    label: "项目编号",
                    value: res.data.projectnum
                }, {
                    label: "项目地址",
                    value: res.data.province + res.data.city + res.data.county + res.data.address
                }, {
                    label: "项目类型",
                    value: res.data.projecttype
                }, {
                    label: "负责人",
                    value: res.data.name
                }, {
                    label: "当前项目阶段",
                    value: res.data.stagename
                }, {
                    label: "赢率",
                    value: res.data.winrate
                }, {
                    label: "最近跟进时间",
                    value: res.data.followdate
                }],
                list1: [{
                    label: "项目名称",
                    value: res.data.projectname
                }, {
                    label: "项目编号",
                    value: res.data.projectnum
                }, {
                    label: "项目类型",
                    value: res.data.projecttype
                }, {
                    label: "省市县",
                    value: res.data.province ? res.data.province + res.data.city + res.data.county : ''
                }, {
                    label: "详细地址",
                    value: res.data.address
                }, {
                    label: "项目等级",
                    value: res.data.grade
                }, {
                    label: "当前阶段",
                    value: res.data.stagename
                }, {
                    label: "项目报备时间",
                    value: res.data.createdate
                }, {
                    label: "项目规模",
                    value: res.data.scale
                }, {
                    label: "项目状态",
                    value: res.data.status
                }, {
                    label: "项目预算",
                    value: res.data.budgetary
                }, {
                    label: "预计签约金额",
                    value: res.data.signamount_due
                }, {
                    label: "预计签约月份",
                    value: res.data.signdate_due
                }, {
                    label: "赢率",
                    value: res.data.winrate
                }, {
                    label: "负责人",
                    value: res.data.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.followby
                }, {
                    label: "最近跟进时间",
                    value: res.data.followdate
                }, {
                    label: "转手次数",
                    value: res.data.changecount
                }, {
                    label: "报备人",
                    value: res.data.reportby
                }, {
                    label: "报备时间",
                    value: res.data.reportdate
                }, {
                    label: "报备审核通过时间",
                    value: res.data.reportcheckdate
                }]
            })
            //更新列表赢率
            getCurrentPages().forEach(v => {
                if (v.__route__ == 'packageA/project/index') {
                    let i = v.data.list.findIndex(e => e.sa_projectid == this.data.sa_projectid);
                    if (i != -1) v.setData({
                        [`list[${i}].winrate`]: parseFloat(res.data.winrate.substring(0, res.data.winrate.length - 1))
                    })
                }
            })
            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) {
        const id = this.data.detail.sa_projectid;
        let model = '';
        let name = this.data.tabsList[this.data.tabsActive].label;
        switch (name) {
            case "跟进动态":
                model = "#Trace"
                break;
            case "阶段任务":
                model = '#Task'
                break;
            case "关联客户":
                model = "#Treaty"
                break;
            case "联系人":
                model = "#Contacts"
                break;
        };
        if (model) {
            // 确定好模块ID total = null 是第一次加载 加载数据
            // init是新增或修改需要清数据
            let Component = this.selectComponent(model);
            const {
                total,
                pageNumber,
                pageTotal
            } = Component.data.content;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            } else {
                //用来判断 在搜索页面修改，与tabs选项不一致 但是切换到该选项 重置数据
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail;
        switch (detail.label) {
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_project&ownerid=${data.sa_projectid}`,
                })
                break;
            case "编辑":
                wx.navigateTo({
                    url: `/packageA/project/addAndEdit?data=${JSON.stringify(data)}`,
                })
                break;
            case "作废":
                wx.navigateTo({
                    url: `/packageA/project/delete?id=${this.data.detail.sa_projectid}`,
                })
                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/packageA/group/select?data=${JSON.stringify({
                             ownertable:"sa_project",
                             ownerid:data.sa_projectid,
                        })}&radio=true&principal=true`,
                })
                break;
            case "报备":
                console.log(detail)
                _Http.basic({
                    "id": 20221114163402,
                    "content": {
                        "sa_projectid": data.sa_projectid,
                        "tag": "报备中"
                    }
                }).then(res => {
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    this.getTags();
                    setTimeout(() => {
                        wx.showToast({
                            title: '已提交报备!',
                            icon: "none"
                        })
                    }, 200)
                })
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 更换负责人 */
    handelSubmit(arr) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认更换负责人',
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220930103701,
                    "content": {
                        ownertable: "sa_project",
                        ownerid: that.data.detail.sa_projectid,
                        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();
                        getCurrentPages().forEach(v => {
                            if (['packageA/project/index'].includes(v.__route__)) v.getList(true)
                        })
                        wx.navigateBack();
                    }, 300)
                })
            }
        })
    },
    onShareAppMessage() {

    }
})