const app = require('../../utils/util.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    avatarUrl: '../../images/morenImg.png',
    tabArr: [{
        name: '我的信息',
        img: '../../images/icon1.png',
        url: '/pages/InfoType/InfoType'
      }, {
        name: '我的活动',
        img: '../../images/icon2.png',
        url: '/pages/myActive/myActive'
      }, {
        name: '心动0次',
        img: '../../images/icon3.png',
        url: ''
      }, {
        name: '被心动0次',
        img: '../../images/icon4.png',
        url: ''
      },
      // {
      //   name: '增加权益',
      //   img: '../../images/icon7.png',
      //   url: '/pages/addRights/addRights'
      // }, 
      {
        name: '推荐好友',
        img: '../../images/icon6.png',
        url: '/pages/share/share'
      },
      // {
      //   name: '我的订单',
      //   img: '../../images/icon8.png',
      //   url: '/pages/paylists/paylists'
      // }
    ]
  },
  goTab(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.src
    })
  },
  goIndex() {
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  onShareAppMessage: function (options) {
    console.log(options)
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: "月合文化", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/personal/personal', // 默认是当前页面，必须是以‘/’开头的完整路径
      imageUrl: '../../images/photo.jpg', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        console.log(res)
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      }
    }
    // 来自页面内的按钮的转发
    if (options.from == 'button') {
      var eData = options.target.dataset;
      console.log(eData.name); // shareBtn
      // 此处可以修改 shareObj 中的内容
      shareObj.path = '/pages/btnname/btnname?btn_name=' + eData.name;
    }
    // 返回shareObj
    return shareObj;
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo)
    this.setData({
      nick: e.detail.userInfo.nickName,
      avatarUrl: e.detail.userInfo.avatarUrl
    })
    this.upload(e.detail.userInfo.nickName, e.detail.userInfo.avatarUrl);
  },
  getNick() {
    // if (wx.getStorageSync('nick') && wx.getStorageSync('avatarUrl')) {
    //   this.setData({
    //     nick: wx.getStorageSync('nick'),
    //     avatarUrl: wx.getStorageSync('avatarUrl')
    //   })
    // } else {
    // wx.getSetting({
    //   success: (res) => {
    //     if (res.authSetting['scope.userInfo']) {
    //       wx.getUserInfo({
    //         success: (res) => {
    //           this.setData({
    //             nick: res.userInfo.nickName,
    //             avatarUrl: res.userInfo.avatarUrl
    //           })
    //           wx.setStorageSync('nick', res.userInfo.nickName)
    //           wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl)
    //         }
    //       })
    //     } else {
    //       console.log(res)
    //     }
    //   }
    // })
    // }
  },
  upload(nickName, avatar) {
    const form = {
      nickName: nickName,
      avatar: avatar
    }
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: form,
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          console.log('上传头像昵称成功')
        }
      }
    })
  },
  getTimes() {
    let myHeartTimes = 'tabArr[2].name'
    wx.request({
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/0/myHearts',
      method: "get",
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            [myHeartTimes]: '心动' + res.data.heartCount + '次'
          })
        }
        console.log(this.data.tabArr[2].name)
      }
    })
    let myHeartedTimes = 'tabArr[3].name'
    wx.request({
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/0/myHearted',
      method: "get",
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            [myHeartedTimes]: '被心动' + res.data.heartedCount + '次'
          })
          console.log(this.data.tabArr[3].name)
        }
      }
    })
  },
  getPhoneNumber(e) {
    console.log(e)
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: app.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/phone',
        // data: {
        //   encryptedData: e.detail.encryptedData,
        //   iv: e.detail.iv,
        //   sessionKey: that.data.session_key,
        //   uid: "",
        // },
        data: {
          appid: 'wxbfa171fdd4000e03',
          sessionKey: this.data.sessionKey,
          signature,
          rawData,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        method: "get",
        success: function (res) {
          console.log(res);
        }
      })
    }
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
  },
  getInfos() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
      method: "get",
      success: (res) => {
        console.log(res, '我是获取的信息')
        if (res.data.code == 0) {
          if (res.data.info.nickName && res.data.info.avatar) {
            this.setData({
              nick: res.data.info.nickName,
              avatarUrl: res.data.info.avatar
            })
            wx.setStorageSync('nick', res.data.info.nickName)
            wx.setStorageSync('avatarUrl', res.data.info.avatar)
          }
        }
      }
    })
  },
  async onShow() {
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1]
      })
      // this.getNick();
      this.getInfos();
      this.getTimes();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onShareTimeline: function () {
    return {
      title: '测试',
      query: {
        id: 0
      },
      imageUrl: '../../images/icon1.png'
    }
  },
})