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

import { computed } from 'vue';

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

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

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

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

interface RowType {
  id: string;
  productId: string;
  year: string;
  title: string;
}

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

const { formOptions } = useCardListForm({
  schema: [
    {
      component: 'Input',
      componentProps: { allowClear: true },
      fieldName: 'title',
      defaultValue: '',
      label: '培训班名称',
    },
  ],
});

const { Grid, cancelCheck, setChecked } = useCardListGrid<RowType, CreateOrderType>({
  payment: computed(() => props.payment),
  selectedData: computed(() => props.selectedData),
  rowKey: 'productId',
  rowKeyPrefix: props.tabKey,
  columns: [
    { type: 'checkbox', width: 60 },
    { field: 'title', title: '培训班名称', minWidth: 200 },
    { field: 'year', title: '年份', minWidth: 100 },
  ],
  query: (params) =>
    reconciliateBillsApi.getTrainingClassList({
      ...params,
      paymentId: props.payment?.id || 0,
    }),
  transformData,
  emit,
  formOptions,
});

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

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