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

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    otherTeamMap: {},
    type: '1',
    teamId: '',

  },
  getToken: function() {
    var self = this;
    return new Promise(resolve =>{
      wx.getStorage({
        key: 'token',
        success(res) {
          self.setData({
            token: res.data
          })
          return resolve();
        }
      })
    })
  },
  newTeam: function () {
    wx.navigateTo({
      url: '../newTeam/newTeam',
    })
  },
  changeTab: function(e) {
    var self = this;
    self.setData({
      type: e.currentTarget.dataset.type
    })
    if(self.data.type === '1') {
      self.otherTeam(app.volunteerUrl + '/wxminiapp/team/otherteamm?otherTeam=true');
    }else {
      self.otherTeam(app.volunteerUrl + '/wxminiapp/team/myjointeam?myTeam=true');
    }
  },
  otherTeam: function(url) {
    var self = this;
    app.restAjax.get(app.restAjax.path(url, []), 
      {}, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          self.setData({
            otherTeamMap: data.rows
          })
        }
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      
    })
  },
  /**
   * 申请/退出加入团队
   */
  joinOrOutTeam: function(e) {
    var self = this;
    self.setData({
      teamId: e.currentTarget.dataset.teamid
    })
    var teamMemberVO;
    var url;
    var methodType;
    var title;
    if('1' == self.data.type) {
      url = app.volunteerUrl + '/wxminiapp/teammember/saveteammember'
      teamMemberVO = {
        teamId: e.currentTarget.dataset.teamid
      }
      methodType = app.restAjax.post
      title = '申请成功！'
    }else {
      url = app.volunteerUrl + '/wxminiapp/teammember/quitTeam/' + self.data.teamId
      methodType = app.restAjax.get
      title = '退出成功！'
    }

    methodType(app.restAjax.path(url, []), 
      teamMemberVO, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          app.dialog.msg(title);
          wx.navigateTo({
            url: '../volunteer/volunteer',
          })
        }
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getToken().then(result => {
      this.otherTeam(app.volunteerUrl + '/wxminiapp/team/otherteamm?otherTeam=true');
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})