@extends('admin.layouts.app')

@section('title', 'Customers')
@section('header_title', 'Customer Directory')

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

    <!-- Header Actions & Search Bar -->
    <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">Customer Directory</h2>
            <p class="text-xs text-slate-500">View real customers who have placed orders with Dwaraka Collection.</p>
        </div>
        <div class="flex items-center space-x-2">
            <span class="px-3 py-1.5 rounded-xl bg-purple-50 border border-purple-200 text-purple-800 text-xs font-bold">
                {{ number_format($totalCustomersCount) }} Total Customers
            </span>
        </div>
    </div>

    <!-- SEARCH BAR FORM -->
    <div class="bg-white rounded-3xl p-6 border border-amber-900/10 shadow-xs">
        <form method="GET" action="{{ route('admin.customers.index') }}" class="grid grid-cols-1 lg:grid-cols-12 gap-4">
            
            <div class="lg:col-span-10 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 customer by 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>

            <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">
                    SEARCH
                </button>
                <a href="{{ route('admin.customers.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">
                    <i class="fa-solid fa-rotate-left"></i>
                </a>
            </div>

        </form>
    </div>

    <!-- CUSTOMERS 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">Customer Name</th>
                        <th class="py-4 px-4">Contact Info</th>
                        <th class="py-4 px-4 text-center">Total Orders</th>
                        <th class="py-4 px-4 text-right">Total Spent</th>
                        <th class="py-4 px-4 text-right">Last Order Date</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 text-xs">
                    @forelse($customers as $customer)
                        <tr class="hover:bg-amber-50/30 transition-colors">
                            
                            <!-- Customer Name Avatar -->
                            <td class="py-3.5 px-4">
                                <div class="flex items-center space-x-3">
                                    <div class="w-9 h-9 rounded-full bg-gold-500/20 text-gold-700 font-bold text-sm flex items-center justify-center border border-gold-400/30">
                                        {{ strtoupper(substr($customer->customer_name, 0, 1)) }}
                                    </div>
                                    <div>
                                        <p class="font-bold text-slate-900">{{ $customer->customer_name }}</p>
                                        <span class="text-[10px] text-emerald-600 font-semibold bg-emerald-50 px-1.5 py-0.5 rounded">Verified Buyer</span>
                                    </div>
                                </div>
                            </td>

                            <!-- Contact Info -->
                            <td class="py-3.5 px-4">
                                <div class="font-mono text-slate-800 font-semibold">
                                    <i class="fa-solid fa-phone text-[10px] text-slate-400 mr-1"></i>{{ $customer->customer_phone }}
                                </div>
                                <div class="text-[10px] text-slate-400">{{ $customer->customer_email ?? 'No email on record' }}</div>
                            </td>

                            <!-- Total Orders -->
                            <td class="py-3.5 px-4 text-center">
                                <span class="px-2.5 py-1 rounded-full bg-slate-100 font-bold text-slate-800">
                                    {{ $customer->total_orders }} orders
                                </span>
                            </td>

                            <!-- Total Spent -->
                            <td class="py-3.5 px-4 text-right font-serif font-bold text-slate-900 text-sm">
                                ₹{{ number_format($customer->total_spent, 2) }}
                            </td>

                            <!-- Last Order Date -->
                            <td class="py-3.5 px-4 text-right text-slate-500 text-[11px]">
                                {{ $customer->last_order_date ? \Carbon\Carbon::parse($customer->last_order_date)->format('M d, Y') : 'N/A' }}
                            </td>

                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="py-12 text-center text-slate-400">
                                <i class="fa-solid fa-users-slash text-4xl mb-3 text-slate-300 block"></i>
                                <p class="font-medium text-sm">No customers recorded in database yet.</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 {{ $customers->firstItem() ?? 0 }} to {{ $customers->lastItem() ?? 0 }} of {{ $customers->total() }} customer records</p>
            <div>
                {{ $customers->links() }}
            </div>
        </div>
    </div>

</div>
@endsection
