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

  /**
   * 页面的初始数据
   */
  data: {
    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;
    app.restAjax.get(app.restAjax.path(app.volunteerUrl + '/app/volunteerservice/getvolunteerservicebyid/' + id, []), 
      {}, {
        headers: {
        'token': self.data.token
        }
      }, function(code, data) {
        if('200' == code) {
          var ph=data.photo
          if(data.photo.endsWith(",")){
            ph=data.photo.substr(0,data.photo.length-1);
          }
          data.photo = app.volunteerUrl + '/route/file/download/true/' +ph;
          self.setData({
            dataMap: data
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  /**
   * 志愿者团队报名
   */
  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;
    app.restAjax.post(app.restAjax.path(app.volunteerUrl + '/wechat-miniapp/servicemember/saveservicemember', []), 
      serviceMemberVO, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          app.dialog.msg('注册成功！');
          wx.redirectTo({
            url: '../volunteer/volunteer',
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  /**
   * 发送取消报名请求
   */
  sendCancelEnrollRequest() {
    var self = this;
    var url;
    if(self.data.teamArray.length > 0) {
      url = app.volunteerUrl + '/wechat-miniapp/servicemember/cancelapply/'+ self.data.type +'/'+ self.data.serviceId +'/' + self.data.team[self.data.teamIndex].teamId;
    }else {
      url = app.volunteerUrl + '/wechat-miniapp/servicemember/cancelapply/'+ self.data.type +'/'+ self.data.serviceId +'/null';
    }

    app.restAjax.get(app.restAjax.path(url, []), {}, {
      headers: {
        token: self.data.token
      }
    }, function(code, data) {
		    if('200' == code) {
          app.dialog.msg('取消报名成功！');
          wx.redirectTo({
            url: '../volunteer/volunteer',
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  /**
   * 获取我的团队
   */
  getMyTeam: function() {
    var self = this;
    console.log(self.data.token)
    app.restAjax.get(app.restAjax.path(app.volunteerUrl + '/wechat-miniapp/team/myteamm', []), 
      {}, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
		    if('200' == code) {
          var array = [];
          for(var item of 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: data.rows
          })
        }
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      
    })
  },
  toActivityHis: function(e) {
    var self = this;
    const status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '../activityDialog/activityDialog?serviceId=' + self.data.serviceId + '&status=' + status,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var self = this;
    self.setData({
      serviceId: id
    })
    this.getToken().then(result => {
      this.getDetail(id);
      this.getMyTeam();
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})