<script lang="ts" setup>
import type { CreateOrderType, PaymentRowType } from '../types';

import { computed } from 'vue';

import { reconciliateBillsApi } from '#/api/posting';

import { useCardListGrid } from '../composables/useCardListGrid';

const props = withDefaults(
  defineProps<{
    payment: PaymentRowType | undefined;
    selectedData: CreateOrderType[] | undefined;
    tabKey: string;
  }>(),
  {},
);

const emit = defineEmits(['completeCheck', 'deletedChecked']);

interface RowType {
  type: string;
  title: string;
}

function transformData(rows: RowType[]) {
  return rows.map((row) => ({
    id: row.type,
    courseId: '',
    tel: '',
    come: '2',
    orderType: '1',
    paymentId: props.payment?.id || '',
    productName: row.title,
    productId: '',
    catalogueId: '',
    orderMoney: '',
    realMoney: String(props.payment?.fee) || '',
    districtMoney: '',
    startTime: props.payment?.ctime || '',
    endTime: '',
    vipType: row.type.slice(1),
  }));
}

const { Grid, cancelCheck, setChecked } = useCardListGrid<RowType, CreateOrderType>({
  payment: computed(() => props.payment),
  selectedData: computed(() => props.selectedData),
  rowKey: 'type',
  rowKeyPrefix: props.tabKey,
  columns: [
    { type: 'checkbox', width: 60 },
    { field: 'title', title: 'VIP名称', minWidth: 150 },
  ],
  query: (params) =>
    reconciliateBillsApi.getVipList({
      ...params,
      paymentId: props.payment?.id || 0,
    }),
  transformData,
  emit,
  gridOptions: {
    pagerConfig: {
      enabled: false,
    },
  },
});

defineExpose({ cancelCheck, setChecked });
</script>

<template>
  <div class="orders-container h-full">
    <Grid />
  </div>
</template>
