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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    customerName: '',
  },

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

  //待开发票
  getList(type) {
    let params = {
      mobile: utils.getInfo().mobile,
      // reqChannel: 5,
      belongEntTaxId: wx.getStorageSync('entTaxId'),
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    let customerName = utils.trimAll(this.data.customerName)
    if (customerName !== '') {
      params.customerName = customerName
    }
    utils.axios({
      method: 'get',
      url: '/sys/fapiao/todo/findPage',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        let result = res.data
        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
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getList('toFresh')
  },

  //搜素
  search(e) {
    let customerName = e.detail.value
    this.setData({
      customerName
    })
    this.toFresh()
  },

  //确认框
  showDel(e) {
    let params = {
      mobile: utils.getInfo().mobile,
      reqChannel: 5,
      delList: [{
        recordId: e.currentTarget.dataset.recordid,
        belongEntTaxId: e.currentTarget.dataset.belong,
        status: e.currentTarget.dataset.status
      }]
    }
    let index = Number(e.currentTarget.dataset.index)
    wx.showModal({
      content: '确定删除该条数据吗？',
      confirmColor: '#007dff',
      success: res => {
        if (res.confirm) {
          this.del(params, index)
        }
      }
    })
  },

  //删除
  del(params, index) {
    utils.axios({
      method: 'post',
      url: '/sys/fapiao/todo/delete',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '删除中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        let list = this.data.list
        list.splice(index, 1)
        this.setData({
          list
        })
        utils.toast('删除成功')
      }
    })
  },

  //发票详情
  toDetail(e) {
    let list = this.data.list
    let index = e.currentTarget.dataset.index
    let status = e.currentTarget.dataset.status
    let recordId = e.currentTarget.dataset.recordid
    let belongEntTaxId = e.currentTarget.dataset.belong
    wx.setStorageSync('ticketWaitingData', list[index])
    wx.navigateTo({
      url: `/pages/ticketWaiting/detail?recordId=${recordId}&belongEntTaxId=${belongEntTaxId}&status=${status}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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'
    }
  }
})