// pages/invoice/invoice.js
const app = getApp();
const now = new Date().getFullYear();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    years: [],
    curYear: now,
    tabIndex: 0,//0代表开发票 1代表我的发票
    InvoiceRecordInfo: [],//开票实收信息
    InvoiceInfo: [],//电子发票信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setYears();
    if(options.index){
      this.setData({
        tabIndex:options.index
      })
    }
    this.getInvoiceRecordInfo(this.data.curYear);
    this.getInvoiceInfo(this.data.curYear)
  },
  setYears() {
    const years = [];
    for (let i = now; i >= 1990; i--) {
      years.push(`${i}年`);
    }
    this.setData({
      years
    })
  },
  previewImage(e){
    var current = e.currentTarget.dataset.imgurl;
    wx.previewImage({
      current: current,//当前显示图片的http链接，我这边是把图片转成了base64
      urls: [current] //需要预览的图片http链接列表
    })
  },
  bindPickerChange(e) {
    const year = this.data.years[e.detail.value].substr(0, 4);
    this.setData({
      index: e.detail.value,
      curYear: year
    })
    this.getInfo();
  },
  change(e) {
    this.setData({
      tabIndex: e.target.dataset.current
    })
    this.getInfo();
  },
  getInfo() {
    if (this.data.tabIndex == 0) {
      this.getInvoiceRecordInfo(this.data.curYear)
    } else {
      this.getInvoiceInfo(this.data.curYear)
    }
  },
  toInvoice(e) {
    wx.navigateTo({
      url: `/pages/invoice/pages/tickets/tickets?fee=` + e.currentTarget.dataset.fee+'&payseriesno=' +  e.currentTarget.dataset.payseriesno
    })
  },
  toDetail(e){
    wx.navigateTo({
      url: `/pages/invoice/pages/detail/detail?invoiceId=` + e.currentTarget.dataset.invoiceid+'&invoiceState='+e.currentTarget.dataset.invoiceState
    })
  },
  toOtherPages(e){
    const {
      url
    } = e.target.dataset;
    wx.navigateTo({
      url: `/pages/invoice/pages/${url}/${url}`,
    })
  },
  // GET /invoice/getInvoiceRecordInfo 获取开票实收信息
  getInvoiceRecordInfo(year) {
    app.showLoading();
    app.$http.get('/invoice/getInvoiceRecordInfo', {
      year
    }).then(({ status, data = {} }) => {
      if (status == 0) {
        data.forEach(v => {
          v.fee = app.$util.numberFormat(v.fee, 2);
        })
        this.setData({
          InvoiceRecordInfo: data
        })
      }
      app.hideLoading();
    }).catch(() => {
      app.hideLoading();
    })
  },
  // GET /invoice/getInvoiceInfo  获取电子发票信息
  getInvoiceInfo(year) {
    app.showLoading();
    app.$http.get('/invoice/getInvoiceInfo', {
      year
    }).then(({ status, data = {} }) => {
      if (status == 0) {
        data.forEach(v => {
          v.totalPrintAmount = app.$util.numberFormat(v.totalPrintAmount, 2);
          v.imgUrl = v.imgUrl.split(',')[0]
        })
        this.setData({
          InvoiceInfo: data
        })
      }
      app.hideLoading();
    }).catch(() => {
      app.hideLoading();
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.ticketsTabIndex);
    if(app.ticketsTabIndex != null && app.ticketsTabIndex != undefined) {
      this.setData({
        tabIndex: app.ticketsTabIndex
      })
      app.ticketsTabIndex = null;
    }
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})