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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    customerName: '',
    userInfo: '',
    company: '',
    blankInvalidCnt: 0,
    blueInvalidCnt: 0,
    blueInvoiceCnt: 0,
    redInvalidCnt: 0,
    redInvoiceCnt: 0,
    invoiceDateBegin: '',
    invoiceDateEnd: '',
    invoiceType: ['增值税普通纸质发票', '增值税专用纸质发票', '增值税普通电子发票', '增值税专用电子发票'],
    invoiceTypeIndex: 0,
    invoiceStatus: 3
  },

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

  //获取企业信息(当前登录用户的默认企业信息)
  getUser() {
    this.getMonthFirstDay()
    this.getMonthLastDay()
    this.setData({
      userInfo: utils.getInfo(),
      company:utils.getCurrEntInfo()
    })
    this.getList()
    // utils.getCompany().then(res => {
    //   let result = res.data
    //   this.setData({
    //     company: result
    //   })
    //   this.getList()
    // })
  },

  //获取待开抬头
  getList(type) {
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
      mobile: this.data.userInfo.mobile,
      invoiceDateBegin: this.data.invoiceDateBegin + ' 00:00:00',
      invoiceDateEnd: this.data.invoiceDateEnd + ' 23:59:59',
      invoiceCategory: this.data.invoiceStatus,
      reqChannel: 5
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoiceExt/reports',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        this.setData({
          list: res.data.reportsData,
          pages: 1,
          finish: true,
          blankInvalidCnt: res.data.blankInvalidCnt,
          blueInvalidCnt: res.data.blueInvalidCnt,
          blueInvoiceCnt: res.data.blueInvoiceCnt,
          redInvalidCnt: res.data.redInvalidCnt,
          redInvoiceCnt: res.data.redInvoiceCnt
        })
      }
    })
  },
  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
      }]
    }
    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/taitou/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('删除成功')
      }
    })
  },

  //点击开票
  toTicket(e) {
    let index = e.currentTarget.dataset.index
    let item = this.data.list[index]
    wx.setStorageSync('ticketMakeData', item)
    wx.navigateTo({
      url: '/pages/ticketMake/ticketMake'
    })
  },
  // 获取当月第一天
  getMonthFirstDay() {
    var date = new Date();
    date.setDate(1);
    var month = parseInt(date.getMonth() + 1);
    var day = date.getDate();
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    this.setData({
      invoiceDateBegin: date.getFullYear() + '-' + month + '-' + day
    })
  },
  // 获取当月最后一天
  getMonthLastDay() {
    var endDate = new Date();
    var currentMonth = endDate.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(endDate.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    var lastTime = new Date(nextMonthFirstDay - oneDay);
    var endMonth = parseInt(lastTime.getMonth() + 1);
    var endDay = lastTime.getDate();
    if (endMonth < 10) {
      endMonth = '0' + endMonth
    }
    if (endDay < 10) {
      endDay = '0' + endDay
    }
    this.setData({
      invoiceDateEnd: endDate.getFullYear() + '-' + endMonth + '-' + endDay
    })
  },
  //设置开始结束时间
  setStart(e) {
    let invoiceDateBegin = e.detail.value
    this.setData({
      invoiceDateBegin
    })
    this.toFresh()
  },
  //设置结束时间
  setEnd(e) {
    let invoiceDateEnd = e.detail.value
    this.setData({
      invoiceDateEnd
    })
    this.toFresh()
  },
  //发票类型
  setInvoiceType(e) {
    let value = Number(e.detail.value)
    let status = ''
    if (value === 0) {
      status = 3
    }
    if (value === 1) {
      status = 4
    }
    if (value === 2) {
      status = 1
    }
    if (value === 3) {
      status = 2
    }
    this.setData({
      invoiceTypeIndex: value,
      invoiceStatus: status
    })
    this.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'
    }
  }
})