const app = require('../../utils/util.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    avatarUrl: '../../images/morenImg.png',
    tabArr: [
      {
        name: '我的信息',
        img: '../../images/icon1.jpg',
        url: '/pages/InfoType/InfoType'
      }, 
      {
        name: '我的活动',
        img: '../../images/icon6.jpg',
        url: '/pages/myActive/myActive'
      },
      {
        name: '心动0次',
        img: '../../images/icon3.jpg',
        url: ''
      }, {
        name: '被心动0次',
        img: '../../images/icon7.jpg',
        url: ''
      },
      {
        name: '推荐好友',
        img: '../../images/icon5.jpg',
        url: '/pages/share/share'
      }
    ],
    userInfo: '',
    ysf: {
      title: '注册页',
      config: ''
    }
  },
  goTab(e) {
    console.log(e.currentTarget.dataset.index)
    if (e.currentTarget.dataset.index == 0 || e.currentTarget.dataset.index == 1) {
      if (this.data.loginStatus) {
        wx.navigateTo({
          url: e.currentTarget.dataset.src
        })
      } else {
        wx.showModal({
          title: '您还未登录',
          showCancel: false
        });
      }
    }if (e.currentTarget.dataset.index == 4){
      wx.navigateTo({
        url: '/pages/share/share'
      })
    }
  },
  goInfo(){
    wx.redirectTo({
      url: '/pages/personInfo/personInfo',
    })
  },
  goIndex() {
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo)
    this.setData({
      nick: e.detail.userInfo.nickName,
      avatarUrl: e.detail.userInfo.avatarUrl,
      loginStatus: true
    })
    this.upload(e.detail.userInfo.nickName, e.detail.userInfo.avatarUrl);
  },
  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)
        }
      }
    })
  },
  // getInfos() {
  //   tool.isLogin().then(res => {
  //     console.log(res)
  //     this.setData({
  //       userInfo: JSON.parse(res.rawData),
  //       avatarUrl: res.userInfo.avatarUrl,
  //       nick: res.userInfo.nickName,
  //       loginStatus: true
  //     })
  //   }).catch(err => {
  //     this.setData({
  //       loginStatus: false
  //     })
  //   })
  // },
  getInfos2() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
      method: "get",
      success: (res) => {
        if (res.data.code == 0) {
          let config='ysf.config';
          let sex1 = '';
          if(res.data.info.sex==0){
            sex1='女'
          }else{
            sex1='男'
          }
          this.setData({
            myInfo: res.data.info,
            [config]:JSON.stringify({
              "uid": res.data.id, // 用户唯一标识
              "level": 1, // vip等级
              "data": JSON.stringify([{
                  "index": 0,
                  "key": "name",
                  "label": "姓名",
                  "value": res.data.info.name
                },
                {
                  "index": 1,
                  "key": "createTime",
                  "label": "创建时间",
                  "value": res.data.info.createTime
                },
                {
                  "index": 2,
                  "key": "sex",
                  "label": "性别",
                  "value": sex1
                },
                {
                  "index": 3,
                  "key": "tel",
                  "label": "电话",
                  "value": res.data.info.tel
                }
              ])
            })
          })
          console.log(this.data.ysf.config)
          if(res.data.info.avatar&&res.data.info.nickName){
            this.setData({
              avatarUrl: res.data.info.avatar,
              nick: res.data.info.nickName,
              loginStatus: true,
          })
        }else{
          this.setData({
            loginStatus: false,
        })
        }}
      }
    })
  },
  async onShow() {
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1]
      })
      // this.getInfos();
      this.getInfos2();
      this.getTimes();
    })
  }
})