<script setup lang="ts">
import { onMounted, ref, unref } from "vue";
import { ElTable } from "element-plus";
import { useAsync } from "/@/hooks";
import { httpTradeList } from "/@/api/InvoiceSales/refund";
import { CAPITAL_COLUMNS } from "/@/utils/details/tragelog";

const visible = ref(false);
const selectOrder = ref<Array<Record<string, string>>>([]);

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

const { run, data, getPaginationParams, loading, pagination } = useAsync({
  initalData: [],
  isList: true,
  initalPagination: {
    currentPage: 1,
    pageSize: 10,
    total: 0
  }
});

const requestPaymentlist = () => {
  run(httpTradeList({ status: "2", ...getPaginationParams() }));
};

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
      border
      ref="tableRef"
      :data="data"
      size="small"
      row-key="id"
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="({ field }, index) in CAPITAL_COLUMNS"
        :key="index"
        :prop="field"
        show-overflow-tooltip
      />
    </el-table>

    <el-pagination
      :total="pagination.total"
      v-model:current-page="pagination.currentPage"
      @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>
