// index.js
// 获取应用实例
const app = getApp()
const link = app.globalData.url

Page({
  data: {
    grids1: [{
      image: 'order',
      text: '我的订单'
    }, {
      image: 'user',
      text: '好友列表'
    }, {
      image: 'help',
      text: '帮助中心'
    }, {
      image: 'add',
      text: '邀请好友'
    }, {
      image: 'to-top',
      text: '手动同步订单'
    }, {
      image: 'customer-service',
      text: '联系客服'
    }, {
      image: 'setting',
      text: '设置'
    },
     {
      image: 'success',
      text: '提现'
    }, 
    //{
    //   image: 'eye',
    //   text: '我的足迹'
    // }, {
    //   image: 'scan',
    //   text: '扫一扫'
    // }, 
  ],
  grids2: [{
    image: 'order',
    text: '我的订单'
  }, {
    image: 'user',
    text: '好友列表'
  }, {
    image: 'help',
    text: '帮助中心'
  }, {
    image: 'add',
    text: '邀请好友'
  }, {
    image: 'to-top',
    text: '手动同步订单'
  }, {
    image: 'customer-service',
    text: '联系客服'
  }, {
    image: 'setting',
    text: '设置'
  },
   {
    image: 'success',
    text: '提现'
  }, 
  {
    image: 'phone',
    text: '管理员'
  }, 
  //{
  //   image: 'scan',
  //   text: '扫一扫'
  // }, 
],
    // grids2: [{
    //   image: 'cart',
    //   text: '我的购物车'
    // }, {
    //   image: 'help',
    //   text: '帮助中心'
    // }, {
    //   image: 'address',
    //   text: '地址管理'
    // }, {
    //   image: 'order',
    //   text: '我的订单'
    // }, {
    //   image: 'customer-service',
    //   text: '联系客服'
    // }, ],
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    var that = this
    var opneId = wx.getStorageSync('openId')
    var nickName = wx.getStorageSync('nickName')
    //var admin = wx.getStorageSync('admin')
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    //获取金额
    wx.request( { 
      url: link + "/v1/forward/lhf", 
      header: { 
       "Content-Type": "application/json",
       "function":"/v1/user/wallet/get"
      }, 
      method: "POST",
      data:  { openId: opneId }, 
      success (res) {
        console.log(res)
       
        that.setData({
          wallet: res.data.data.wallet
         })
        
      }
     }) 
     
     console.log(nickName)
    //  if (nickName == '' ){
      wx.request( { 
        url: link + "/v1/forward/lhf", 
        header: { 
         "Content-Type": "application/json",
         "function":"/v1/user/open.id/get"
        }, 
        method: "POST",
        data:  { openId: opneId }, 
        success (res) {
          console.log(res)
          var admin = false
          wx.setStorageSync('nickName', res.data.data.user.nickName)  
          wx.setStorageSync('userId', res.data.data.user.id) 
          if (res.data.data.user.admin == 1){
            admin = true
          }
          that.setData({
            userId: res.data.data.user.id,
            admin:admin
           })
        }
       }) 
    // } else{
    //   var userId = wx.getStorageSync('userId')
    //   console.log(userId)
    //   that.setData({
    //     userId: userId
    //    })
    // }


  },
  clickGrid(e) {
    var that = this
    console.log(e)
    if (e.detail.cell.text == "我的订单"){
      wx.navigateTo({
        url:"/pages/order/order"
      })
    } else if (e.detail.cell.text == "联系客服"){
      wx.showModal({
        title: '客服微信',
        content: '客服微信:tao1024_com,点击复制按钮,直接复制客服微信号,粘贴到微信中搜索添加客服.',
        showCancel: true,//是否显示取消按钮
        cancelText:"取消",//默认是“取消”
        //cancelColor:'skyblue',//取消文字的颜色
        confirmText:"复制",//默认是“确定”
        //confirmColor: 'skyblue',//确定文字的颜色
        success: function (res) {
           if (res.cancel) {
              //点击取消,默认隐藏弹框
           } else {
            wx.setClipboardData({
              data: 'tao1024_com',
              success (res) {
              //点击确定
              wx.showToast({
                title: '复制成功',//提示文字
                duration:2000,//显示时长
                mask:true,//是否显示透明蒙层,防止触摸穿透,默认:false  
                icon:'success', //图标,支持"success"、"loading"  
                success:function(){ },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
             })
              }
            })
           
  
           }
        },
        fail: function (res) { },//接口调用失败的回调函数
        complete: function (res) { },//接口调用结束的回调函数(调用成功、失败都会执行)
     })

    }
     else if (e.detail.cell.text == "提现") {
      wx.showToast({
        title: "结算金额:0元",
        icon: 'none'
      });

    } else if (e.detail.cell.text == "邀请好友"){
      console.log('邀请好友')
      wx.navigateTo({
        url:"/pages/friends/friends"
      })
    } else if (e.detail.cell.text == "好友列表"){
      wx.navigateTo({
        url:"/pages/friends-list/friends-list"
      })
    } else if (e.detail.cell.text == "管理员"){
      wx.navigateTo({
        url:"/pages/admin/admin"
      })
    }
    
    else {
       wx.showToast({
        title: "功能开发中...",
        icon: 'none'
      });
    }

   
  },
  getUserProfile(e) {
    var that = this
    var openId = wx.getStorageSync('openId')
    var nickName = wx.getStorageSync('nickName')
    console.log("'"+nickName+"'")
    console.log(openId)
    if (nickName == ''){
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
        success: (res) => {
          console.log(res)
          //更新user
          wx.request( { 
            url: link + "/v1/forward/lhf", 
            header: { 
             "Content-Type": "application/json",
             "function":"/v1/user/update/name"
            }, 
            method: "POST",
            data:  { openId: openId ,nickName:res.userInfo.nickName,avatarUrl:res.userInfo.avatarUrl}, 
            success (ress) {
              console.log(ress)
              wx.setStorageSync('nickName', res.userInfo.nickName)  
              
            }
           }) 
           that.clickGrid(e)
        }
      })
    } else{
      that.clickGrid(e)
    }
   

    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //   var openId = wx.getStorageSync('openId')
  //   return {
  //     title: '优惠券小程序',
  //     desc: '电商内部优惠券',
  //     path: 'pages/boutique/outique?openId=' + openId // 路径,传递参数到指定页面。
  //    }
  // }
})