<script setup lang="ts">
import { onMounted, reactive, ref, unref } from "vue";
import { ElTable } from "element-plus";
import { useResponseHandle } from "/@/hooks";
import { httpList } from "/@/api/InvoiceSales/invoiceApply";
import { inv_columns } from "./../../config/configs";
const paymentList = ref<Array<Record<string, string>>>([]);
const selectOrder = ref<Array<Record<string, string>>>([]);
const visible = ref(false);

const emit = defineEmits(["on-add-order"]);
const loading = ref(false);
const tableRef = ref<InstanceType<typeof ElTable>>(null);
const responseHandle = useResponseHandle();

const pagination = reactive({
  pageSize: 10,
  currentPage: 1,
  total: 0
});

async function requestPaymentList() {
  const { pageSize: size, currentPage: page } = pagination;
  loading.value = true;
  const { code, message, data } = await httpList({
    status: "3",
    size,
    page
  });

  responseHandle({
    code,
    message,
    handler: () => {
      console.log(data.count);
      pagination.total = data.count;
      paymentList.value = data.list;
    }
  });

  loading.value = false;
}

function handleConfirm() {
  visible.value = false;
  emit("on-add-order", unref(selectOrder.value[0]));
}

function handleSelectionChange(values) {
  if (values.length > 1) {
    const value = values.pop();
    tableRef.value.clearSelection();
    tableRef.value.toggleRowSelection(value, true);
    selectOrder.value = [value];
  } else {
    selectOrder.value = [values[0]];
  }
}

defineExpose({
  onDisplay: () => (visible.value = true)
});

onMounted(() => requestPaymentList());
</script>

<template>
  <el-dialog v-model="visible" title="发票列表" center>
    <el-table
      v-loading="loading"
      border
      ref="tableRef"
      :data="paymentList"
      size="small"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="(col, index) in inv_columns"
        :key="index"
        v-bind="col"
        show-overflow-tooltip
      />
    </el-table>

    <el-pagination
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="requestPaymentList"
    />

    <div w-full flex justify-end mt-2>
      <el-button type="primary" size="small" @click="handleConfirm"
        >确定</el-button
      >
      <el-button size="small" @click="() => (visible = false)">取消</el-button>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-table__header) {
  .el-checkbox {
    display: none;
  }
}
</style>
