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

  /**
   * 页面的初始数据
   */
  data: {
    recordId: '',
    proList: [], //项目明细
    seller: '', //销方信息
    buyer: { //购方信息
      address: '',
      bankAccount: '',
      contactPhone: '',
      createBy: '',
      createTime: '',
      customerCode: '',
      customerId: '',
      customerMobile: '',
      customerName: '',
      email: '',
      entTaxId: '',
      lastUpdateBy: '',
      lastUpdateTime: '',
      manageUserId: '',
      parentCode: null,
      remark: null,
      shortCode: null
    },
    bankName: '', //开户行
    interactTypeDetail: '', //interactTypeDetail
    cardList: [{ //交付方式
        text: '邮箱',
        value: 1,
        selected: false
      },
      {
        text: '手机短信',
        value: 2,
        selected: false
      },
      {
        text: '微信卡包',
        value: 3,
        selected: true
      },
      {
        text: '支付宝发票管家',
        value: 3,
        selected: false
      },
      {
        text: '其他',
        value: 99,
        selected: false
      }
    ],
  },

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

  //提交开票
  submitData() {
    let buyer = this.data.buyer
    let params = {
      recordId: this.data.recordId,
      customerEntTaxId: utils.trimAll(buyer.entTaxId),
      customerName: utils.trimAll(buyer.customerName),
      address: utils.trimAll(buyer.address),
      contactPhone: utils.trimAll(buyer.contactPhone),
      bankName: utils.trimAll(this.data.bankName),
      bankAccountNo: utils.trimAll(buyer.bankAccount),
      customerEmail: utils.trimAll(buyer.email),
      customerMobile: utils.trimAll(buyer.customerMobile),
      interactType: '',
      interactTypeDetail: utils.trimAll(this.data.interactTypeDetail)
    }
    if (params.customerName === '') {
      utils.toast('发票抬头不能为空')
      return
    }
    if (params.customerEntTaxId === '') {
      utils.toast('税号不能为空')
      return
    }
    if (params.customerEmail === '') {
      utils.toast('邮箱不能为空')
      return
    }
    if (params.customerMobile === '') {
      utils.toast('手机号码不能为空')
      return
    }
    if (params.interactTypeDetail === '') {
      utils.toast('接收方式不能为空')
      return
    }
    let cardList = this.data.cardList
    for (let i = 0; i < cardList.length; i++) {
      if (cardList[i].selected) {
        params.interactType = cardList[i].value
      }
    }
    utils.axios({
      method: 'post',
      url: '/sys/fapiao/todo/apply',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '提交申请中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.toast('开票申请提交成功')
        wx.redirectTo({
          url: '/pages/ticketWaiting/ticketWaiting'
        })
      }
    })
  },

  //开票基本信息
  getDetail() {
    let params = {
      recordId: this.data.recordId
    }
    utils.axios({
      method: 'get',
      url: '/sys/fapiao/todo/findBaseInfoById',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        this.setData({
          seller: res.data
        })
      }
    })
  },

  //获取项目明细
  getItem() {
    let params = {
      recordId: this.data.recordId
    }
    utils.axios({
      method: 'get',
      url: '/sys/fapiao/todo/findItemsById',
      data: params,
      sendBefore() {
        wx.showNavigationBarLoading()
      },
      complete() {
        wx.hideNavigationBarLoading()
      },
      success: res => {
        this.setData({
          proList: res.data
        })
      }
    })
  },

  //查询购方名字
  toSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },

  //选择接收方式
  selectType(e) {
    let index = Number(e.currentTarget.dataset.index)
    let cardList = this.data.cardList
    for (let i = 0; i < cardList.length; i++) {
      cardList[i].selected = false
    }
    cardList[index].selected = true
    this.setData({
      cardList
    })
  },

  //以下是获取表单信息的函数
  getcustomerName(e) {
    this.setData({
      ['buyer.customerName']: e.detail.value
    })
  },
  getentTaxId(e) {
    this.setData({
      ['buyer.entTaxId']: e.detail.value
    })
  },
  getemail(e) {
    this.setData({
      ['buyer.email']: e.detail.value
    })
  },
  getcustomerMobile(e) {
    this.setData({
      ['buyer.customerMobile']: e.detail.value
    })
  },
  getaddress(e) {
    this.setData({
      ['buyer.address']: e.detail.value
    })
  },
  getcontactPhone(e) {
    this.setData({
      ['buyer.contactPhone']: e.detail.value
    })
  },
  getbankName(e) {
    this.setData({
      bankName: e.detail.value
    })
  },
  getbankAccount(e) {
    this.setData({
      ['buyer.bankAccount']: e.detail.value
    })
  },
  getremark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  getinteractTypeDetail(e) {
    this.setData({
      interactTypeDetail: e.detail.value
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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