<script setup lang="ts">
import { Minus, Plus, Trash2 } from '@lucide/vue';
import { NumberInput } from '@/components/form/number-input';
import { Button } from '@/components/ui/button';
import {
    Empty,
    EmptyDescription,
    EmptyHeader,
    EmptyTitle,
} from '@/components/ui/empty';
import {
    Field,
    FieldLabel,
} from '@/components/ui/field';
import { formatRupiah } from '@/lib/rupiah';
import type { OrderCartItem } from '@/types/order';

defineProps<{
    cart: OrderCartItem[];
    stockQualityLabel: (stockQuality: string) => string;
    lineSubtotal: (item: OrderCartItem) => number;
}>();

const emit = defineEmits<{
    remove: [index: number];
    'adjust-quantity': [index: number, delta: number];
    'sync-quantity': [index: number];
}>();
</script>

<template>
    <div v-if="cart.length === 0" class="rounded-lg border border-dashed text-center text-sm text-muted-foreground">
        <Empty>
            <EmptyHeader>
                <EmptyTitle>Keranjang masih kosong</EmptyTitle>
                <EmptyDescription>
                    Pilih varian produk di sebelah kiri untuk menambahkan ke keranjang.
                </EmptyDescription>
            </EmptyHeader>
        </Empty>
    </div>

    <div v-else class="scrollbar-thin max-h-80 space-y-3 overflow-y-auto overscroll-y-contain">
        <div
            v-for="(item, index) in cart"
            :key="`${item.product_variant_id}-${item.stock_quality}`"
            class="rounded-lg border p-3"
        >
            <div class="flex gap-3">
                <div class="min-w-0 flex-1 space-y-2">
                    <div class="flex items-start justify-between gap-2">
                        <div class="min-w-0">
                            <p class="truncate text-sm font-medium">
                                {{ item.product_name }}
                            </p>
                            <p class="truncate text-xs text-muted-foreground">
                                {{ item.variant_name }}
                                ·
                                {{ item.stock_quality_label ?? stockQualityLabel(item.stock_quality) }}
                            </p>
                        </div>
                        <Button
                            type="button"
                            variant="ghost"
                            size="icon"
                            class="text-destructive hover:text-destructive size-7 shrink-0"
                            @click="emit('remove', index)"
                        >
                            <Trash2 class="size-3.5" />
                        </Button>
                    </div>

                    <Field>
                        <FieldLabel class="text-xs">Jumlah (pcs)</FieldLabel>
                        <div class="flex items-center gap-1">
                            <Button
                                type="button"
                                variant="outline"
                                size="icon"
                                class="size-8 shrink-0"
                                @click="emit('adjust-quantity', index, -1)"
                            >
                                <Minus class="size-3.5" />
                            </Button>
                            <NumberInput
                                v-model="item.quantity"
                                class="h-8 text-center"
                                @change="emit('sync-quantity', index)"
                            />
                            <Button
                                type="button"
                                variant="outline"
                                size="icon"
                                class="size-8 shrink-0"
                                @click="emit('adjust-quantity', index, 1)"
                            >
                                <Plus class="size-3.5" />
                            </Button>
                        </div>
                    </Field>

                    <p class="text-xs text-muted-foreground">
                        Harga satuan Rp {{ formatRupiah(item.unit_price) }}
                    </p>

                    <p class="text-right text-sm font-medium">
                        Rp {{ formatRupiah(lineSubtotal(item)) }}
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>
