const _Http = getApp().globalData.http;
Page({
    data: {
        isLeader: false, //是否为负责人
        tabsActive: 1, //tabs 选中项
        sa_customersid: 0,
        tabsList: [{
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        }, {
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "线索",
            icon: "icon-tabxiansuo",
            model: "#Clue"
        }, {
            label: "项目商机",
            icon: "icon-tabxiangmu",
            model: "#Project"
        }, {
            label: "客户地址",
            icon: "icon-tabdizhi",
            model: "#Address"
        }, {
            label: "联系人",
            icon: "icon-tablianxiren",
            model: "#Contacts"
        }, {
            label: "报价单",
            icon: "icon-tabbaojiadan",
            model: "#Offers"
        }, {
            label: "合同",
            icon: "icon-tabhetong",
            model: "#Contract"
        }, {
            label: "账户余额",
            icon: "icon-tabkaipiaoxinxi",
            model: "#Account"
        }, {
            label: "银行卡信息",
            icon: "icon-tabchanpin",
            model: "#Bankcard"
        }, {
            label: "开票信息",
            icon: "icon-tabbaojiadan",
            model: "#Financing"
        }, {
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        }, {
            label: "操作",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }],
    },
    onLoad(options) {
        this.setData({
            sa_customersid: options.id,
            isAdmin: wx.getStorageSync('auth').wCustomer.options.some(v => v == "admin"), //是否具有管理权限
            options: wx.getStorageSync('auth').wCustomer.options, //权限列表
        })
        this.getDetail();

    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail,
            that = this;
        switch (detail.label) {
            case "退回":
                wx.showModal({
                    title: '提示',
                    content: `是否确定将${data.enterprisename}退回到公海客户`,
                    complete: (res) => {
                        console.log(data)
                        if (res.confirm) _Http.basic({
                            "id": 20221014102602,
                            "content": {
                                "sa_customerpoolid": data.sa_customerpoolid_source,
                                "sa_customersid": data.sa_customersid
                            }
                        }).then(res => {
                            console.log("公海线索退回", res)
                            wx.showToast({
                                title: res.msg == '成功' ? '退回成功' : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') setTimeout(() => {
                                let page = getCurrentPages().find(v => v.__route__ == 'packageA/setclient/index');
                                if (page) page.setData({
                                    list: page.data.list.filter(v => v.sa_customersid != data.sa_customersid)
                                });
                                wx.redirectTo({
                                    url: '/packageA/publicCustomer/detail?id' + data.sa_customersid,
                                })
                            }, 300)
                        })
                    }
                });
                break;
            case "回收":
                wx.navigateTo({
                    url: `/packageA/publicCustomer/pond/index?params=${JSON.stringify({
                        "id": 20221206195102,
                        "content": {
                            nocache:true,
                            "where": {
                                "condition": ""
                            }
                        }
                    })}`,
                })
                getApp().globalData.handleSelect = this.handleTransfer.bind(this);
                break;
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_customers&ownerid=${data.sa_customersid}`,
                })
                break;
            case "编辑":
                data.parentid = data.superiorenterprisename ? [data.superiorenterprisename, [data.parentid]] : "";
                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: `/pages/group/select?data=${JSON.stringify({
                         ownertable:"sa_customers",
                         ownerid:this.data.sa_customersid,
                    })}&radio=true&principal=true`,
                })
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 处理回收客户 */
    handleTransfer({
        value
    }) {
        wx.showModal({
            title: '提示',
            content: `是否确定将客户回收至${value[0]}?`,
            complete: (res) => {
                if (res.confirm) _Http.basic({
                    "id": 20221014165602,
                    "content": {
                        "sa_customerpoolid": value[1][0], //公海池id
                        "sa_customersids": [this.data.detail.sa_customersid],
                        "sa_customersid": this.data.detail.sa_customersid
                    },
                }).then(res => {
                    console.log("回收客户", res)
                    wx.showToast({
                        title: res.data == '成功' ? `回收成功` : res.msg,
                        icon: "none"
                    });
                    if (res.data == '成功') setTimeout(() => {
                        wx.navigateBack()
                        let page = getCurrentPages().find(v => v.__route__ == 'packageA/setclient/index');
                        if (page) page.setData({
                            list: page.data.list.filter(v => v.sa_customersid != this.data.detail.sa_customersid)
                        });
                        wx.redirectTo({
                            url: '/packageA/publicCustomer/detail?id=' + this.data.detail.sa_customersid,
                        })
                    }, 300)
                })
            }
        })
    },
    /* 更换负责人 */
    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"
            })
            this.setPreview(res);
            //获取标签
            this.getTags();
            this.getGroup()
            this.partialRenewal();
        });
    },
    setPreview(res) {
        /* 摘要信息 */
        let briefs = [{
            label: "客户类型",
            value: res.data.type
        }, {
            label: "企业等级",
            value: res.data.grade
        }, {
            label: "上级企业",
            value: res.data.superiorenterprisename
        }, {
            label: "负责人",
            value: res.data.leader.map(v => v.name) || ""
        }, {
            label: "归属经销商",
            value: res.data.agentname
        }];
        /* 基本信息 */
        let list1 = [{
            label: "客户名称",
            value: res.data.enterprisename
        }, {
            label: "客户编号",
            value: res.data.sa_customersid
        }, {
            label: "erp编号",
            value: res.data.erpagentnum
        }, {
            label: "企业简称",
            value: res.data.abbreviation
        }, {
            label: "客户类型",
            value: res.data.type
        }, {
            label: "企业等级",
            value: res.data.grade
        }, {
            label: "客户分类",
            value: res.data.customergrade
        }, {
            label: "客户行业",
            value: res.data.industry
        }, {
            label: "税号",
            value: res.data.taxno
        }, {
            label: "法人",
            value: res.data.contact
        }, {
            label: "电话",
            value: res.data.telephone
        }, {
            label: "省市县",
            value: res.data.province ? [res.data.province, res.data.city, res.data.county] : ""
        }, {
            label: "详细地址",
            value: res.data.address
        }, {
            label: "上级企业",
            value: res.data.superiorenterprisename
        }, {
            label: "客户来源",
            value: res.data.source
        }, {
            label: "来源公海",
            value: res.data.sourcepoolname
        }, {
            label: "成交状态",
            value: res.data.tradingstatus
        }, {
            label: "成交次数",
            value: res.data.contact_signnum
        }, {
            label: "合作状态",
            value: res.data.status
        }, {
            label: "负责人",
            value: res.data.leader.map(v => v.name) || ""
        }, {
            label: "归属经销商",
            value: res.data.agentname
        }];
        /* 系统信息 */
        let 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.deletereason
        }, {
            label: "转手次数",
            value: res.data.leader[0] ? res.data.leader[0].leadernum : 0
        }];
        /* 底部功能 */
        let isAdmin = this.data.isAdmin,
            isLeader = res.data.leader.some(some => some.userid == wx.getStorageSync('userMsg').userid),
            tabbarList = [];
        //负责人或管理员基础功能
        if (isLeader || isAdmin) tabbarList = [{
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-zhuanyi",
            label: "更换负责人"
        }, {
            icon: "icon-shanchu",
            label: "作废"
        }];
        //来自公海
        if (res.data.ispublic == 1) {
            //查询到回收公海客户权限
            if (this.data.options.some(v => v == 'recycle')) tabbarList.push({
                icon: "icon-dibu-chehui",
                label: "回收"
            })
            //负责人
            if (isLeader) tabbarList.push({
                icon: "icon-dibu-chehui",
                label: "退回"
            })
        }
        this.setData({
            detail: res.data,
            briefs,
            list1,
            list2,
            tabbarList,
            isLeader
        });
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 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 = model == "#Clue" ? this.data.detail.sa_customersid : this.data.detail.sys_enterpriseid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber < pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onShareAppMessage() {}
})