// pages/share/game.js
const api = require('../../utils/api.js')
let app = getApp()

Page({
  data: {
    result: {
      mem: {},
      game: {},
      auth: {},
      currentInfo: {},
      authEnter: false,
      ShowRedPackMask: false, // 红包
      getSuccess: false, // 红包是否领取
      redAnimation: false, // 红包动画
      redIcon: '',
      redName: ''
    },
    mpType: app.globalData.mpType
  },
  onLoad: function(opt) {
    let timer = setInterval(() => {
      if (app.globalData.PageCallback) {
        let {
          game_id,
          state,
          mem_id
        } = opt
        this.initPage()
        this.getInfo(mem_id)
        this.getGameInfo(game_id, state)
        clearInterval(timer)
      }
    }, 200)
  },
  initPage: function() {
    this.setData({
      auth: app.globalData.auth,
    })
    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
        })
      }
    }
  },
  // 获取用户信息
  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)
  },
  // 设置红包动画
  setAnimation: function(e) {
    const _self = this
    return new Promise((resolve, reject) => {
      _self.setData({
        ShowRedPackMask: true
      })
      resolve()
    })
  },
  getGameInfo: function(game_id, state) {
    api.getShareGameDetail({
      game_id,
      state
    }).then(res => {
      this.setData({
        result: res.data
      })
    }, err => {
      console.log(err)
    })
  },
  getInfo: function(id) {
    api.getHomeInfo({
      to_mem_id: id
    }).then(res => {
      this.setData({
        currentInfo: res.data
      })
    })
  },
  switchTo: function(e) {
    wx.switchTab({
      url: e.currentTarget.dataset.path
    })
  }
})