import {
  imine,
  ihome
} from '../../../api/api.js'

Page({
  data: {
    user: {},
  },
  input(e) {
    this.setData({
      [`user.${e.currentTarget.dataset.key}`]: e.detail.value
    })
  },
  onLoad(args) {
    this.setData({
      endDate: new Date().format('yyyy-MM-dd'),
    })
    ihome.loadSponsors(r => {
      this.setData({
        sponsors: r.list
      })
    })
    wx.getStorage({
      key: 'user',
      success: r => {
        r.data.realname = r.data.realname || r.data.nickname
        this.setData({
          user: Object.assign(this.data.user, r.data)
        })
      },
    })

    this.loadMyinformation()
  },

  loadMyinformation() {
    imine.loadMyinformation({reqdata:{id:1}},r => {
      r.userinfo.realname = r.userinfo.realname || r.userinfo.nickname
      this.setData({
        user: Object.assign(this.data.user, r.userinfo)
      })
      wx.setStorage({
        key: 'user',
        data: this.data.user
      })
    })
  },

  clickRegister() {
    imine.loadReg(this.data.user, r => {
      wx.reLaunch({
        url: '../../home/home',
      })
    })
  },
})