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

  /**
   * 页面的初始数据
   */
  data: {
    seller: '', //销方信息
    recordId: '',
    belongEntTaxId: '',
    status: '',
    userInfo: '',
    buyer: '',
    invoiceCategory: 1,
    cmdParamObject: '', //详情数据
    curValue: 3, //接收方式
    interactTypeDetail: '微信卡包',
    payees: ['手动输入'], //收款人
    payeesIndex: '',
    payeesText: '',
    reviewers: ['手动输入'], //复核人
    reviewersIndex: '',
    reviewersText: '',
    drawers: ['手动输入'], //开票人
    drawersIndex: '',
    drawersText: '',
  },

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

  //提交开票
  submitData() {
    let cmdParamObject = this.data.cmdParamObject.REQUEST_COMMON_FPKJ
    //接收方式
    let interactTypeDetail = utils.trimAll(this.data.interactTypeDetail)
    if (interactTypeDetail === '') {
      utils.toast('接收方式不能为空')
      return
    } else {
      if (this.data.curValue === 1) {
        if (!utils.testEmail(interactTypeDetail)) {
          utils.toast('接收方式请填写正确的邮箱')
          return
        }
      }
      if (this.data.curValue === 2) {
        if (!utils.testMobile(interactTypeDetail)) {
          utils.toast('接收方式请填写正确的手机号码')
          return
        }
      }
    }
     //复核人，收款人, 开票人
     let SKR = ''
     let payeesIndex = this.data.payeesIndex
     if (payeesIndex !== '') {
       if (payeesIndex === 0) {
         SKR = utils.trimAll(this.data.payeesText)
       } else {
         SKR = this.data.payees[payeesIndex]
       }
     }
     let FHR = ''
     let reviewersIndex = this.data.reviewersIndex
     if (reviewersIndex !== '') {
       if (reviewersIndex === 0) {
         FHR = utils.trimAll(this.data.reviewersText)
       } else {
         FHR = this.data.reviewers[reviewersIndex]
       }
     }
     let KPR = ''
     let drawersIndex = this.data.drawersIndex
     if (drawersIndex !== '') {
       if (drawersIndex === 0) {
         KPR = utils.trimAll(this.data.drawersText)
       } else {
         KPR = this.data.drawers[drawersIndex]
       }
     }
    let params = {
      mobile: this.data.userInfo.mobile,
      todoRecordId: this.data.recordId,
      reqChannel: 5,
      interactType: this.data.curValue,
      interactTypeDetail: utils.trimAll(this.data.interactTypeDetail),
      cmdParam: {
        "REQUEST_COMMON_FPKJ": {
          "SBLX": this.data.seller.defaultDeviceInfo.deviceType,
          "SBBH": this.data.seller.defaultDeviceInfo.taxDiscId,
          "KPLX":"0",
          "FPZL":1,
          "TTLX": this.data.buyer.customerType,
          "KPF_NSRSBH": cmdParamObject.KPF_NSRSBH,
          "KPF_MC": cmdParamObject.KPF_MC,
          "KPF_DZ": cmdParamObject.KPF_DZ,
          "KPF_DH": cmdParamObject.KPF_DH,
          "KPF_YHZH":cmdParamObject.KPF_YHZH,
          "KPF_KHHMC":cmdParamObject.KPF_KHHMC,
          "SPF_NSRSBH": utils.trimAll(this.data.buyer.customerEntTaxId),
          "SPF_MC": utils.trimAll(this.data.buyer.customerName),
          "SPF_DH": utils.trimAll(this.data.buyer.customerMobile),
          "SPF_DZ": utils.trimAll(this.data.buyer.address),
          "SPF_KHHMC": utils.trimAll(this.data.buyer.bankName),
          "SPF_YHZH": utils.trimAll(this.data.buyer.bankAccountNo),
          "KPR": KPR,
          "SKR": SKR,
          "FHR": FHR,
          "JSHJ": cmdParamObject.JSHJ,
          "HJJE": cmdParamObject.HJJE,
          "HJSE": cmdParamObject.HJSE,
          "BZ": utils.trimAll(this.data.buyer.remark),
          "TSPZ": cmdParamObject.TSPZ,
          "COMMON_FPKJ_XMXXS": {
            "COMMON_FPKJ_XMXX": cmdParamObject.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX
          }
        }
      }
    }
    utils.axios({
      method: 'post',
      url: '/sys/fapiao/todo/commit',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '提交开票中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        wx.redirectTo({
          url: '/pages/ticketWaiting/success'
        })
      }
    })
  },

  //购方企业信息
  getUser() {
    this.setData({
      buyer: wx.getStorageSync('ticketWaitingData'),
      seller:utils.getCurrEntInfo(),
      userInfo:utils.getInfo()
    })
    this.setData({
      curValue: this.data.buyer.interactType,
      interactTypeDetail: this.data.buyer.customerEmail
    })
    let payees = this.data.payees
    if (this.data.seller.payees) {
      let arr = this.data.seller.payees.split(',')
      payees = payees.concat(arr)
      this.setData({
        payees
      })
    }
    let reviewers = this.data.reviewers
    if (this.data.seller.reviewers) {
      let arr = this.data.seller.reviewers.split(',')
      reviewers = reviewers.concat(arr)
      this.setData({
        reviewers
      })
    }
    let drawers = this.data.drawers
    if (this.data.seller.drawers) {
      let arr = this.data.seller.drawers.split(',')
      arr.push(this.data.userInfo.userName)
      drawers = drawers.concat(arr)
      this.setData({
        drawers
      })
    }
    // utils.getCompany().then(res => {
    //   this.setData({
    //     seller: res.data
    //   })
    // })
  },
//收款人，复核人, 开票人
setPayees(e) {
  let index = Number(e.detail.value)
  this.setData({
    payeesIndex: index
  })
},
getPayees(e) {
  let value = e.detail.value
  this.setData({
    payeesText: value
  })
},
setReviewers(e) {
  let index = Number(e.detail.value)
  this.setData({
    reviewersIndex: index
  })
},
setDrawers(e) {
  let index = Number(e.detail.value)
  this.setData({
    drawersIndex: index
  })
},
getReviewers(e) {
  let value = e.detail.value
  this.setData({
    reviewersText: value
  })
},
getDrawers(e) {
  let value = e.detail.value
  this.setData({
    drawersText: value
  })
},
  //获取详情
  getDetail() {
    let params = {
      recordId: this.data.recordId,
      belongEntTaxId: this.data.belongEntTaxId
    }
    utils.axios({
      method: 'get',
      url: '/sys/fapiao/todo/findCmdParam',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        console.log(res);
        this.setData({
          cmdParamObject: res.data.cmdParamObject
        })
      }
    })
  },

  //去详情
  toDetail() {
    wx.navigateTo({
      url: `/pages/ticketWaiting/prodetail?recordId=${this.data.recordId}&belongEntTaxId=${this.data.belongEntTaxId}&status=${this.data.status}`
    })
  },

  //选择接收方式
  selectType(e) {
    let curValue = e.detail.value
    let interactTypeDetail = ''
    if (curValue === 1) {
      if (this.data.buyer.email) {
        interactTypeDetail = this.data.buyer.email
      }
    }
    if (curValue === 2) {
      if (this.data.buyer.customerMobile) {
        interactTypeDetail = this.data.buyer.customerMobile
      }
    }
    if (curValue === 3) {
      interactTypeDetail = '微信卡包'
    }
    if (curValue === 4) {
      interactTypeDetail = '支付宝发票管家'
    }
    this.setData({
      curValue,
      interactTypeDetail
    })
  },

  //以下是获取表单信息的函数
  getremark(e) {
    this.setData({
      ['buyer.remark']: e.detail.value
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})