const _Http = getApp().globalData.http;
let downCount = null;
Page({
    data: {
        userMsg: {},
        pathList: [], //功能权限
        teamAuth: '', //团队管理权限列表 JSON字符串 用于传递
    },
    onLoad(options) {
        this.queryUserMsg();
        getApp().globalData.Language.getLanguagePackage(this)
        let authlist = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['个人中心']);
        let pathList = [{
            name: "修改登录密码",
            icon: "icon-a-wodeguanyuyingyong",
            color: "var(--warning)",
            path: `/pages/tabbar/mine/changePassword/index`
        }]
        if (authlist.length) {
            authlist[0].apps.forEach(v => {
                switch (v.name) {
                    case "teamManagement":
                        pathList.unshift({
                            name: "团队管理",
                            icon: "icon-a-wodetuanduiguanli",
                            color: "var(--assist)",
                            path: `/${v.path}`
                        })
                        this.setData({
                            teamAuth: JSON.stringify(v.meta.auth)
                        })
                        break;
                }
            });
            this.setData({
                pathList
            })
        };
        this.setData({
            auth: JSON.stringify(wx.getStorageSync('auth')),
            userMsg: JSON.stringify(wx.getStorageSync('userMsg')),
            site: JSON.stringify(wx.getStorageSync('siteP')),
            templetList: JSON.stringify(wx.getStorageSync('templetList')),
            languagecode: wx.getStorageSync('languagecode'),
            isnewsale: getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['CRM'], ['销售管理'])[0].apps.some(v => v.name == "newsale")
        })
    },
    /* 查询用户信息 */
    queryUserMsg() {
        _Http.basic({
            "classname": "common.usercenter.usercenter",
            "method": "queryUserMsg",
            "content": {
                "nocache": true
            }
        }).then(res => {
            console.log("查询用户信息", res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                userMsg: res.data
            })
        })
    },
    onShow() {
        this.getTabBar().init();
        if (this.data.isnewsale) _Http.basic({
            "id": 2025030613432602,
            "content": {},
            "languagecode": "ZH",
        }).then(res => {
            console.log("获取订单数量", res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            let list = [{
                key: "新建",
                icon: "icon-xinjian"
            }, {
                key: "提交",
                icon: "icon-tijiao1"
            }, {
                key: "交期待确认",
                icon: "icon-jiaoqidaiqueren"
            }, {
                key: "交期确认",
                icon: "icon-jiaoqiqueren"
            }, {
                key: "审核",
                icon: "icon-shenhe1"
            }]
            if (wx.getStorageSync('userMsg').siteid == 'HY') list.splice(1, 0, {
                key: "预提交",
                icon: "icon-yutijiao"
            })
            this.setData({
                orders: list.map(v => {
                    v.badge = res.data[v.key]
                    return v
                })
            })
        })
    },
    /* 退出登录 */
    outLogin() {
        clearTimeout(downCount);
        wx.showLoading({
            title: getApp().globalData.Language.getMapText('加载中') + '...',
        })
        downCount = setTimeout(() => {
            _Http.logout().then(res => {
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('退出成功')
                });
                let loginMsg = wx.getStorageSync("loginMsg"),
                    languagecode = wx.getStorageSync("languagecode"),
                    logins = wx.getStorageSync("logins");

                wx.clearStorageSync();
                wx.setStorageSync('loginMsg', loginMsg)
                wx.setStorageSync('logins', logins)
                wx.setStorageSync('isAgree', true)
                wx.removeStorageSync('languagecode');
                wx.setStorageSync('languagecode', languagecode)
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/login/phone',
                    })
                }, 300)
            })
        }, 300);
    },
    /* 去修改用户信息 */
    changeUserMsg() {
        let {
            name,
            phonenumber,
            attinfos,
            hr,
            accountno
        } = this.data.userMsg;
        wx.navigateTo({
            url: `./userMsg/index?attinfos=${JSON.stringify(attinfos)}&name=${name}&phonenumber=${phonenumber}&email=${hr.email}&accountno=${accountno}`
        })
    },
    /* 绑定或解绑微信 */
    bindingWechat(e) {
        if (this.data.userMsg.iswechatbinding) {
            let that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否解除绑定'),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                success: (res) => {
                    if (res.confirm) that.handleBDWechat(0);
                }
            })
        } else {
            this.handleBDWechat(1);
        }
    },
    handleBDWechat(isbinging) {
        let that = this;
        wx.getUserProfile({
            desc: getApp().globalData.Language.getMapText('用于完善用户资料'),
            success: ({
                userInfo
            }) => {
                wx.login({
                    success(res) {
                        if (res.code) _Http.basic({
                            "classname": "common.usercenter.usercenter",
                            "method": "WechatBinding",
                            content: {
                                "wechat_code": res.code,
                                isbinging, // 0解绑 1绑定
                                wechatuserinfo: userInfo,
                                "appid": "wx197f219a82a89d7b"
                            }
                        }).then(s => {
                            console.log("微信绑定", isbinging, s)
                            if (s.code != '1') return wx.showToast({
                                title: s.data,
                                icon: "none"
                            });
                            setTimeout(() => {
                                wx.showToast({
                                    title: isbinging == 0 ? getApp().globalData.Language.getMapText('解绑成功') : getApp().globalData.Language.getMapText('绑定成功'),
                                    icon: "none"
                                })
                            }, 100);
                            that.queryUserMsg();
                        })
                    }
                })
            },
            fail: () => {
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('未获取授权'),
                    icon: "none"
                })
            }
        })
    },
    /* 前去查询 */
    bindingOfficialAccounts() {
        wx.navigateTo({
            url: './webView',
        })
    }
})