<script setup lang="ts">
import { computed } from 'vue';
import DataTableActions from './data-table-actions.vue';
import DataTableToolbar from '@/components/data-table/DataTableToolbar.vue';
import GroupedTableFooter from '@/components/data-table/GroupedTableFooter.vue';
import { Badge } from '@/components/ui/badge';
import {
    Empty,
    EmptyDescription,
    EmptyHeader,
    EmptyTitle,
} from '@/components/ui/empty';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { usePaginationSummary } from '@/composables/usePaginationSummary';
import { orderStatusBadgeVariant } from '@/constants/order-status';
import { groupedTableRowNumber } from '@/lib/grouped-table';
import type {
    DataTablePagination,
    DataTablePaginationLink,
} from '@/types/data-table';
import type { OrderListItem } from '@/types/order';

const props = defineProps<{
    orders: OrderListItem[];
    firstItem?: number;
    pagination?: DataTablePagination;
    paginationLinks?: DataTablePaginationLink[];
}>();

const search = defineModel<string>('search', { default: '' });

const emit = defineEmits<{
    'filters-reset': [];
}>();

const showingCount = computed(() => props.orders.length);
const paginationSummary = usePaginationSummary(() => props.pagination, showingCount, 'pesanan');

function rowNumber(index: number): number {
    return groupedTableRowNumber(props.firstItem, index);
}
</script>

<template>
    <div class="space-y-4">
        <DataTableToolbar v-model:search="search" @filters-reset="emit('filters-reset')" />

        <div v-if="orders.length" class="space-y-4">
            <div v-for="(order, index) in orders" :key="order.id" class="overflow-hidden rounded-md border">
                <div
                    class="flex flex-col gap-3 border-b bg-muted/30 px-4 py-3 sm:flex-row sm:items-start sm:justify-between">
                    <div class="flex min-w-0 items-start gap-3">
                        <span class="text-muted-foreground w-8 shrink-0 pt-0.5 text-center text-sm tabular-nums">
                            {{ rowNumber(index) }}
                        </span>
                        <div class="min-w-0 space-y-2">
                            <div class="flex flex-wrap items-center gap-2">
                                <h3 class="font-medium leading-tight">
                                    {{ order.order_number }}
                                </h3>
                                <Badge :variant="orderStatusBadgeVariant(order.status)">
                                    {{ order.status_label }}
                                </Badge>
                                <Badge variant="secondary">
                                    {{ order.channel_label }}
                                </Badge>
                                <Badge v-if="order.tiktok_order_id" variant="outline">
                                    ID Pesanan: {{ order.tiktok_order_id }}
                                </Badge>
                                <Badge v-if="order.shopee_order_id" variant="outline">
                                    ID Pesanan: {{ order.shopee_order_id }}
                                </Badge>
                                <Badge v-if="order.is_affiliate" variant="outline">
                                    Afiliasi
                                </Badge>
                            </div>
                            <div class="text-muted-foreground space-y-1 text-sm">
                                <p v-if="order.customer">
                                    {{ order.customer.name }}
                                </p>
                                <p>{{ order.created_at_formatted }}</p>
                                <p>Oleh {{ order.created_by?.profile?.full_name ?? order.created_by?.username }}</p>
                            </div>
                            <div class="flex flex-wrap gap-x-4 gap-y-1 text-sm">
                                <span>Tipe Harga <strong class="text-primary">{{ order.price_type_label
                                        }}</strong></span>
                                <span>Pembayaran <strong class="text-primary">{{ order.payment_type_label
                                        }}</strong></span>
                                <span>Subtotal <strong class="text-primary">{{ order.subtotal_formatted
                                        }}</strong></span>
                                <span>Diskon <strong class="text-primary">{{ order.discount_formatted
                                }}</strong></span>
                                <span v-if="order.nego_price_formatted">Harga Nego <strong class="text-primary">{{ order.nego_price_formatted
                                }}</strong></span>
                                <span>Total <strong class="text-primary">{{ order.total_amount_formatted
                                }}</strong></span>
                                <span v-if="order.marketplace_settings_snapshot?.total_fee_amount" class="text-destructive">
                                    Potongan Marketplace <strong class="text-destructive">-{{ order.marketplace_settings_snapshot.total_fee_amount_formatted }}</strong>
                                </span>
                                <span v-if="order.marketplace_settings_snapshot?.net_amount">
                                    Total Bersih <strong class="text-green-600">{{ order.marketplace_settings_snapshot.net_amount_formatted }}</strong>
                                </span>
                            </div>
                            <p v-if="order.notes" class="text-muted-foreground text-sm">
                                {{ order.notes }}
                            </p>
                        </div>
                    </div>

                    <div class="flex shrink-0 items-center justify-end gap-2 sm:pt-0.5">
                        <DataTableActions :order="order" />
                    </div>
                </div>

                <Table>
                    <TableHeader>
                        <TableRow>
                            <TableHead>Produk</TableHead>
                            <TableHead>Varian</TableHead>
                            <TableHead>Jumlah</TableHead>
                            <TableHead>Harga Satuan</TableHead>
                            <TableHead>Subtotal</TableHead>
                        </TableRow>
                    </TableHeader>
                    <TableBody>
                        <TableRow v-if="!order.items.length" :key="`${order.id}-empty`">
                            <TableCell colspan="5" class="text-muted-foreground">
                                Belum ada item
                            </TableCell>
                        </TableRow>
                        <TableRow v-for="item in order.items" :key="item.id">
                            <TableCell class="font-medium">
                                {{ item.product_variant?.product?.name }}
                            </TableCell>
                            <TableCell>
                                {{ item.product_variant?.name }}
                            </TableCell>
                            <TableCell class="tabular-nums">
                                {{ item.quantity_formatted }}
                            </TableCell>
                            <TableCell class="tabular-nums">
                                {{ item.unit_price_formatted }}
                            </TableCell>
                            <TableCell class="tabular-nums font-medium">
                                {{ item.subtotal_formatted }}
                            </TableCell>
                        </TableRow>
                    </TableBody>
                </Table>
            </div>
        </div>

        <div v-else class="rounded-md border px-6 py-10">
            <Empty>
                <EmptyHeader>
                    <EmptyTitle>Data tidak ditemukan</EmptyTitle>
                    <EmptyDescription>
                        Silakan lakukan pencarian untuk menemukan data yang Anda cari.
                    </EmptyDescription>
                </EmptyHeader>
            </Empty>
        </div>

        <GroupedTableFooter
            :summary="paginationSummary"
            :pagination="pagination"
            :pagination-links="paginationLinks"
        />
    </div>
</template>
