const _Http = getApp().globalData.http;
Page({
    data: {
        tabsActive: 0, //tabs 选中项
        sa_customersid: 0,
        detail: {}, //详情数据
        briefs: [], //简介列表
        tabsList: [{
                label: "详细信息",
                icon: "icon-tabxiangxixinxi1"
            }, {
                label: "跟进动态",
                icon: "icon-tabgenjinjilu",
                model: "#Trace"
            }, {
                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"
            }
        ],
        list1: [], //基本信息
        list2: [], //系统信息
        tabbarList: []
    },
    onLoad(options) {
        this.setData({
            sa_customersid: options.id,
            isAdmin: wx.getStorageSync('auth').wPublicCustomer.options.some(v => v == "admin"), //是否具有管理权限
            options: wx.getStorageSync('auth').wPublicCustomer.options, //权限列表
        })
        this.getDetail();
    },
    /* 分配 */
    handleSelectContacts({
        value
    }) {
        let that = this;
        wx.showModal({
            title: '提示',
            content: `是否确认将"${this.data.detail.enterprisename}"分配给"${value[0]}"?`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20221014165502,
                    "content": {
                        "userid": value[1][0],
                        "sa_customersids": [that.data.detail.sa_customersid]
                    },
                }).then(res => {
                    console.log("分配公海客户", res)
                    wx.showToast({
                        title: res.msg == '成功' ? '分配成功' : res.msg,
                        icon: "none"
                    });
                    if (res.msg == '成功') setTimeout(() => {
                        that.handleInit()
                        wx.navigateBack();
                    }, 300)
                })
            }
        })
    },
    /* 领取/分配后初始化数据 */
    handleInit() {
        // this.getDetail();
        let page = getCurrentPages().find(v => v.__route__ == 'packageA/publicCustomer/index');
        page.setData({
            list: page.data.list.filter(v => v.sa_customersid != this.data.detail.sa_customersid),
            'content.total': page.data.content.total - 1
        })
        wx.redirectTo({
            url: '/packageA/setclient/detail?id=' + this.data.detail.sa_customersid,
        })
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail;
        switch (detail.label) {
            case "领取":
                let that = this;
                wx.showModal({
                    title: '提示',
                    content: `是否确认领取"${that.data.detail.enterprisename}?"`,
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "id": 20221014160202,
                            "content": {
                                "sa_customersid": that.data.detail.sa_customersid
                            },
                        }).then(res => {
                            console.log("领取公海客户", res)
                            wx.showToast({
                                title: res.msg == '成功' ? '领取成功' : res.msg,
                                icon: "none",
                                mask: true
                            });
                            if (res.msg == '成功') setTimeout(() => {
                                that.handleInit();
                            }, 500)
                        })
                    }
                })
                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]] : "";
                data.sa_customerpoolid = data.sourcepoolname ? [data.sourcepoolname, [data.sa_customerpoolid]] : "";
                wx.navigateTo({
                    url: `/packageA/publicCustomer/addAndEditor?data=${JSON.stringify(data)}`,
                })
                break;
            case "删除":
                wx.showModal({
                    title: '提示',
                    content: `是否确定删除“${data.enterprisename}”`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            id: 20221010164402,
                            content: {
                                "sa_customersids": [data.sa_customersid],
                                datastatus: 1, //（0:正常；1:删除；2:锁定）
                            }
                        }).then(res => {
                            console.log("删除公海客户", res)
                            wx.showToast({
                                title: res.msg == '成功' ? '删除成功' : res.msg,
                                icon: "none",
                                mask: true
                            });
                            if (res.msg == '成功') setTimeout(() => {
                                let page = getCurrentPages().find(v => v.__route__ == 'packageA/publicCustomer/index');
                                if (page) page.setData({
                                    list: page.data.list.filter(v => v.sa_customersid != data.sa_customersid),
                                    "content.total": page.data.content.total - 1
                                });
                                wx.navigateBack()
                            }, 500)
                        })
                    }
                })
                break;
            case "分配":
                wx.navigateTo({
                    url: '/packageA/select/contacts/select?params=' + JSON.stringify({
                        "id": 20221017094102,
                        "content": {
                            nacache: true,
                            "sa_customerpoolid": this.data.detail.sa_customerpoolid
                        },
                    }) + '&radio=true&idname=userid',
                })
                getApp().globalData.handleSelect = this.handleSelectContacts.bind(this);
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 获取详情 */
    getDetail() {
        /* 基本信息 */
        _Http.basic({
            "id": 20221012164302,
            "content": {
                nocache: true,
                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.partialRenewal();
        });
    },
    setPreview(res) {
        /* 摘要信息 */
        let briefs = [{
            label: "公海池:",
            value: res.data.poolname
        }, {
            label: "客户类型",
            value: res.data.type
        }, {
            label: "企业等级",
            value: res.data.grade
        }, {
            label: "上级企业",
            value: res.data.superiorenterprisename
        }];
        /* 基本信息 */
        let list1 = [{
            label: "客户名称",
            value: res.data.enterprisename
        }, {
            label: "客户编号",
            value: res.data.sa_customersid
        }, {
            label: "erp编号",
            value: res.data.erpagentnum
        }, {
            label: "公海池",
            value: res.data.poolname
        }, {
            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
        }];
        /* 系统信息 */
        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
        }];
        let tabbarList = [{
            icon: "icon-genjin",
            label: "跟进"
        }];
        //公海客户可领取并且拥有领取客户的权限
        if (res.data.freeget == 1 && this.data.options.some(v => v == 'getCustomer')) tabbarList.unshift({
            icon: "icon-paixushaixuanxuanzhong",
            label: "领取"
        })
        if (this.data.isAdmin) tabbarList = tabbarList.concat([{
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-shanchu",
            label: "删除"
        }]);
        //拥有分配权限或管理员权限
        if (this.data.options.some(v => v == 'allot') || this.data.isAdmin) tabbarList.splice(0, 0, {
            icon: "icon-a-tuiguangsucailaxinliang",
            label: "分配"
        })
        this.setData({
            detail: res.data,
            briefs,
            list1,
            list2,
            tabbarList
        });

    },
    //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();
    },
    onReachBottom() {
        this.partialRenewal();
    }
})