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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    statusList: ['开票中', '开票成功', '开票失败'],
    statusIndex: '',
    status: '',
    typeList: ['增值税普通电子发票', '增值税专用电子发票'],
    hongchongFlag: '',
    typeIndex: '',
    type: '',
    tabIndex: 0,
    time: '',
    keywords: '',
  },

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

  //列表
  getList(type) {
    let params = {
      mobile: utils.getInfo().mobile,
      reqChannel: 5,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    if (this.data.keywords !== '') {
      params.customerName = this.data.keywords
    }
    if (this.data.time !== '') {
      params.invoiceDateEnd = this.data.time + ' 23:59:59'
    }
    if (this.data.status !== '') {
      params.status = this.data.status
    }
    if (this.data.hongchongFlag !== '') {
      params.hongchongFlag = this.data.hongchongFlag
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoiceInfo/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) {
    this.setData({
      keywords: utils.trimAll(e.detail.value)
    })
    this.toFresh()
  },

  //全部状态
  setStatus(e) {
    let statusIndex = Number(e.detail.value)
    let status = ''
    if (statusIndex === 0) {
      status = 2
    }
    if (statusIndex === 1) {
      status = 3
    }
    if (statusIndex === 2) {
      status = 4
    }
    this.setData({
      statusIndex,
      status
    })
    this.toFresh()
  },
  clearStatus() {
    this.setData({
      statusIndex: '',
      status: ''
    })
    this.toFresh()
  },

  //发票类型
  setType(e) {
    let typeIndex = Number(e.detail.value)
    let type = ''
    if (typeIndex === 0) {
      type = 2
    }
    if (typeIndex === 1) {
      type = 3
    }
    this.setData({
      typeIndex,
      type
    })
    /* this.toFresh() */
    utils.toast('该查询条件接口暂无参数可传递')
  },
  clearType() {
    this.setData({
      typeIndex: '',
      type: ''
    })
    /* this.toFresh() */
  },

  //开票时间
  setTime(e) {
    let time = e.detail.value
    this.setData({
      time
    })
    this.toFresh()
  },
  clearTime() {
    this.setData({
      time: ''
    })
    this.toFresh()
  },

  //切换
  switchTab(e) {
    let tabIndex = e.currentTarget.dataset.index
    let hongchongFlag = ''
    if (tabIndex === 0) {
      hongchongFlag = ''
    }
    if (tabIndex === 1) {
      hongchongFlag = 5
    }
    this.setData({
      tabIndex,
      hongchongFlag
    })
    this.toFresh()
  },

  //开票详情
  toDetail(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    let item = list[index]
    wx.setStorageSync('ticketDetail', item)
    wx.navigateTo({
      url: '/pages/ticketDetail/ticketDetail'
    })
  },

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