<script setup lang="ts">
import { onMounted, ref, unref } from "vue";
import { ElTable } from "element-plus";
import { useResponseHandle } from "/@/hooks";
import { httpTradeList } from "/@/api/InvoiceSales/refund";
import { capital_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 tableRef = ref<InstanceType<typeof ElTable>>(null);
const responseHandle = useResponseHandle();

//初始化订单对账列表
async function requestPaymentList() {
  const { code, message, data } = await httpTradeList({
    status: "2"
  });

  responseHandle({
    code,
    message,
    handler: () => {
      paymentList.value = data.list;
    }
  });
}

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="paymentList"
      size="small"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="(col, index) in capital_columns"
        :key="index"
        v-bind="col"
        show-overflow-tooltip
      />
    </el-table>

    <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>
