const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 0, //tabs 选中项
        sa_customersid: 0,
        detail: {}, //详情数据
        briefs: [], //简介列表
        tabsList: [{
            label: "跟进动态",
            num: 132
        }, {
            label: "详细信息",
            num: 132
        }, {
            label: "客户地址",
            num: 132
        }, {
            label: "联系人",
            num: 132
        }, {
            label: "客户财务信息",
            num: 132
        }],
        list1: [], //基本信息
        list2: [], //系统信息
        tabbarList: [{
            icon: "icon-genjin",
            label: "跟进"
        }, {
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-zhuanyi",
            label: "更换负责人"
        }, {
            icon: "icon-shanchu",
            label: "作废"
        }]
    },
    onLoad(options) {
        this.setData({
            sa_customersid: options.id
        })
        this.getDetail();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail;
        switch (detail.label) {
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_customers&ownerid=${data.sa_customersid}`,
                })
                break;
            case "编辑":
                wx.navigateTo({
                    url: `/packageA/setclient/addAndEditor?data=${JSON.stringify(data)}`,
                })
                break;
            case "作废":
                wx.navigateTo({
                    url: `/packageA/setclient/delete?item=${JSON.stringify({
                        "sa_customersids": [this.data.detail.sa_customersid],
                        datastatus: 1, //（0:正常；1:作废；2:锁定）
                    })}`,
                })
                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/packageA/group/select?data=${JSON.stringify({
                         ownertable:"sa_customers",
                         ownerid:this.data.sa_customersid,
                    })}&radio=true&principal=true`,
                })
                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_customers",
                        ownerid: that.data.sa_customersid,
                        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/setclient/index'].includes(v.__route__)) v.getList(true)
                        })
                        wx.navigateBack();
                    }, 300)
                })
            }
        })
    },
    /* 获取详情 */
    getDetail() {
        /* 基本信息 */
        _Http.basic({
            "id": 20221012164302,
            "content": {
                sa_customersid: this.data.sa_customersid
            },
        }).then(res => {
            console.log("详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            /* 摘要信息 */
            let briefs = [{
                label: "客户级别",
                value: res.data.grade
            }, {
                label: "客户类型",
                value: res.data.type
            }, {
                label: "上级企业",
                value: res.data.superiorenterprisename
            }];
            /* 基本信息 */
            let list1 = [{
                label: "客户名称",
                value: res.data.enterprisename
            }, {
                label: "客户编号",
                value: res.data.sa_customersid
            }, {
                label: "客户级别",
                value: res.data.grade
            }, {
                label: "客户类型",
                value: res.data.type
            }, {
                label: "上级企业",
                value: res.data.superiorenterprisename
            }, {
                label: "分配状态",
                value: ""
            }, {
                label: "锁定状态",
                value: (function () {
                    if (res.data.datastatu == 0) {
                        return "正常"
                    } else {
                        return res.data.datastatu == 1 ? "作废" : "锁定"
                    }
                })(),
            }, {
                label: "来源",
                value: res.data.source
            }];
            /* 系统信息 */
            let list2 = [{
                label: "创建人",
                value: res.data.createby
            }, {
                label: "创建时间",
                value: res.data.createdate
            }, {
                label: "成交状态",
                value: res.data.tradingstatus
            }];
            this.setData({
                detail: res.data,
                briefs,
                list1,
                list2
            });
            //负责人
            _Http.basic({
                "id": 20220930103501,
                "content": {
                    "ownertable": "sa_customers",
                    "ownerid": this.data.sa_customersid
                }
            }).then(s => {
                console.log("负责人", s)
                if (s.data.length != 0) {
                    let list = list1.concat([{
                        label: "负责人",
                        value: s.data[0].teamleader[0].name
                    }, {
                        label: "负责人部门",
                        value: s.data[0].teamleader[0].position
                    }])
                    list[5].value = s.data.length > 0 ? '已分配' : '未分配';
                    this.setData({
                        principal: s.data,
                        list1: list,
                        briefs: this.data.briefs.concat([{
                            label: "负责人",
                            value: s.data[0].teamleader[0].name
                        }, {
                            label: "最近跟进时间",
                            value: s.data[0].teamleader[0].changedate
                        }])
                    });
                }
            });
            //系统信息
            _Http.basic({
                "id": 20221019091602,
                "content": {
                    "sa_customersid": this.data.sa_customersid
                }
            }).then(s => {
                console.log("系统信息", s)
                let list = this.data.list2.concat([{
                    label: "转手次数",
                    value: s.data.changecount
                }, {
                    label: "最近编辑人",
                    value: s.data.changeby
                }, {
                    label: "最近编辑时间",
                    value: s.data.changedate
                }, {
                    label: "最近跟进人",
                    value: s.data.followby
                }, {
                    label: "最近跟进时间",
                    value: s.data.followdate
                }, {
                    label: "客户资料完善度",
                    value: s.data.tradingstatus
                }])
                this.setData({
                    list2: list
                })
            })
            //获取标签
            this.getTags();
            this.getGroup()
            this.partialRenewal();
        });
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        const id = this.data.detail.sys_enterpriseid;
        let model = '';
        let name = this.data.tabsList[this.data.tabsActive].label;
        switch (name) {
            case "跟进动态":
                model = "#Trace"
                break;
            case "客户地址":
                model = "#Address"
                break;
            case "联系人":
                model = "#Contacts"
                break;
            case "客户财务信息":
                model = "#Financing"
                break;
            default:
                break;
        };
        if (model) {
            // 确定好模块ID total = null 是第一次加载 加载数据
            // init是新增或修改需要清数据
            let Component = this.selectComponent(model);
            const {
                total,
                pageNumber,
                pageTotal
            } = Component.data.content;
            if (total == null || init) {
                console.log(1)
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                console.log(2)
                Component.getList(id, false);
            } else {
                //用来判断 在搜索页面修改，与tabs选项不一致 但是切换到该选项 重置数据

            }
        }
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onShareAppMessage() {}
})