// pages/invoiceList/index.js
import { deleteInvoiceInfo } from "../../service/invoice";
import { alertInfo, alertSuccess, wxModal } from "../../utils/index";
import request from '../../utils/request'
const { OK } = request
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    show: false,
    actions: [
      { name: '查看', },
      { name: '编辑', },
      { name: '删除', },
    ],
    selectData: {},
    active: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { tab } = options;
    if (isNaN(Number(tab))) {
      return
    }
    this.setData({
      active: Number(tab)
    })
  },
  operate(e) {
    const { data } = e.currentTarget.dataset;
    this.setData({
      show: true,
      selectData: data
    })
  },
  onClose() {
    this.setData({ show: false, selectData: {} });
  },
  onSelect(e) {
    const type = e.detail.name;
    switch(type) {
      case "编辑":
        break;
      case "查看":
        wx.navigateTo({
          url: '/pages/invoiceDetail/index?id=' + this.data.selectData.id,
        })
        break;
      case "删除":
        this.handleDelete();
        break;
    }
  },
  onChange(e) {
    this.setData({ active: e.detail.index })
  },
  async handleDelete() {
    const { id } = this.data.selectData;
    await wxModal({ content: "确定要删除当前记录吗？" })
    const { code, message } = await deleteInvoiceInfo(id)
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    alertSuccess("删除成功")
    this.init();
  },
  handleCreate() {
    wx.navigateTo({
      url: '/pages/editInvoice/index?id=-1',
    })
  },
})