// pages/ticketProdetail/ticketProdetail.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceReqFlowNo: '',
    list: [],
    pageNum: 1,
    pageSize: 200,
    pages: '',
    finish: false,
    moneyTotal: 0, //发票总金额
    rateTotal: 0, //发票总税额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      invoiceReqFlowNo: options.invoiceReqFlowNo
    })
    this.getList()
  },

  //获取发票的项目明细
  getList(type) {
    let params = {
      invoiceReqFlowNo: this.data.invoiceReqFlowNo,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoiceInfo/pageItems',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        let result = res.data
        let moneyTotal = this.data.moneyTotal
        let rateTotal = this.data.rateTotal
        for (let i = 0; i < result.records.length; i++) {
          moneyTotal += result.records[i].amount
          rateTotal += result.records[i].taxes
        }
        this.setData({
          moneyTotal,
          rateTotal
        })
        if (type === 'toFresh') {
          this.setData({
            list: result.records,
            pages: result.pages
          })
        } else {
          let list = this.data.list
          list = list.concat(result.records)
          this.setData({
            list,
            pages: result.pages
          })
        }
        if (result.total <= this.data.pageSize) {
          this.setData({
            finish: true
          })
        }
      }
    })
  },
  toFresh() {
    this.setData({
      pageNum: 1,
      pages: '',
      finish: false
    })
    this.getList('toFresh')
  },

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

  },

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

  },

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

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNum = this.data.pageNum
    let pages = this.data.pages
    pageNum++
    if (pageNum <= pages) {
      this.setData({
        pageNum
      })
      this.getList()
    } else {
      this.setData({
        finish: true
      })
      return
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '诺信云',
      path: '/pages/index/index'
    }
  }
})