// pages/listDel/index.js

import invoice from '../../../api/invoice'
const cache = require('../../../utils/cache.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // x轴方向的偏移
    x: 0,
    // 当前x的值
    currentX: 0,
    phone: '',
    invoiceList: [],
    way: ''
  },

  handleMovableChange: function (e) {
    // this.data.currentX = e.detail.x;
    this.data.currentX = e.detail.x;
  },

  handleTouchend: function (e) {
    this.isMove = true;
    if (this.data.currentX < -46) {
      this.data.x = -112;
      this.setData({
        x: this.data.x
      });
    } else {
      this.data.x = 0;
      this.setData({
        x: this.data.x
      });
    }
  },

  //添加发票
  goToAddAinvoice(e) {
    // wx.navigateTo({
    //   url: '../../groupmeallist/addinvoices/addinvoices',
    // })

    let way = e.currentTarget.dataset.way;
    let invoiceId = e.currentTarget.dataset.index;
    console.log(invoiceId)
    if (invoiceId >= 0) {
      wx.navigateTo({
        url: '../../groupmeallist/addinvoices/addinvoices?type=' + way + '&invoiceId=' + invoiceId,
      })
    } else {

      wx.navigateTo({
        url: '../../groupmeallist/addinvoices/addinvoices?type=' + way,
      })
    }
  },
  returncreatesubmit(e) {
    let value = e.currentTarget.dataset.value;
    let idx =e.currentTarget.dataset.idx;
    if(this.data.way == 'order'){
      wx.redirectTo({
        url: '../submitOrder/submitOrder?invoiceValue='+value + '&invoiceId=' + idx,
      })
     
    }

  },

  handleDeleteInvoice(e) {
    let index = e.currentTarget.dataset.index;
    console.log(index)

    invoice.delinvoicebyid({
      mobile: this.data.phone,
      invoiceId: index
    }).then(res => {
      wx.showToast({
        title: '删除成功',
        icon: 'none',
        duration: 3000
      })

      this.getInvoiceList()
    })
  },
  handleTouchestart: function (e) {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let way = options.way
    if (way != null) {
      this.setData({
        way: way
      })
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let phone = cache.getMobile()
    this.setData({
      phone: phone
    })

    this.getInvoiceList()

  },


  //获取发票列表
  getInvoiceList() {
    invoice.getinvoicelist({
      mobile: this.data.phone,
    }).then(res => {
      let resdata = res.data
      resdata.forEach(item => {


        item.x = 0;
        item.currentX = 0;

      });



      this.setData({
        invoiceList: res.data
      })



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

  },

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

  },

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

  },

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

  },

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

  }
})