// pages/order/invoicingApply/index.js
const util = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceInfo: [], //开票信息展示包含password和金额的数组
    password: [], //保存password的数组
    sumPrice: 0,
    company: '',
    dutyNumber: '', //税号
    email: '',
    showconsumptionCode: false,
    type: 1, //消费码//1开票金额发生变更
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.invoiceInfo = JSON.parse(options.queryBean)
    let arr = this.data.invoiceInfo
    // console.log(arr);
    // 开票信息显示数组
    let invoiceInfoPassword = []
    arr.forEach(e => {
      invoiceInfoPassword.push({
        password: e.password,
        price: e.income,
        attendantNames: e.attendantNames
      })
    })
    console.log(invoiceInfoPassword);
    let sumPrice = []
    invoiceInfoPassword.forEach((v) => {
      sumPrice.push(v.price),
        this.data.password.push(v.password)
    })
    // console.log(sumPrice);
    let priceSum = sumPrice.reduce((acc, cur) => {
      return acc + cur
    }, 0)
    this.setData({
      invoiceInfo: invoiceInfoPassword,
      sumPrice: priceSum.toFixed(2),
    })
    this.invoicingApply()
  },
  //输入事件
  bindinput(e) {
    let type = e.currentTarget.dataset.type
    let value = e.detail.value
    console.log(value);
    if (type == 'company') {
      this.setData({
        company: value
      })
    }
    if (type == 'dutyNumber') {
      this.setData({
        dutyNumber: value
      })
    }
    if (type == 'email') {
      this.setData({
        email: value
      })
    }
  },
  closeTips() {
    let type = this.data.type
    console.log(type);
    this.setData({
      showconsumptionCode: false
    })
    wx.navigateBack({
      delta: 1
    })
  },
  preImg(e) {
    console.log(e)
    let url="https://img.bbztx.com/miniProgram/teamLeader/invoiceSample.png"
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  // 提交
  submit() {
    let that = this
    if (util.isEmpty(that.data.company)) {
      util.showTips('公司名称不能为空')
    } else if (util.isEmpty(that.data.dutyNumber)) {
      util.showTips('税号不能为空')
    } else if (util.isEmpty(that.data.email)) {
      util.showTips('邮箱不能为空')
    } else {
      // console.log(this.data.showconsumptionCode);
      // console.log(that.data.company, that.data.dutyNumber, that.data.email);
      let data = {}
      data.passwords = that.data.password
      data.type = 0
      data.company = that.data.company
      data.dutyNumber = that.data.dutyNumber
      data.email = that.data.email
      data.isUpdate = false
      data.price = that.data.sumPrice
      util.ajax({
        func: "articleInvoice/save",
        data,
        method: 'POST',
        load:false,
      }, ({
        data,
        code,
        reason
      }) => {
        if (code == 0) {
          console.log(data);
          wx.redirectTo({
            url: '/pages/order/subSuccessful/index',
          })
        } else if (code == -12) {
          this.setData({
            type: 0,
            showconsumptionCode: true
          })
        } else if (code == -13) {
          this.setData({
            type: 1,
            showconsumptionCode: true
          })
        }
      })
    }


  },
  // 申请开票回显
  invoicingApply() {
    let that = this
    // let passwords = ['26719439']
    // console.log(that.data.password.toString().replace('[]', ''));
    util.ajax({
      func: "articleInvoice/getArticleInvoiceByPassword",
      data: {
        passwords: that.data.password.toString().replace('[]', ''),
      },
      load:false,
    }, function ({
      data,
      code
    }) {
      if (code == 0) {
        that.setData({
          company: data.company,
          dutyNumber: data.dutyNumber,
          email: data.email
        })
      } else {
        util.showTips(res.reason);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})