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

  /**
   * 页面的初始数据 
   */
  data: {
    imgurl: util.imgurl,
    name: '',
    count: 0,
    users: [],
    selcount: 0,
    bookid: 0,
    books: []
  },
  bookChange(e) {
    var nobk = this.data.books[e.detail.value]
    this.setData({
      bookid: nobk.id
    })
    this.getDetail()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    var ui = wx.getStorageSync("userinfo")
    if (ui) {
      this.setData({
        nowui: ui
      })
    }
    if (options.id) {
      this.setData({
        bookid: options.id
      })
      this.getDetail()
    }
    this.getBooks()
  },
  getBooks() {
    util.query('accountbook/wxapi/list', {
      pageNo: 1,
      pageSize: 99999
    }, function(res) {
      if (res.code === 10000) {
        var books = []
        res.data.forEach(function(e) {
          books.push({
            id: e.id,
            name: e.name
          })
        })
        that.setData({
          books: books
        })
      } else {

      }
    })
  },
  deluser() {
    if (this.data.selcount === 0) {
      util.showToast('请选择记账人')
      return
    }
    wx.showModal({
      title: '确认',
      content: '是否确定删除记账人',
      success(res) {
        if (res.confirm) {
          var userIds = []
          that.data.users.forEach(function(e) {
            if (e.sel) {
              userIds.push({
                userId: e.id
              })
            }
          })
          util.query("accountbook/wxapi/left", {
            accountBookId: that.data.bookid,
            userIds: userIds
          }, function(res) {
            if (res.code == 10000) {

              util.showToast('删除成功')
              that.getDetail()
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  setsel(e) {
    var data = this.data.users
    data[e.currentTarget.dataset.index].sel = !data[e.currentTarget.dataset.index].sel
    var i = 0
    data.forEach(function(res) {
      if (res.sel) {
        i++
      }
    })
    this.setData({
      users: data,
      selcount: i
    })
  },

  getDetail() {
    util.query('accountbook/wxapi/findbyid', {
      id: this.data.bookid
    }, function(res) {
      if (res.code === 10000) {
        var ismine = false
        if (that.data.nowui.uid === res.data.uid) {
          ismine = true
        }
        res.data.userInfos.forEach(function(e) {
          e.sel = false
          if (e.uid === res.data.uid) {
            that.setData({
              ui: e
            })
          }
        })

        that.setData({
          name: res.data.name,
          count: res.data.userInfos.length,
          users: res.data.userInfos,
          ismine: ismine
        })
      } else {
        util.showToast('未发现该账本')
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      return {
        title: this.data.ui.wechatName + '邀请你一起记账',
        path: 'pages/myinfo/joinbook/joinbook?id=' + this.data.bookid + '&uid=' + this.data.ui.id
      }
    }
  }
})