<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 {
  courseId: string;
  productId: string;
  price: number;
  productName: string;
}

function transformData(rows: RowType[]) {
  return rows.map((row) => ({
    id: row.productId,
    courseId: row.courseId || '',
    tel: '',
    come: '1',
    orderType: '2',
    paymentId: props.payment?.id || '',
    productName: row.productName,
    productId: row.productId.slice(1),
    catalogueId: '',
    orderMoney: String(row.price),
    realMoney: String(props.payment?.fee) || '',
    districtMoney: '',
    startTime: props.payment?.ctime || '',
    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: 'productName', title: '商品名称', minWidth: 100 },
    { field: 'price', title: '金额', minWidth: 100 },
  ],
  query: reconciliateBillsApi.getWumenCourseBuyList,
  transformData,
  emit,
  formOptions,
});

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

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