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

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    dataMap: {},
    phone: '',
    code: '',
    name: '',
    idCard: '',
    trueCode: '',
    activityId: ''
  },
  getToken: function() {
    var self = this;
    return new Promise(resolve =>{
      wx.getStorage({
        key: 'token',
        success(res) {
          self.setData({
            token: res.data
          })
          return resolve();
        }
      })
    })
  },
  getCode: function() {
    var self = this;
    if(!self.data.phone) {
      app.dialog.msg('请输入手机号');
      return false;
    }
    app.restAjax.get(app.restAjax.path(app.activityUrl + '/app/activityuserapply/saveSmsCoderelease/' + self.data.phone, []), 
      {}, null, function(code, data) {
        console.log(data)
		    if('200' == code) {
          
        }
    }, function(code, data) {
      app.dialog.msg(data.msg)
    }, function() {
      
    })
  },
  commit: function() {
    var self = this;
    if(!self.data.phone) {
      app.dialog.msg('请输入手机号');
      return false;
    }
    if(!self.data.name) {
      app.dialog.msg('请输入姓名');
      return false;
    }
    if(!self.data.idCard) {
      app.dialog.msg('请输入身份证号码');
      return false;
    }
    var activityUserApplyVO = {
      activityId: self.data.activityId,
      code: self.data.code,
      userIdCard: self.data.idCard,
      userName: self.data.name,
      userPhone: self.data.phone
    }
    app.restAjax.post(app.restAjax.path(app.activityUrl + '/app/activityuserapply/saveactivityuserapply', []), 
      activityUserApplyVO, {
        headers: {
          token: self.data.token
        }
      }, function(code, data) {
        console.log(data)
		    if('200' == code) {
          wx.navigateTo({
            url: '../activitySign/activitySign?id=' + self.data.activityId
          })
        }
    }, function() {

    }, function() {
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    this.getToken().then(result => {
      var url;
      var id = options.id;
      if(!self.data.token) {
        url = '/wxminiapp/activitylibrary/getactivitylibrarybyidrelease/' + id;
      }else {
        url = '/wxminiapp/activitylibrary/getactivitylibrarybyid/' + id
      }
      self.setData({
        activityId: options.id
      })
      app.restAjax.get(app.restAjax.path(app.activityUrl + url, []), 
        {}, {
          headers: {
            token: self.data.token
          }
        }, function(code, data) {
          if('200' == code) {
            data.activityImage = app.activityUrl + '/route/file/downloadfile/true/' + data.activityImage;
            self.setData({
              dataMap: data
            })
          }
      }, function() {

      }, function() {
        
      })
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})