let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uss: []
  }, 
  onGotUserInfo(e) {
    this.bindphone(e.detail.userInfo)
  },
  bindphone(ui) {

    wx.request({
      url: util.baseurl + 'cuser/wxapi/binduserinfo',
      data: {
        phone: '',
        avatarUrl: ui.avatarUrl,
        nickName: ui.nickName
      },
      dataType: 'json',
      success: function(d) {
        wx.hideLoading();
        if (d.statusCode === 200) {
          that.setData({
            user: d.data.userInfo
          })
          that.joinbook()

        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'none',
            duration: 2000
          })
        }

      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': this.data.token
      }
    })
  },
  findUser(id) {
    util.query('userinfo/wxapi/findbyid', {
      id: id
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          u: res.data
        })
      } else {

      }
    })

  },
  join() {
    if (!this.data.user) {
      util.showToast('还未登录，请先登录')
      setTimeout()
    } else {
      this.joinbook()
    }
  },
  joinbook() {

    util.query('accountbook/wxapi/join', {
      accountBookId: this.data.nowbook.id,
      userIds: [{
        userId: this.data.user.id
      }]
    }, function(res) {
      if (res.code === 10000) {
        util.showToast('加入成功')
        that.getDefaultBook()
      } else {
        util.showToast('加入失败')
      }
    }, 'post')
  },
  checkjoin() {
    var myid = this.data.user.id
    this.data.nowbook.userInfos.forEach(function(res) {
      if (res.id === myid) {
        that.setData({
          isjoin: true
        })
        return
      }
    })
  },
  wxlogin() {
    wx.login({
      success: function(res) {
        if (res.code) {
          util.query(
            "/cuser/wxapi/getopenid", {
              code: res.code
            },
            function(da) {
              if (da.code === 10000) {
                wx.setStorageSync("token", da.token)
                that.setData({
                  token: da.token
                })
                // wx.setStorageSync("userinfo", da.userinfo)
                that.getDefaultBook()
                that.findUser(that.data.uid)
                if (da.registed) {
                  that.setData({
                    user: da.userinfo,
                    registed: true
                  })

                } else {
                  that.setData({
                    registed: false
                  })
                }
              } else {
                wx.showToast({
                  title: '登录失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            }, "get", 1
          )
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.setData({
      id: options.id,
      uid: options.uid
    })
    that.wxlogin()

  },
  getDefaultBook() {
    util.query('accountbook/wxapi/findbyid', {
      id: this.data.id
    }, function(res) {
      if (res.code === 10000) {
        var uss = []
        var us = res.data.userInfos
        for (var i = 0; i < parseInt(us.length / 5) + 1; i++) {
          uss[i] = []
          for (var j = 5 * i; j < (i + 1) * 5; j++) {
            if (us[j]) {  
              uss[i].push({
                name: us[j].wechatName,
                logo: us[j].wechatIcon
              })
            } else {
              uss[i].push({
                no: 1
              })
            }

          }
        }
        that.setData({
          nowbook: res.data,
          uss: uss
        })
        that.checkjoin()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})