// pages/signStatus/signStatus.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      page: 1,
      rows: 10
    },
    signList: []
  },
  // 获取报名情况
  getApplyStatus: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getMyApply, [app.baseUrls.testUrl]), self.data.page, {
      headers: {
          token: wx.getStorageSync('token')
      }
    },function (code, data) {
      if (data.rows.length > 0) {
        for (let i = 0; i < data.rows.length; i++) {
          if (data.rows[i].applyAuditState == '0') {
            data.rows[i]['statusName'] = 'wait'
          } else if (data.rows[i].applyAuditState == '1') {
            data.rows[i]['statusName'] = 'draw'
          } else if (data.rows[i].applyAuditState == '2') {
            data.rows[i]['statusName'] = 'pass'
          } else if (data.rows[i].applyAuditState == '3') {
            data.rows[i]['statusName'] = 'complete'
          } else {
            data.rows[i]['statusName'] = 'not-pass'
          }
        }
        self.setData({
          signList: self.data.signList.concat(data.rows)
        })
      } else {
        wx.showToast({
          title: '暂无更多',
          icon: 'none'
        })
        self.setData({
          ['page.page']: self.data.page.page -1
        })
      }
      
    },function (code, err) {
        console.log(err)
    })
  },
  // 取消报名
  cancelSign: function (e) {
    var id = e.currentTarget.dataset.id
    var self = this
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '提示',
      content: '取消报名后需重新填写报名信息',
      success: function (res) {
        if (res.confirm) {
          app.restAjax.put(app.restAjax.path(app.apis.cancelSign, [app.baseUrls.testUrl, id]), {}, {
            headers: {
                token: wx.getStorageSync('token')
            }
          },function (code, data) {
            if (code == '200') {
              wx.showToast({
                title: '取消报名成功！',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '../index/index',
                })
              }, 1500)
            }
          },function (code, err) {
              console.log(err)
          })
        }
      }
    })
  },
  // 查看详情
  checkDetail: function (e) {
    wx.navigateTo({
      url: '../signRecord/signRecord?applyId=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getApplyStatus()
  },

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

  },

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

  },

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

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var self = this
    self.setData({
      ['page.page']: self.data.page.page + 1
    })
    self.getApplyStatus()
  },

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

  }
})