// pages/tabber/my/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnInfo: null,
        menuList: [
            {
                icon: '/image/my/youhuiquan.png',
                text: '优惠券',
                url: '/pages/my/coupon/coupon'
            },
            // {
            //     icon: '/image/my/fenxianghaoyou.png',
            //     text: '分享好友'
            // },
             {
                icon: '/image/my/dizhiguanli.png',
                text: '地址管理',
                url: '/pages/my/address/address'

            },
            //  {
            //     icon: '/image/my/fuwuliucheng.png',
            //     text: '服务流程'
            // }, {
            //     icon: '/image/my/yonghuxieyi.png',
            //     text: '用户协议'
            // },
            // {
            //     icon: '/image/my/lipeixize.png',
            //     text: '理赔细则'
            // },
             {
                icon: '/image/my/kefurexian.png',
                text: '客服热线',
                url:""
            }
        ],
        active: true,
        userInfo: "",
        hasUserInfo: false,
        code: "",
        yue:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       
        this.getUser();
      
        if (this.data.active == true) {
            this.data.menuList.shift()
            this.setData({
                code: wx.getStorageSync('phone'),
                menuList: this.data.menuList
            })

        }
        // this.getwallet();
    },
onShow(){
    this.getBtnInfo()
    this.getwallet();
},
    getBtnInfo() {
        this.setData({
            btnInfo: app.globalData.btnInfo
        })
    },

    toUrl(e) {
        let url = e.currentTarget.dataset.url
        wx.navigateTo({
            url,
        })
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {

                // this.setData({
                //     userInfo: res.userInfo,
                //     hasUserInfo: true
                // })
                this.updateUser(res.userInfo);

             
            }
        })
    },
    getPhoneNumber(e) {
        let that = this
        console.log(e)
        that.setData({
            code: e.detail.code,
        })
        var data = {
            'mds': wx.getStorageSync('seisson_key'),
            'encryptedData': e.detail.encryptedData,
            'errMsg': e.detail.errMsg,
            'iv': e.detail.iv,
            'appid': wx.getAppBaseInfo().host.appId,
        }

        app.api.useApi(app.globalData.baseAppUrl+"api/user/getPhoneNumber",data,"post").then((res)=>{
            if (res.code == 200) {
                var phone = res.message.data.phone
                console.log(phone);
                if (phone) {
                    that.setData({
                        code: phone,
                    })
                }
                that.getUser()
            }
        }).catch((err)=>{
            console.log(err)
        })
    },

    getwallet() {
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/wallet", {
        }, "get").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 2000);

            } else {
                console.log(res.message.data);
                that.setData({
                    yue: res.message.data.balance,
                })
            }

        }).catch(function (err) {
            console.log(222);
        })

    },


    updateUser(userinfo){
        wx.showLoading({
          title: '更新用户信息中',
        })
        var that=this;
        var userinfo=userinfo;
        app.api.useApi(app.globalData.baseAppUrl+"api/user",userinfo,"post").then((res)=>{
            if(res.code==200){
                wx.hideLoading();
                that.getUser()
            }else{
                that.updateUser(userinfo)
            }
        }).catch((err)=>{
            that.updateUser(userinfo)
        })
    },

    /**
     * 获取用户信息
     */
    getUser(){
        var that=this;
        app.api.useApi(app.globalData.baseAppUrl+"api/user/getUser","","get").then((res)=>{
            if(res.code==200){
                wx.hideLoading();
                that.setData({
                    userInfo:res.message.data,
                    hasUserInfo:res.message.data.avatar!=""?true:false
                })
            }else{
                that.getUser()
            }
        }).catch((err)=>{
            that.getUser()
        })
    }


    
})