@extends('admin.layouts.app')

@section('title', 'All Orders')
@section('header_title', 'Order Management')

@section('content')
<div class="space-y-8">

    <!-- Header Actions & Info -->
    <div class="bg-white rounded-3xl p-6 border border-amber-900/10 shadow-xs flex flex-col lg:flex-row items-stretch lg:items-center justify-between gap-4">
        <div>
            <h2 class="font-serif text-xl font-bold text-slate-900">Order Management</h2>
            <p class="text-xs text-slate-500">Track, filter, update, and process customer orders placed from the Dwaraka store.</p>
        </div>
        <div class="flex items-center space-x-2">
            <span class="px-3 py-1.5 rounded-xl bg-amber-50 border border-amber-200 text-gold-800 text-xs font-bold">
                <i class="fa-solid fa-clock-rotate-left mr-1"></i> Live Real-Time DB Orders
            </span>
        </div>
    </div>

    <!-- SUMMARY METRIC CARDS GRID -->
    <div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-4">
        
        <!-- Total Orders -->
        <div class="bg-white rounded-2xl p-4 border border-amber-900/10 shadow-xs">
            <div class="text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1">Total Orders</div>
            <div class="text-xl font-serif font-bold text-slate-900">{{ number_format($totalOrders) }}</div>
        </div>

        <!-- Pending -->
        <div class="bg-white rounded-2xl p-4 border border-amber-200/80 shadow-xs">
            <div class="text-[10px] font-bold text-amber-600 uppercase tracking-wider mb-1">Pending</div>
            <div class="text-xl font-serif font-bold text-amber-700">{{ number_format($pendingOrders) }}</div>
        </div>

        <!-- Processing -->
        <div class="bg-white rounded-2xl p-4 border border-blue-200/80 shadow-xs">
            <div class="text-[10px] font-bold text-blue-600 uppercase tracking-wider mb-1">Processing</div>
            <div class="text-xl font-serif font-bold text-blue-700">{{ number_format($processingOrders) }}</div>
        </div>

        <!-- Shipped -->
        <div class="bg-white rounded-2xl p-4 border border-purple-200/80 shadow-xs">
            <div class="text-[10px] font-bold text-purple-600 uppercase tracking-wider mb-1">Shipped</div>
            <div class="text-xl font-serif font-bold text-purple-700">{{ number_format($shippedOrders) }}</div>
        </div>

        <!-- Delivered -->
        <div class="bg-white rounded-2xl p-4 border border-emerald-200/80 shadow-xs">
            <div class="text-[10px] font-bold text-emerald-600 uppercase tracking-wider mb-1">Delivered</div>
            <div class="text-xl font-serif font-bold text-emerald-700">{{ number_format($deliveredOrders) }}</div>
        </div>

        <!-- Cancelled -->
        <div class="bg-white rounded-2xl p-4 border border-red-200/80 shadow-xs">
            <div class="text-[10px] font-bold text-red-500 uppercase tracking-wider mb-1">Cancelled</div>
            <div class="text-xl font-serif font-bold text-red-600">{{ number_format($cancelledOrders) }}</div>
        </div>

    </div>

    <!-- FILTER & SEARCH BAR FORM -->
    <div class="bg-white rounded-3xl p-6 border border-amber-900/10 shadow-xs">
        <form method="GET" action="{{ route('admin.orders.index') }}" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-12 gap-4">
            
            <!-- Search Keyword -->
            <div class="lg:col-span-5 relative">
                <i class="fa-solid fa-magnifying-glass absolute left-3.5 top-1/2 -translate-y-1/2 text-xs text-slate-400"></i>
                <input 
                    type="text" 
                    name="search" 
                    value="{{ request('search') }}" 
                    placeholder="Search by Order #, Customer Name, Email or Phone..." 
                    class="w-full pl-9 pr-4 py-2.5 text-xs bg-slate-50 border border-slate-200 rounded-xl focus:outline-none focus:border-gold-500 focus:bg-white transition-all"
                >
            </div>

            <!-- Order Status Filter -->
            <div class="lg:col-span-3">
                <select name="order_status" class="w-full px-3 py-2.5 text-xs bg-slate-50 border border-slate-200 rounded-xl focus:outline-none focus:border-gold-500 transition-all">
                    <option value="">All Order Statuses</option>
                    <option value="pending" {{ request('order_status') == 'pending' ? 'selected' : '' }}>Pending</option>
                    <option value="processing" {{ request('order_status') == 'processing' ? 'selected' : '' }}>Processing</option>
                    <option value="shipped" {{ request('order_status') == 'shipped' ? 'selected' : '' }}>Shipped</option>
                    <option value="delivered" {{ request('order_status') == 'delivered' ? 'selected' : '' }}>Delivered</option>
                    <option value="cancelled" {{ request('order_status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                </select>
            </div>

            <!-- Payment Status Filter -->
            <div class="lg:col-span-2">
                <select name="payment_status" class="w-full px-3 py-2.5 text-xs bg-slate-50 border border-slate-200 rounded-xl focus:outline-none focus:border-gold-500 transition-all">
                    <option value="">All Payment</option>
                    <option value="pending" {{ request('payment_status') == 'pending' ? 'selected' : '' }}>Pending</option>
                    <option value="paid" {{ request('payment_status') == 'paid' ? 'selected' : '' }}>Paid</option>
                    <option value="failed" {{ request('payment_status') == 'failed' ? 'selected' : '' }}>Failed</option>
                    <option value="refunded" {{ request('payment_status') == 'refunded' ? 'selected' : '' }}>Refunded</option>
                </select>
            </div>

            <!-- Submit & Reset Buttons -->
            <div class="lg:col-span-2 flex items-center space-x-2">
                <button type="submit" class="flex-1 py-2.5 bg-obsidian-900 text-white rounded-xl text-xs font-bold hover:bg-obsidian-800 transition-colors">
                    FILTER
                </button>
                <a href="{{ route('admin.orders.index') }}" class="p-2.5 bg-slate-100 text-slate-600 rounded-xl text-xs font-bold hover:bg-slate-200 transition-colors text-center" title="Reset Filters">
                    <i class="fa-solid fa-rotate-left"></i>
                </a>
            </div>

        </form>
    </div>

    <!-- ORDERS TABLE -->
    <div class="bg-white rounded-3xl border border-amber-900/10 shadow-xs overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse">
                <thead>
                    <tr class="bg-slate-50/80 text-[11px] font-bold text-slate-500 uppercase tracking-wider border-b border-slate-100">
                        <th class="py-4 px-4">Order ID</th>
                        <th class="py-4 px-4">Customer</th>
                        <th class="py-4 px-4">Contact</th>
                        <th class="py-4 px-4">Items</th>
                        <th class="py-4 px-4">Total Amount</th>
                        <th class="py-4 px-4">Payment</th>
                        <th class="py-4 px-4">Order Status</th>
                        <th class="py-4 px-4">Date</th>
                        <th class="py-4 px-4 text-right">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 text-xs">
                    @forelse($orders as $order)
                        <tr class="hover:bg-amber-50/30 transition-colors">
                            
                            <!-- Order Number -->
                            <td class="py-3.5 px-4 font-mono font-bold text-slate-900">
                                <a href="{{ route('admin.orders.show', $order->id) }}" class="text-gold-700 hover:underline">
                                    {{ $order->order_number }}
                                </a>
                            </td>

                            <!-- Customer Name -->
                            <td class="py-3.5 px-4">
                                <div class="font-bold text-slate-900">{{ $order->customer_name }}</div>
                                <div class="text-[10px] text-slate-400 truncate max-w-xs">{{ $order->customer_email ?? 'No Email Provided' }}</div>
                            </td>

                            <!-- Phone -->
                            <td class="py-3.5 px-4 font-mono text-slate-600">
                                {{ $order->customer_phone }}
                            </td>

                            <!-- Item Count & Summary -->
                            <td class="py-3.5 px-4">
                                <span class="font-semibold text-slate-800">{{ $order->items->sum('quantity') }} items</span>
                                <div class="text-[10px] text-slate-400 truncate max-w-xs">
                                    {{ $order->items->pluck('product_name')->implode(', ') }}
                                </div>
                            </td>

                            <!-- Total Amount -->
                            <td class="py-3.5 px-4 font-serif font-bold text-slate-900 text-sm">
                                ₹{{ number_format($order->total_amount, 2) }}
                                <div class="text-[10px] text-slate-400 uppercase font-sans font-medium">{{ strtoupper($order->payment_method) }}</div>
                            </td>

                            <!-- Payment Status -->
                            <td class="py-3.5 px-4">
                                <span class="px-2.5 py-1 rounded-full text-[10px] font-bold border {{ $order->payment_status_badge }}">
                                    {{ strtoupper($order->payment_status) }}
                                </span>
                            </td>

                            <!-- Order Status -->
                            <td class="py-3.5 px-4">
                                <span class="px-2.5 py-1 rounded-full text-[10px] font-bold border {{ $order->order_status_badge }}">
                                    {{ strtoupper($order->order_status) }}
                                </span>
                            </td>

                            <!-- Date -->
                            <td class="py-3.5 px-4 text-slate-500 text-[11px]">
                                {{ $order->created_at ? $order->created_at->format('M d, Y g:i A') : 'N/A' }}
                            </td>

                            <!-- Action -->
                            <td class="py-3.5 px-4 text-right">
                                <a href="{{ route('admin.orders.show', $order->id) }}" class="px-3 py-1.5 rounded-lg bg-obsidian-900 text-white hover:bg-gold-600 transition-colors font-bold text-[11px] inline-flex items-center space-x-1 shadow-xs">
                                    <i class="fa-solid fa-eye text-[10px]"></i>
                                    <span>VIEW</span>
                                </a>
                            </td>

                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="py-12 text-center text-slate-400">
                                <i class="fa-solid fa-box-open text-4xl mb-3 text-slate-300 block"></i>
                                <p class="font-medium text-sm">No customer orders found matching your search.</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        <div class="p-4 border-t border-slate-100 bg-slate-50/50 flex items-center justify-between">
            <p class="text-xs text-slate-500">Showing {{ $orders->firstItem() ?? 0 }} to {{ $orders->lastItem() ?? 0 }} of {{ $orders->total() }} orders</p>
            <div>
                {{ $orders->links() }}
            </div>
        </div>
    </div>

</div>
@endsection
