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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    invoiceDateBegin: "",
    invoiceDateEnd: "",
    invoiceType: [],
    invoiceTypeIndex: '',
    invoiceTypes: [],
    invoiceTypeIndexs: '',
    invoiceStatus: '3',
    certType: [],
    certTypeIndex: '',
    getWay: ["自行领取", '快递配送'],
    getWayIndex: '0',
    getWayStatus: '',
    number: '', //申领数量
    certNo: '', //证件号码
    claimant: '', //经办人
    remark: '发票申领', //备注
    tabIndex: 0,
    userInfo: '',
    company: '',
    handleStatusType: ['申领中', '成功', '失败'],
    handleStatusTypeIndex: '',
    handleStatusTypeInfo: '',
    invoiceData: {},
    selectInvoiceType: {},
    selectInvoiceTypeCode: '',
    selectInvoiceTypeName: {},
    selectInvoiceTypeNameCode: '',
    addressee: '',
    receivingAddress: '',
    mobilePhone: '',
    fixedTelephone: '',
    postalCode: ''
  },

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

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

  //获取申领信息
  getInvoiceInfo() {
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
    }
    utils.axios({
      method: 'get',
      url: '/sys/invoicePurchase/preInfo',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        console.log("测试数据");
        console.log(res);
        this.setData({
          invoiceData: res.data
        })
        let data = []
        res.data.pzhdInfos.forEach((item) => {
          let dataJason = {
            id: item.invoiceCateCode,
            name: item.invoiceCateName
          }
          let i = 0
          data.forEach((invoiceType) => {
            if (dataJason.id === invoiceType.id) {
              i++
            }
          })
          if (i === 0) {
            data.push(dataJason)
          }
        })
        this.setData({
          invoiceType: data
        })
        let certificates = []
        res.data.papers.forEach((item) => {
          let dataJason = {
            id: item.paperType,
            name: item.paperTypeChn
          }
          certificates.push(dataJason)
        })
        this.setData({
          certType: certificates,
          certNo: res.data.operatorInfos[0].paperNumber,
          claimant: res.data.operatorInfos[0].operatorName
        })
        if (res.data.operatorInfos.length > 0) {
          let id = res.data.operatorInfos[0].paperType
          this.data.certType.forEach((item, i) => {
            if (item.id === id) {
              this.setData({
                certTypeIndex: i
              })
              return
            }
          })
        }
      }
    })
  },
  //发票类型
  setInvoiceType(e) {
    let value = Number(e.detail.value)
    this.setData({
      invoiceTypeIndex: value,
      selectInvoiceType: this.data.invoiceType[value]
    })
    let data = []
    this.data.invoiceData.pzhdInfos.forEach((item) => {
      if (this.data.invoiceType[value].name === item.invoiceCateName) {
        let dataJason = {
          id: item.invoiceKindCode,
          name: item.invoiceKindName
        }
        data.push(dataJason)
      }
    })
    this.setData({
      invoiceTypes: data
    })
  },

  //发票类型名称
  setInvoiceTypeName(e) {
    let value = Number(e.detail.value)
    this.setData({
      invoiceTypeIndexs: value,
      selectInvoiceTypeName: this.data.invoiceTypes[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
    })
  },
  getaddressee(e) {
    this.setData({
      addressee: e.detail.value
    })
  },
  getreceivingAddress(e) {
    this.setData({
      receivingAddress: e.detail.value
    })
  },
  getmobilePhone(e) {
    this.setData({
      mobilePhone: e.detail.value
    })
  },
  getfixedTelephone(e) {
    this.setData({
      fixedTelephone: e.detail.value
    })
  },
  getpostalCode(e) {
    this.setData({
      postalCode: e.detail.value
    })
  },
  //申领
  submitData() {
    if (this.data.invoiceTypeIndex === '') {
      utils.toast('发票类别不能为空')
      return
    }
    if (this.data.invoiceTypeIndexs === '') {
      utils.toast('发票种类不能为空')
      return
    }
    if (this.data.number === '') {
      utils.toast('申领数量不能为空')
      return
    }
    if (this.data.certTypeIndex === '') {
      utils.toast('证件类型不能为空')
      return
    }
    if (this.data.certNo === '') {
      utils.toast('证件号码不能为空')
      return
    }
    if (this.data.claimant === '') {
      utils.toast('经办人不能为空')
      return
    }
    if (this.data.getWayIndex === '') {
      utils.toast('领票方式不能为空')
      return
    }
    if (this.data.getWayStatus === 2) {
      if (this.data.addressee === '') {
        utils.toast('收件人不能为空')
        return
      }
      if (this.data.receivingAddress === '') {
        utils.toast('快递地址不能为空')
        return
      }
      if (this.data.mobilePhone === '') {
        utils.toast('移动电话不能为空')
        return
      }
      if (this.data.fixedTelephone === '') {
        utils.toast('固定电话不能为空')
        return
      }
      if (this.data.postalCode === '') {
        utils.toast('邮政编码不能为空')
        return
      }
    }
    if (this.data.remark === '') {
      utils.toast('申请说明不能为空')
      return
    }
    let params = {
      entTaxId: this.data.company.entTaxId,
      deviceType: this.data.company.defaultDeviceInfo.deviceType,
      taxDiscId: this.data.company.defaultDeviceInfo.taxDiscId,
      invoiceKindCode: this.data.invoiceTypes[this.data.invoiceTypeIndex].id,
      invoiceKindName: this.data.invoiceTypes[this.data.invoiceTypeIndex].name,
      invoiceCateCode: this.data.invoiceType[this.data.invoiceTypeIndexs].id,
      invoiceCateName: this.data.invoiceType[this.data.invoiceTypeIndexs].name,
      applyNum: utils.trimAll(this.data.number),
      operatorName: utils.trimAll(this.data.claimant),
      paperType: this.data.certType[this.data.certTypeIndex].id,
      paperTypeChn: this.data.certType[this.data.certTypeIndex].name,
      paperNumber: utils.trimAll(this.data.certNo),
      remark: utils.trimAll(this.data.remark),
      getWay: this.data.getWayStatus,
      receiverName: utils.trimAll(this.data.addressee),
      receiverAddress: utils.trimAll(this.data.receivingAddress),
      mobilePhoneNumber: utils.trimAll(this.data.mobilePhone),
      zipCode: utils.trimAll(this.data.postalCode),
      fixedPhoneNumber: utils.trimAll(this.data.fixedTelephone),
      mobile: this.data.userInfo.mobile,
      reqChannel: 5
    }
    utils.axios({
      method: 'post',
      url: '/sys/invoicePurchase/apply',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '申领中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.toast('申领成功')
        this.toBack()
      }
    })
  },
  //领取方式
  setGetWay(e) {
    let value = Number(e.detail.value)
    let status = ''
    if (value === 0) {
      status = 1
    }
    if (value === 1) {
      status = 2
    }
    this.setData({
      getWayIndex: value,
      getWayStatus: status
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})