@extends('layouts.admin')

@section('title', 'POS Orders')
@section('page-title', 'POS Orders')

@section('content')
<div class="space-y-6">
    <section class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm dark:border-slate-700 dark:bg-slate-900">
        <form method="GET" class="grid gap-4 lg:grid-cols-[2fr_1fr_auto]">
            <input type="text" name="q" value="{{ request('q') }}" placeholder="Invoice #, customer name, or user email"
                   class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100">
            <select name="status" class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100">
                <option value="">All statuses</option>
                @foreach(['completed', 'pending', 'cancelled', 'refunded'] as $s)
                    <option value="{{ $s }}" @selected(request('status') === $s)>{{ ucfirst($s) }}</option>
                @endforeach
            </select>
            <button type="submit" class="rounded-2xl bg-brand-600 px-5 py-3 text-sm font-semibold text-white transition hover:bg-brand-700">Filter</button>
        </form>
    </section>

    <section class="rounded-3xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
        <div class="overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="border-b border-slate-200 bg-slate-50 text-slate-500 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-400">
                    <tr>
                        <th class="px-6 py-4 font-semibold">User</th>
                        <th class="px-6 py-4 font-semibold">Invoice</th>
                        <th class="px-6 py-4 font-semibold">Customer</th>
                        <th class="px-6 py-4 font-semibold">Total</th>
                        <th class="px-6 py-4 font-semibold">Status</th>
                        <th class="px-6 py-4 font-semibold">Date</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-700">
                    @forelse($orders as $order)
                        <tr class="table-row-hover">
                            <td class="px-6 py-4">
                                <div class="font-semibold text-slate-900 dark:text-slate-100">{{ $order->user->name ?? 'Unknown' }}</div>
                                <div class="text-xs text-slate-500">{{ $order->user->email ?? '—' }}</div>
                            </td>
                            <td class="px-6 py-4 font-mono text-xs text-slate-700 dark:text-slate-300">
                                {{ $order->invoice_number ?: ('ORD-' . substr($order->id, -8)) }}
                            </td>
                            <td class="px-6 py-4 text-slate-700 dark:text-slate-300">
                                {{ $order->customer_name ?: 'Walk-in' }}
                            </td>
                            <td class="px-6 py-4 font-semibold text-slate-900 dark:text-slate-100">
                                {{ number_format($order->total_amount, 2) }}
                            </td>
                            <td class="px-6 py-4">
                                @php
                                    $badgeClass = match($order->status) {
                                        'completed' => 'bg-emerald-100 text-emerald-700',
                                        'pending'   => 'bg-yellow-100 text-yellow-700',
                                        'cancelled' => 'bg-red-100 text-red-700',
                                        'refunded'  => 'bg-purple-100 text-purple-700',
                                        default     => 'bg-slate-100 text-slate-600',
                                    };
                                @endphp
                                <span class="rounded-full px-2 py-0.5 text-xs font-semibold {{ $badgeClass }}">
                                    {{ ucfirst($order->status ?? 'unknown') }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-slate-500 text-xs">
                                {{ $order->order_date ? \Carbon\Carbon::parse($order->order_date)->format('d M Y, H:i') : '—' }}
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="px-6 py-16 text-center text-sm text-slate-500">No orders synced yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-slate-200 px-6 py-4 dark:border-slate-700">{{ $orders->links() }}</div>
    </section>
</div>
@endsection
