const app = require('../../utils/util.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    currentIndex: 0,
    arr: [],
    arr1: [],
    isInfo: false,
    isTargetInfo: false,
    letterStatus: true,
    lbArr: [{
      src: '../../images/lb2.jpg'
    }],
    userInfo: '',
    ysf: {
      title: '注册页',
      config: ''
    }
  },
  // 轮播
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  // 关闭信件
  closeLetter() {
    console.log('1')
    this.setData({
      letterStatus: true
    })
    wx.setStorageSync('letterStatus', true)
  },
  goShare() {
    wx.navigateTo({
      url: '/pages/share/share',
    })
  },
  // 跳转活动详情页面
  goActive(e) {
    if (e.currentTarget.dataset.activestatus != -1) {
      let id = e.currentTarget.dataset.id
      let openId = e.currentTarget.dataset.openid
      wx.navigateTo({
        url: '/pages/activeDetail/activeDetail?id=' + id + '&openId=' + openId,
      })
    } else {
      wx.showModal({
        title: '活动已经结束啦',
        showCancel: false
      });
    }
  },
  goActiveDefault() {
    wx.navigateTo({
      url: '/pages/activeDefaultDetails/activeDefaultDetails',
    })
  },
  // 获取活动列表
  getActivies() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/actives',
      method: "get",
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          this.setData({
            arr: res.data.actives
          })
        }
      },
      complete() {
        wx.hideLoading();
      }
    })
  },
  goInfo() {
    wx.redirectTo({
      url: '/pages/personInfo/personInfo',
    })
  },
  goMy() {
    wx.redirectTo({
      url: '/pages/personal/personal',
    })
  },
  getIntroduce(id) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/introduce',
      method: "get",
      data: {
        introduce: id
      },
      success: (res) => {
        console.log(res, '获取推荐人')
      }
    })
  },
  getletterStauts() {
    console.log(wx.getStorageSync('letterStatus'))
    if (wx.getStorageSync('letterStatus') == true) {
      console.log('我缓存里有值是1')
      this.setData({
        letterStatus: true
      })
      // this.closeLetter();
    } else {
      console.log('我没有缓存、')
      this.setData({
        letterStatus: false
      })
    }
    console.log(this.data.letterStatus)
  },
  getInfos() {
    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='男'
          }
          console.log(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)
        }
      }
    })
  },
  onLoad(options) {
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1],
      })
      this.getActivies();
      this.getInfos()
    })
    if (options.id) {
      this.getIntroduce(options.id);
    }
  },
  onShow() {
    this.getletterStauts();
    this.getInfos()
    tool.isLogin().then(res => {
      this.setData({
        userInfo: JSON.parse(res.rawData)
      })
      console.log(this.data.userInfo)
    }).catch(err => {
      console.log(err)
    })
  }
})