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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    invoiceDateBegin: "",
    invoiceDateEnd: "",
    invoiceType: ['增值税普通纸质发票', '增值税专用纸质发票', '增值税普通电子发票', '增值税专用电子发票'],
    invoiceTypeIndex: 0,
    invoiceStatus: '3',
    certType: ['身份证', '护照'],
    certTypeIndex: '',
    getWay: ['自行领取', '快递配送'],
    getWayIndex: '',
    number: '', //申领数量
    certNo: '', //证件号码
    claimant: '', //经办人
    remark: '', //备注
    tabIndex: 0,
    userInfo: '',
    company: '',
    handleStatusType: ['申领中', '成功', '失败'],
    handleStatusTypeIndex: '',
    handleStatusTypeInfo: '',
    distribute: ['待分发', '分发中', '已分发'],
    distributeIndex: '0',
    distributeList: [],
    pageNums: 1,
    pageSizes: 10,
    pagesTotal: '',
    distributeFinish: false,
    distributeInvoiceTypeIndex: 0,
    distributeInvoiceStatus: '3',
    distributeInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      invoiceDateBegin: utils.getNowDate(new Date()),
      invoiceDateEnd: utils.getNowDate(new Date())
    })
    this.getUser()
  },

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

  //获取申领记录
  getList(type) {
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
      invoiceCategory: this.data.invoiceStatus,
      applyDateBegin: this.data.invoiceDateBegin,
      applyDateEnd: this.data.invoiceDateEnd,
      handleStatus: this.data.handleStatusTypeInfo,
      mobile: this.data.userInfo.mobile,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoicePurchase/statusInfos',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        this.setData({
          list: res.data.statusInfoVOs
        })
        if (res.data.statusInfoVOs.length > 0) {
          this.setData({
            pages: 1
          })
        } else {
          this.setData({
            pages: 0
          })
        }
        this.setData({
          finish: true
        })
      }
    })
  },
  //获取分发记录
  getDistributeList(type) {
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
      invoiceCategory: this.data.distributeInvoiceStatus,
      distributeStatus: this.data.distributeIndex,
      pageNo: this.data.pageNums,
      pageSize: this.data.pageSizes,
      mobile: this.data.userInfo.mobile,
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoicePurchase/distributeInfos',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        this.setData({
          distributeInfo: res.data
        })
        if (res.data.distributeVOs.length > 0) {
          this.setData({
            pagesTotal: 1
          })
        } else {
          this.setData({
            pagesTotal: 0
          })
        }
        this.setData({
          distributeFinish: true
        })
      }
    })
  },
  //发票分发
  setDistribute(type) {
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
      invoiceCateCode: this.data.distributeInfo.distributeVOs[0].invoiceCateCode,
      surplusNum: this.data.distributeInfo.distributeVOs[0].surplusNum,
      invoiceCode: this.data.distributeInfo.distributeVOs[0].invoiceCode,
      invoiceBeginNo: this.data.distributeInfo.distributeVOs[0].invoiceBeginNo,
      invoiceEndNo: this.data.distributeInfo.distributeVOs[0].invoiceEndNo,
      distributeNum: this.data.distributeInfo.distributeVOs[0].surplusNum,
      mobile: this.data.userInfo.mobile,
      reqChannel: 5
    }
    utils.axios({
      method: 'post',
      url: '/sys/invoicePurchase/distribute',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        if (res.code === 0) {
          utils.toast(res.data.statusMemo)
        }
      }
    })
  },
  toFresh(status) {
    if (status) {
      this.setData({
        pageNums: 1,
        pagesTotal: '',
        distributeFinish: false
      })
    } else {
      this.setData({
        pageNum: 1,
        pages: '',
        finish: false
      })
    }
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    if (status) {
      this.getDistributeList('toFresh')
    } else {
      this.getList('toFresh')
    }
  },
  // 发票申请
  applyInvoice(e) {
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/ticketGet/applyInvoice?index=${index}`
    })
  },
  //取消
  toBack() {
    wx.navigateBack()
  },

  //发票类型
  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()
  },
  //发票类型
  setDistributeInvoiceType(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({
      distributeInvoiceTypeIndex: value,
      distributeInvoiceStatus: status
    })
    this.toFresh(true)
  },

  //分发状态
  setDistribute(e) {
    let value = Number(e.detail.value)
    this.setData({
      distributeIndex: value
    })
    this.toFresh(true)
  },
  // 申领状态
  setInvoiceHandType(e) {
    let value = Number(e.detail.value)
    this.setData({
      handleStatusTypeIndex: value,
      handleStatusTypeInfo: value
    })
    this.toFresh()
  },
  //证件类型
  setCertType(e) {
    let value = Number(e.detail.value)
    this.setData({
      certTypeIndex: value
    })
  },

  //领取方式
  setGetWay(e) {
    let value = Number(e.detail.value)
    this.setData({
      getWayIndex: value
    })
  },

  //以下是表单信息
  getnumber(e) {
    this.setData({
      number: e.detail.value
    })
  },
  getcertNo(e) {
    this.setData({
      certNo: e.detail.value
    })
  },
  getclaimant(e) {
    this.setData({
      claimant: e.detail.value
    })
  },
  getremark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  clearStart() {
    this.setData({
      invoiceDateBegin: ''
    })
    this.toFresh()
  },
  //设置开始结束时间
  setStart(e) {
    let invoiceDateBegin = e.detail.value
    this.setData({
      invoiceDateBegin
    })
    this.toFresh()
  },
  //设置结束时间
  setEnd(e) {
    let invoiceDateEnd = e.detail.value
    this.setData({
      invoiceDateEnd
    })
    this.toFresh()
  },

  //切换
  switchTab(e) {
    let tabIndex = e.currentTarget.dataset.index
    this.setData({
      tabIndex
    })
  },

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

  },

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

  },

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

  },

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

  },

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

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

  },

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