const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 4, //tabs 选中项
        sa_customersid: 0,
        detail: {}, //详情数据
        briefs: [], //简介列表
        tabsList: [{
            label: "跟进动态",
            num: 132
        }, {
            label: "详细信息",
            num: 132
        }, {
            label: "客户地址",
            num: 132
        }, {
            label: "联系人",
            num: 132
        }, {
            label: "客户财务信息",
            num: 132
        }],
        list1: [], //基本信息
        list2: [], //系统信息
    },
    onLoad(options) {
        this.setData({
            sa_customersid: options.id
        })
        this.getDetail();
    },
    /* 获取详情 */
    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
            }];
            /* 系统信息 */
            const list2 = [{
                label: "创建人",
                value: res.data.createby
            }, {
                label: "创建时间",
                value: res.data.createdate
            }, {
                label: "成交状态",
                value: res.data.tradingstatus
            }, {
                label: "转手次数",
                value: ""
            }, {
                label: "最近编辑人",
                value: ""
            }, {
                label: "最近编辑时间",
                value: ""
            }, {
                label: "最近跟进人",
                value: ""
            }, {
                label: "最近跟进时间",
                value: ""
            }, {
                label: "客户资料完善度",
                value: ""
            }];
            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("负责人", res)
                let list = list1.concat([{
                    label: "负责人",
                    value: s.data[0].name
                }, {
                    label: "负责人部门",
                    value: s.data[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].name
                    }, {
                        label: "最近跟进时间",
                        value: s.data[0].changedate
                    }])
                });

            });
            //获取标签
            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() {}
})