// pages/apply/apply.js
const app = getApp()
const host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    show:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '',
    })
    this.setData({
      openid: app.globalData.openid
    })
    this.getData();
    wx.getStorage({
      key: 'userInfo',
      success: res=> {
        if(res.data){
          this.setData({show:0})
        }
      },
    })
  },
  getData:function(){
    wx.request({
      url: host+'/api/wx/signup/list',
      header:{
        openid:this.data.openid
      },
      method:'GET',
      success:res=>{
        if (res.statusCode == 403){
          this.save()
        }else{
          wx.hideLoading()
          let data=res.data.data
          for(let i=0;i<data.length;i++){
            data[i].subject_item_1 = data[i].subject_item.split('|')
          }
          this.setData({
            list: data
          })
        }
      }
    })
  },
  save: function () {
    var _this = this
    wx.showLoading({
      title: '正在登录',
    })
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        console.log(res)
        wx.request({
          url: host + '/api/wx/auth',
          method: 'POST',
          data: {
            nickname: res.data.nickName,
            avatar: res.data.avatarUrl,
            openid: app.globalData.openid,
          },
          success: res => {
            wx.hideLoading()
            _this.getData();

          }
        })
      },
    })

  },
  pay:function(e){
    wx.navigateTo({
      url: '../pay/pay?id='+e.target.dataset.id,
    })
  },
  onGotUserInfo: function (e) {
    wx.setStorage({
      key: 'userInfo',
      data: e.detail.userInfo,
    })
    console.log(app.globalData.openid)
    wx.request({
      url: host+'/api/wx/auth',
      method:'POST',
      data:{
        nickname: e.detail.userInfo.nickName, 
        avatar: e.detail.userInfo.avatarUrl, 
        openid: app.globalData.openid, 
      },
      success:res=>{
          console.log(res)
          this.setData({
            show:0
          })
      }
    })
  },
  downloadInvoice:function(e){
    console.log(e,2222222222222222)
    wx.downloadFile({
      url: 'url',
      success: function(res) {
        const tempFilePath = res.tempFilePath
        wx.saveFile({
          tempFilePath: tempFilePath,
          success: function(res) {
            const savedFilePath = res.savedFilePath
            console.log('下载并保存文件成功，保存路径为：', savedFilePath)
          }
        })
      }
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

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

  },

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

  },

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

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

  },

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

  }
})