const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    allchecked: '',
    checkboxItems: [],
    isAllChecked: false,
    idChecked: false,
    reduceNum: 0.00,
    consumpCode: 0,
    show: {
      growup: false,
    },
  },

  // 去开票页面
  goInvoice(e) {
    let arr = this.data.checkboxItems
    let params = []
    let selectList = arr.filter((e, i) => {
      return e.checked == true
    })
    console.log(selectList);
    let paramPswordList = []
    selectList.forEach(e => {
      params.push(e)
      paramPswordList.push(e.password)
    })
    console.log(paramPswordList);
    var queryBean = JSON.stringify(params)
    let data = {}
    data.passwords = paramPswordList.toString().replace('[]', '')
    if (selectList.length != 0) {
      util.ajax({
        func: "articleInvoice/determinePassword",
        data,
        load: false
      }, ({
        code,
        data,
        reason
      }) => {
        if (code == 0) {
          console.log(data);
          wx.navigateTo({
            url: '/pages/order/invoicingApply/index?queryBean=' + queryBean,
          })
        } else if (code == -12) {
          util.showTips(reason)
        }
      })

    } else {
      wx.showToast({
        title: '请至少选择一个消费码',
        icon: 'none'
      })
    }
  },
  // 点击跳转到开票记录页面
  invoicingRecord() {
    let detail = JSON.stringify(this.data.checkboxItems)
    // console.log(detail);
    wx.navigateTo({
      url: '/pages/order/invoicingRecord/index?detail=' + detail,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInvoice()
  },
  // 点击全选
  // 单选---->全选
  checkboxChange(e) {
    let arr = this.data.checkboxItems;
    let index = e.currentTarget.dataset.index
    let slectList = []
    let sum = []
    if (arr[index].checked) {
      arr[index].checked = false
      slectList = arr.filter(vl => {
        return vl.checked == true
      })
      slectList.forEach(ev => {
        sum.push(ev.income)
      })
      console.log(slectList);
      console.log(sum);
      let sumNum = sum.reduce((acc, cur) => {
        return acc + cur
      }, 0)
      console.log(sumNum);
      this.setData({
        reduceNum: sumNum.toFixed(2) || 0,
        consumpCode: slectList.length
      })
    } else {
      arr[index].checked = true
      slectList = arr.filter(vl => {
        return vl.checked == true
      })
      slectList.forEach(ev => {
        sum.push(ev.income)
      })
      let sumNum = sum.reduce((acc, cur) => {
        return acc + cur
      })
      // console.log(sumNum);
      this.setData({
        reduceNum: sumNum.toFixed(2),
        consumpCode: slectList.length
      })
    }
    let bol = arr.every((el => {
      return el.checked == true
    }))
    if (bol) {
      this.setData({
        isAllChecked: true
      })
    } else {
      this.setData({
        isAllChecked: false
      })
    }
  },
  // 全选---单选
  allChecked() {
    // 列表中是否都选中
    let arr = this.data.checkboxItems
    var bol = arr.every(el => el.checked == true)
    // console.log(bol);
    if (bol) {
      arr.forEach(el => el.checked = false)
      this.setData({
        checkboxItems: arr,
        reduceNum: 0,
        consumpCode: 0
      })
    } else {
      let reduceNum = []
      arr.forEach((e) => {
        reduceNum.push(e.income)
      })
      // console.log(reduceNum);
      let sum = reduceNum.reduce((acc, cur) => {
        return acc + cur
      }, 0)
      // console.log(sum);
      arr.forEach(el => el.checked = true)
      this.setData({
        checkboxItems: arr,
        reduceNum: sum.toFixed(2),
        consumpCode: arr.length
      })
      // console.log(this.data.reduceNum);
    }
  },
  // 查看规则
  refulePop() {
    this.setData({
      'show.growup': true
    })
  },
  closeTips(){
    this.setData({
      'show.growup': false
    })
  },
  // 获取开具发票详情
  getInvoice() {
    let that = this;
    util.ajax({
      func: "v2/order/getArticleInvoiceByUid",
      load: false
    }, function ({
      data,
      code
    }) {
      if (code == 0) {
        // console.log(data);
        let arr = data
        arr.forEach(e => {
          e.checked = false
        });
        that.setData({
          checkboxItems: arr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      isAllChecked: false,
      allChecked: false,
      consumpCode: 0,
      reduceNum: 0.00
    })
    this.getInvoice()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})