// pages/activityDetail/activityDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: 'http://192.168.0.111:8888/volunteer/',
    imgUrl: 'http://192.168.0.111:8888/volunteer/route/file/downloadfile/true/',
    dataMap: {},
    token: '',
    serviceId: '',
    type: '',

    teamIndex: 0,
    teamResult: '请选择服务区域',
    team: {}, // 服务区域-市
    teamArray: [], // 服务区域-市
  },
  getToken: function() {
    var self = this;
    return new Promise(resolve =>{
      wx.getStorage({
        key: 'token',
        success(res) {
          self.setData({
            token: res.data
          })
          return resolve();
        }
      })
    })
  },
  getDetail: function(id) {
    var self = this;
    wx.request({
      url: self.data.baseUrl + 'wxminiapp/volunteerservice/getvolunteerservicebyid/' + id,
      header: {
        'token': self.data.token
      },
      success: function(res) {
        if('200' == res.statusCode) {
          res.data.photo = self.data.imgUrl + res.data.photo;
          self.setData({
            dataMap: res.data
          })
        }
      }
    })
  },
  /**
   * 志愿者团队报名
   */
  enroll: function(e) {
    var self = this;
    self.setData({
      serviceId: e.target.dataset.serviceid,
      type: e.target.dataset.type
    })
  },
  /**
   * 
   * @param {志愿者报名} e 
   */
  volunteerEnroll: function(e) {
    var self = this;
    self.setData({
      serviceId: e.target.dataset.serviceid,
      type: e.target.dataset.type
    })

    // 提交报名请求
    var serviceMemberVO = {
      serviceId: self.data.serviceId,
      voluntaryType: self.data.type
    }
    self.sendEnrollRequest(serviceMemberVO);

  },
  /**
   * 
   * @param {团队报名} e 
   */
  teamChange: function(e) {
    var self = this;
    
    self.setData({
      teamIndex: e.detail.value
    })

    // 提交报名请求
    var serviceMemberVO = {
      serviceId: self.data.serviceId,
      teamId: self.data.team[self.data.teamIndex].teamId,
      voluntaryType: self.data.type
    }
    self.sendEnrollRequest(serviceMemberVO);
  },
  /**
   * 
   * @param {团队取消报名} e 
   */
  teamCancelChange: function(e) {
    var self = this;
    
    self.setData({
      teamIndex: e.detail.value
    })

    self.sendCancelEnrollRequest();
  },
  /**
   * 
   * @param {志愿者取消报名} e 
   */
  volunteerCancelEnroll: function(e) {
    var self = this;
    self.setData({
      serviceId: e.target.dataset.serviceid,
      type: e.target.dataset.type,
      teamArray: []
    })
    self.sendCancelEnrollRequest();
  },
  /**
   * 
   * @param {发送报名请求} serviceMemberVO 
   */
  sendEnrollRequest: function(serviceMemberVO) {
    var self = this;
    wx.request({
      url: self.data.baseUrl + 'wxminiapp/servicemember/saveservicemember',
      method: 'POST',
      header: {
        token: self.data.token
      },
      data: serviceMemberVO,
      success(res) {
        if(res.statusCode == 200) {
          wx.showToast({
            title: '报名成功！',
            icon: 'none',
            duration: 1500
          })
          wx.navigateTo({
            url: '../volunteer/volunteer',
          })
        }
      }
    })
  },
  /**
   * 发送取消报名请求
   */
  sendCancelEnrollRequest() {
    var self = this;
    var url;
    if(self.data.teamArray.length > 0) {
      url = self.data.baseUrl + 'wxminiapp/servicemember/cancelapply/'+ self.data.type +'/'+ self.data.serviceId +'/' + self.data.team[self.data.teamIndex].teamId;
    }else {
      url = self.data.baseUrl + 'wxminiapp/servicemember/cancelapply/'+ self.data.type +'/'+ self.data.serviceId +'/null';
    }
    wx.request({
      url: url,
      header: {
        token: self.data.token
      },
      success(res) {
        if(res.statusCode == 200) {
          wx.showToast({
            title: '取消报名成功！',
            icon: 'none',
            duration: 1500
          })
          wx.navigateTo({
            url: '../volunteer/volunteer',
          })
        }
      },
      fail(res) {
        
      }
    })
  },
  /**
   * 获取我的团队
   */
  getMyTeam: function() {
    var self = this;
    wx.request({
      url: self.data.baseUrl + 'wxminiapp/team/myteamm',
      header: {
        token: self.data.token
      },
      success(res) {
        if('200' == res.statusCode) {
          var array = [];
          for(var item of res.data.rows) {
            delete item.teamArea;
            delete item.teamAreaDictionaryName;
            delete item.teamWatchword;
            delete item.teamOwner;
            delete item.memberId;
            delete item.memberName;
            delete item.gmtCreate;
            delete item.photo;
            delete item.count;
            delete item.status;
            delete item.content;
            delete item.isTeamOwner;
            delete item.checkStatus;
            delete item.teamCounty;
            delete item.teamCity;
            delete item.teamCityDictionaryName;
            delete item.teamCountyDictionaryName;
            delete item.teamCity;
            
            array.push(item.teamName);
          }
          self.setData({
            teamArray: array,
            team: res.data.rows
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.getToken().then(result => {
      this.getDetail(id);
      this.getMyTeam();
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})