const api = require('../../utils/api.js')
const app = getApp()
Page({
  data: {
    auth: {},
    userInfo: {},
    ShareGamesParams: '',
    ShowRedPackMask: false, // 红包
    authEnter: false,
    VisitorList: [], // 最近访客
    floats: [],
    gameData: {},
    gameCount: 99,
    sharePath: 'pages/share/share',
    shareInfo: {},
    boxInfo: '', // 主页信息
    redIcon: '',
    redName: '',
    mpType: app.globalData.mpType
  },
  onLoad: function (opt) {
    let timer = setInterval(() => {
      if (app.globalData.PageCallback) {
        this.getBoxInfo(opt.mem_id, opt.scene)
        this.getShareInfo()
        this.getMemGameList(opt.mem_id, opt.scene)
        this.setData({
          auth: app.globalData.auth,
          userInfo: app.globalData.userInfo,
          helpShareId: opt.mem_id || '',
          helpShareState: opt.state || ''
        })
        this.PageInit()
        clearInterval(timer)
      }
    }, 200)
  },

  PageInit: function () {
    this.setData({
      userInfo: app.globalData.userInfo,
      auth: app.globalData.auth,
    })
    // 红包
    console.log('game-onLoad-获取userInfo', app.globalData.userInfo)
    if (app.globalData.userInfo.get_novice_award === 1) {
      // 是否领取了新手奖励 1否 2是
      if (app.globalData.auth.red === 2) {
        wx.hideTabBar({
          animation: true
        })
        api.getMpRedInfo().then(res => {
          this.setData({
            redIcon: res.data.icon || '/image/logo.png',
            redName: res.data.name || app.globalData.mpName
          })
        })
        setTimeout(() => {
          this.setAnimation().then(() => {
            this.setData({
              redAnimation: true
            })
          })
        }, 350)
      } else {
        this.setData({
          authEnter: true
        })
      }
    }
  },

  // 设置红包动画
  setAnimation: function (e) {
    const _self = this
    return new Promise((resolve, reject) => {
      _self.setData({
        ShowRedPackMask: true
      })
      resolve()
    })
  },

  getShareInfo: function () {
    api.getShareInfo({
      data: {
        path: this.data.sharePath
      }
    }).then(res => {
      this.setData({
        shareInfo: res.data
      })
    }).catch(err => {
      console.log('getShare', err)
    })
  },
  // 获取他人主页信息
  getBoxInfo: function (id, state) {
    let params = {
      to_mem_id: id || '',
      state: state || ''
    }
    api.getHomeInfo(params).then(res => {
      this.setData({
        boxInfo: res.data
      })
    })
  },
  ChangeShare: function (e) {
    this.setData({
      ShowShareMask: e.currentTarget.dataset.show ? true : false
    })
  },
  routeHome: function (e) {
    wx.switchTab({
      url: '/pages/game/game'
    })
  },
  getMemGameList: function (id, state) {
    let params = {
      to_mem_id: id || '',
      state: state || '',
      page: 1,
      offset: 3
    }
    api.getMemGameList(params).then(res => {
      if (res.data && res.data.list.length) {
        this.setData({
          memGame: res.data,
          gameCount: res.data.count,
          floats: res.data.list.map((item) => {
            return this.data.mpType === 'gd' ? item.mem_integral : (item.mem_reward).toFixed(2)
          })
        })
      } else {
        this.getGameList(id,state)
      }
    })
  },
  getGameList: function (id, state) {
    let params = {
      to_mem_id: id || '',
      state: state || '',
      page: 1,
      offset: 999
    }
    api.getGameList(params).then(res => {
      this.setData({
        gameData: res.data.list,
        gameCount: res.data.count,
        floats: res.data.list.map((item) => {
          return this.data.mpType === 'gd' ? item.mem_integral : (item.mem_reward).toFixed(2)
        })
      })
    })
  },

  openGame: function (e) {
    let id = e.currentTarget.dataset.id
    api.openGame({
      game_id: id
    }).then(res => {
      console.log(res)
    }, err => {
      console.log(err)
    })
  },

  onShow: function () {
    wx.showShareMenu({ // 允许转发时携带 shareTicket。 
      withShareTicket: true
    })
  },
  onShareAppMessage: function (e) {
    this.getShareInfo()
    let { title, image, state } = this.data.shareInfo
    let memid = this.data.userInfo.mem_id
    if (e.target && e.target.id === 'help') {
      state = this.data.helpShareState
      memid = this.data.helpShareId
    }
    let path = `/pages/share/share?state=${state}&mem_id=${memid}`
    title = title || this.data.userInfo.nickname
    return {
      title,
      path,
      imageUrl: image
    }
  },

  // 获取用户信息
  getUserInfoFun: function () {
    var _self = this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res)
        api.updateUserInfo({
          raw_data: res.rawData,
          signature: res.signature,
          encrypted_data: res.encryptedData,
          iv: res.iv
        }).then(res => {
          if (res.code === 200) {
            _self.setData({
              // redPackAward: _self.data.mpType === 'gd' ? res.data.novice_award_integral : (res.data.novice_award * 1).toFixed(2),
              redPackAward: (res.data.novice_award * 1).toFixed(2),
              getSuccess: true
            })
            // 更新余额
            api.getUserInfo()
            setTimeout(() => {
              _self.setData({
                userInfo: getApp().globalData.userInfo
              })
            }, 500)
          } else {
            wx.showToast({
              title: res.msg,
            })
          }
        })
        _self.setData({
          authEnter: false
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  // 关闭红包弹框
  CloseRedPack: function (e) {
    const _self = this
    _self.setData({
      redAnimation: false
    })
    setTimeout(() => {
      _self.setData({
        ShowRedPackMask: false
      })
      wx.showTabBar({
        animation: true
      })
    }, 300)
  }
})