@extends('layouts.admin')

@section('title', 'Orders')

@section('content')
<div class="max-w-[1100px] mx-auto">
    <div class="flex flex-col md:flex-row md:items-end justify-between mb-8 gap-6">
        <div>
            <p class="text-xs text-gray-500 mb-1">Orders</p>
            <h1 class="text-2xl font-semibold text-gray-900 tracking-tight">Order management</h1>
            <p class="text-gray-500 text-sm mt-1">Track and manage customer orders in real-time.</p>
        </div>
        <div class="flex items-center gap-3">
            <button onclick="window.print()" class="px-4 py-2 bg-white border border-gray-200 rounded-lg text-sm text-gray-600 hover:text-gray-900 hover:bg-gray-50 transition flex items-center gap-2">
                <i class="fas fa-print text-xs"></i>
                Print
            </button>
            <a href="{{ route('admin.orders.export') }}" class="inline-flex items-center gap-2 bg-emerald-600 !bg-brand text-white px-4 py-2 rounded-lg text-sm font-medium shadow-md shadow-brand/20 hover:bg-emerald-700 hover:!bg-brand/90 transition">
                <i class="fas fa-file-export text-xs"></i>
                Export
            </a>
        </div>
    </div>

    <!-- Stats Grid -->
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        <div class="bg-white p-6 rounded-2xl border border-gray-100 shadow-sm flex items-center gap-5">
            <div class="w-14 h-14 bg-emerald-50 rounded-2xl flex items-center justify-center text-brand text-xl">
                <i class="fas fa-shopping-cart"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 mb-0.5">Total orders</p>
                <h3 class="text-2xl font-semibold text-gray-900 leading-none">{{ number_format($totalOrders) }}</h3>
            </div>
        </div>
        <div class="bg-white p-6 rounded-2xl border border-gray-100 shadow-sm flex items-center gap-5">
            <div class="w-14 h-14 bg-amber-50 rounded-2xl flex items-center justify-center text-amber-500 text-xl">
                <i class="fas fa-clock"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 mb-0.5">Pending</p>
                <h3 class="text-2xl font-semibold text-gray-900 leading-none">{{ number_format($pendingOrders) }}</h3>
            </div>
        </div>
        <div class="bg-white p-6 rounded-2xl border border-gray-100 shadow-sm flex items-center gap-5">
            <div class="w-14 h-14 bg-blue-50 rounded-2xl flex items-center justify-center text-blue-500 text-xl">
                <i class="fas fa-box-open"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 mb-0.5">Delivered</p>
                <h3 class="text-2xl font-semibold text-gray-900 leading-none">{{ number_format($deliveredOrders) }}</h3>
            </div>
        </div>
        <div class="bg-white p-6 rounded-2xl border border-gray-100 shadow-sm flex items-center gap-5">
            <div class="w-14 h-14 bg-purple-50 rounded-2xl flex items-center justify-center text-purple-500 text-xl">
                <i class="fas fa-wallet"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 mb-0.5">Total revenue</p>
                <h3 class="text-2xl font-semibold text-gray-900 leading-none">{{ $currencySymbol ?? '$' }}{{ number_format($totalRevenue, 2) }}</h3>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 5000)" 
             class="bg-white border-l-4 border-emerald-500 p-4 mb-8 rounded-xl shadow-sm flex items-center justify-between">
            <div class="flex items-center gap-3">
                <div class="w-8 h-8 bg-emerald-50 rounded-lg flex items-center justify-center text-emerald-500">
                    <i class="fas fa-check text-xs"></i>
                </div>
                <p class="text-sm font-bold text-gray-700">{{ session('success') }}</p>
            </div>
            <button @click="show = false" class="text-gray-400 hover:text-gray-600 transition-colors">
                <i class="fas fa-times text-xs"></i>
            </button>
        </div>
    @endif

    <!-- Filter Bar -->
    <div class="bg-white p-4 mb-6 rounded-2xl border border-gray-100 shadow-sm overflow-x-auto">
        <form action="{{ route('admin.orders.index') }}" method="GET" class="flex flex-col lg:flex-row items-center gap-4 min-w-max lg:min-w-0">
            <div class="relative w-full lg:w-96">
                <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 text-xs"></i>
                <input type="text" name="search" value="{{ request('search') }}" class="w-full pl-10 pr-4 py-2.5 bg-white border border-gray-200 rounded-xl text-sm focus:border-brand focus:ring-2 focus:ring-brand/20 transition placeholder:text-gray-400" placeholder="Search by order ID or customer...">
            </div>

            <div class="flex items-center gap-3">
                <input type="date" name="start_date" value="{{ request('start_date') }}" class="px-4 py-2.5 bg-white border border-gray-200 rounded-xl text-sm focus:border-brand focus:ring-2 focus:ring-brand/20 cursor-pointer" onchange="this.form.submit()">
                <span class="text-gray-300">-</span>
                <input type="date" name="end_date" value="{{ request('end_date') }}" class="px-4 py-2.5 bg-white border border-gray-200 rounded-xl text-sm focus:border-brand focus:ring-2 focus:ring-brand/20 cursor-pointer" onchange="this.form.submit()">
            </div>

            <div class="relative flex-1 lg:flex-none">
                <select name="status" onchange="this.form.submit()" class="w-full lg:w-48 appearance-none px-4 py-2.5 bg-white border border-gray-200 rounded-xl text-sm focus:border-brand focus:ring-2 focus:ring-brand/20 cursor-pointer">
                    <option value="all">All status</option>
                    <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                    <option value="processing" {{ request('status') == 'processing' ? 'selected' : '' }}>Processing</option>
                    <option value="shipped" {{ request('status') == 'shipped' ? 'selected' : '' }}>Shipped</option>
                    <option value="delivered" {{ request('status') == 'delivered' ? 'selected' : '' }}>Delivered</option>
                    <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                </select>
            </div>
            
            <div class="flex items-center gap-2">
                <button type="submit" class="px-4 py-2 bg-brand text-white rounded-lg text-sm hover:bg-brand/90 transition">
                    Filter
                </button>
                <a href="{{ route('admin.orders.index') }}" class="px-4 py-2 bg-gray-50 text-gray-600 rounded-lg text-sm hover:bg-gray-100 transition">
                    Reset
                </a>
            </div>
        </form>
    </div>

    <!-- Orders Table -->
    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse">
                <thead>
                    <tr class="bg-gray-50 border-b border-gray-100">
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Order ID</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Customer</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Date</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Total</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Payment</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Status</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse($orders as $order)
                    <tr class="hover:bg-gray-50 transition-colors group">
                        <td class="px-6 py-4">
                            <span class="text-sm font-semibold text-gray-900">#{{ $order->id }}</span>
                        </td>
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-9 h-9 rounded-xl bg-brand/5 text-brand flex items-center justify-center text-xs font-black">
                                    {{ substr($order->user->name ?? 'G', 0, 1) }}
                                </div>
                                <div>
                                    <p class="text-sm font-medium text-gray-900 leading-none">{{ $order->user->name ?? 'Guest user' }}</p>
                                    <p class="text-xs text-gray-500">{{ $order->user->email ?? 'no-email' }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="flex flex-col">
                                <span class="text-xs font-medium text-gray-900 mb-0.5">{{ $order->created_at->format('M d, Y') }}</span>
                                <span class="text-xs text-gray-500">{{ $order->created_at->format('h:i A') }}</span>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <span class="text-sm font-semibold text-gray-900">{{ $currencySymbol ?? '$' }}{{ number_format($order->total, 2) }}</span>
                        </td>
                        <td class="px-6 py-4">
                            @php
                                $paymentStatus = match($order->payment_status) {
                                    'paid' => ['bg-emerald-50', 'text-emerald-600', 'Paid', 'fa-check-circle'],
                                    'pending' => ['bg-amber-50', 'text-amber-600', 'Pending', 'fa-clock'],
                                    'failed' => ['bg-rose-50', 'text-rose-600', 'Failed', 'fa-times-circle'],
                                    default => ['bg-gray-50', 'text-gray-400', 'Unknown', 'fa-minus-circle'],
                                };
                            @endphp
                            <span class="inline-flex items-center gap-1.5 px-2.5 py-1 {{ $paymentStatus[0] }} {{ $paymentStatus[1] }} rounded-lg text-xs font-medium">
                                <i class="fas {{ $paymentStatus[3] }} text-[11px]"></i>
                                {{ $paymentStatus[2] }}
                            </span>
                        </td>
                        <td class="px-6 py-4">
                            @php
                                $lifecycleStatus = match($order->status) {
                                    'delivered' => ['bg-emerald-500', 'Delivered'],
                                    'shipped' => ['bg-blue-500', 'Shipped'],
                                    'processing' => ['bg-purple-500', 'Processing'],
                                    'pending' => ['bg-amber-500', 'Pending'],
                                    'cancelled' => ['bg-rose-500', 'Cancelled'],
                                    default => ['bg-gray-400', 'Unknown'],
                                };
                            @endphp
                            <div class="flex items-center gap-2">
                                <span class="w-1.5 h-1.5 rounded-full {{ $lifecycleStatus[0] }}"></span>
                                <span class="text-xs font-medium text-gray-900">{{ $lifecycleStatus[1] }}</span>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="flex items-center justify-end gap-2">
                                <a href="{{ route('admin.orders.show', $order->id) }}" class="w-9 h-9 flex items-center justify-center bg-gray-50 rounded-lg text-gray-500 hover:bg-brand/10 hover:text-brand transition">
                                    <i class="fas fa-eye text-xs"></i>
                                </a>
                                <button class="w-9 h-9 flex items-center justify-center bg-gray-50 rounded-lg text-gray-500 hover:bg-blue-50 hover:text-blue-500 transition">
                                    <i class="fas fa-file-invoice text-xs"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-6 py-16 text-center">
                            <div class="max-w-xs mx-auto">
                                <div class="w-16 h-16 bg-gray-50 rounded-xl flex items-center justify-center mx-auto mb-3">
                                    <i class="fas fa-inbox text-2xl text-gray-200"></i>
                                </div>
                                <h3 class="text-base font-semibold text-gray-900 mb-1">No orders found</h3>
                                <p class="text-gray-500 text-sm mb-4">No orders match your current filters.</p>
                                <a href="{{ route('admin.orders.index') }}" class="inline-flex items-center gap-2 bg-brand text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-brand/90 transition">
                                    Reset filters
                                </a>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($orders->hasPages())
        <div class="px-6 py-5 border-t border-gray-100 bg-gray-50">
            {{ $orders->appends(request()->query())->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
