// pages/teamDetail/teamDetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    teamId: '',
    dataMap: [],
    memberData: {},
    checkData: {}
  },
  getToken: function() {
    var self = this;
    return new Promise(resolve =>{
      wx.getStorage({
        key: 'token',
        success(res) {
          self.setData({
            token: res.data
          })
          return resolve();
        }
      })
    })
  },
  getDetail(teamId) {
    var self = this;
    app.restAjax.get(app.restAjax.path(app.volunteerUrl + '/wechat-miniapp/team/getteambyid/' + teamId, []), 
      {}, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
        console.log(data)
		    if('200' == code) {
          self.setData({
            dataMap: data
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  getMember(teamId) {
    var self = this;
    app.restAjax.get(app.restAjax.path(app.volunteerUrl + '/wechat-miniapp/teammember/listteammember?teamId=' + teamId, []), 
      {}, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          var teamMember = [];
          var checkMember = [];
          for(var item of data) {
            if('1' == item.status) {
              teamMember.push(item);
            }else {
              checkMember.push(item);
            }
          }
          self.setData({
            memberData: teamMember,
            checkData: checkMember
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  agree(e) {
    var self = this;
    console.log(e)
    var id = e.currentTarget.dataset.teammemberid;
    var type = e.currentTarget.dataset.type;
    app.restAjax.get(app.restAjax.path(app.volunteerUrl + '/wechat-miniapp/teammember/checkmember/'+id+'/' + type + '?teamId=' + self.data.teamId, []), 
      {}, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          if('1' == type) {
            app.dialog.msg('审核通过！');
          }else {
           app.dialog.msg('审核驳回！');
          }
          wx.navigateTo({
            url: '../myVolunteerTeam/myVolunteerTeam',
          })
        }
    }, function() {

    }, function() {
      
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.setData({
      teamId: options.teamId
    })
    this.getToken().then(result => {
      this.getDetail(options.teamId);
      this.getMember(options.teamId);
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})