// pages/residents/reportHistory.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    displayName: '',
    displayResult: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('onShow')
    let userInfo = wx.getStorageSync("userInfo")
    this.setData({
      userInfo: userInfo,
      displayName: userInfo.realname
    })

    // 获取用户填报历史
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.request({
      url: `${app.globalData.baseUrl}/access/report/getRecordList`,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //用于post
      },
      data: {
        userId: this.data.userInfo.id
      },
      success: function (d) {
        wx.hideLoading()
        let result = d.data
        console.log("result", result)
        if (result.success) {
          console.log("result.data", result.data)
          that.setData({
            displayResult: result.data.map(item => {
              // 时间友好化显示
              let t = new Date(item.time).getTime() + 8 * 3600 * 1000
              item.time = new Date(t).toISOString().substring(0, 19).replace("T", " ")
              return item
            })
          })
        } else {
          wx.showToast({
            title: "加载填报信息失败",
            icon: 'error',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.hideLoading()
        wx.showToast({
          title: "请求失败",
          icon: 'error',
          duration: 2000
        })
      },
      complete: function () {
        wx.hideNavigationBarLoading();
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})