const app = getApp()
import $http from './../../API/http'
Page({
  data:{
    statusList: ['SUCCESS', 'WAITING_PERMIT', 'REFUND_SUCCESS'],
    recordAlliwanceListData: [],
    currentPage: 1,
    totalCount: '',
    pageNumber: 10,
    subsidyNo: ''
  },
  onLoad(query) {
    this.resetData()
    dd.setNavigationBar({title: '账单记录'});
    console.log(query);
    this.setData({
      subsidyNo: query.subsidyNo
    })
  },
  resetData(){
    this.setData({
      sceneType: '',
      recordAlliwanceListData: [],
      currentPage: 1,
      totalCount: '',
      pageNumber: 10,
    })
  },
  queryListPaymentRecord(str) {
    console.log(str)
    const _that = this;
    let data = {
      currentPage: this.data.currentPage,
      pageNumber: this.data.pageNumber,
      statusList: this.data.statusList,
      subsidyNo: this.data.subsidyNo
    }
    $http.$http('consume/list_payment_record', data, 'GET').then((res) => {
      let dataList = [...res.recordList];
      let recordAlliwanceListDataSource = _that.data.recordAlliwanceListData
      if (dataList.length > 0) {
        dataList.map((item, index) => {
          item.createDate = _that.timeToStr(item.createDate)
          recordAlliwanceListDataSource.push(item)
        })
        let totalCount = Math.ceil(parseInt(res.totalCount)/10)
        _that.setData({
          recordAlliwanceListData: recordAlliwanceListDataSource,
          totalCount: totalCount
        })
        dd.hideLoading();
      } else {
        _that.setData({
          recordAlliwanceListData: [],
          totalCount: 1
        })
        dd.hideLoading();
      }
    }).catch((res) => {
      console.log(res)
    })
  },
  goToDetails(event){
    dd.navigateTo({url:`./../billDetails/billDetails?trxNo=${event.target.dataset.trxNo}`})
  },
  onReady() {
    this.queryListPaymentRecord()
  },
  timeToStr (time) {
    let timeData = new Date(parseInt(time))
    let year = timeData.getFullYear()
    let month = timeData.getMonth() + 1
    let date = timeData.getDate()
    let hour = timeData.getHours()
    let minute = timeData.getMinutes()
    return `${year}年${month}月${date}日 ${hour}:${minute}`
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    let currentNum = '';
    if (this.data.currentPage < this.data.totalCount) {
      currentNum = this.data.currentPage + 1
      this.setData({
        currentPage: currentNum
      })
      this.queryListPaymentRecord(currentNum)
    }
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});