@extends('layouts.admin')

@section('title', 'Customer 360 CRM – Admin Console')
@section('page_title', 'Customer 360 CRM')

@section('content')
<div class="space-y-6">
    
    <!-- Header & Search -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-4 flex flex-col sm:flex-row items-center justify-between gap-4">
        <div>
            <h2 class="text-lg font-bold text-white font-heading">Registered Client Accounts</h2>
            <p class="text-xs text-slate-400">All registered buyers with auction bidding authorizations and vehicle orders.</p>
        </div>
        
        <form method="GET" action="{{ route('admin.customers.index') }}" class="w-full sm:w-auto flex items-center gap-2">
            <div class="relative">
                <i class="fa-solid fa-magnifying-glass absolute left-3 top-2.5 text-slate-400 text-xs"></i>
                <input type="text" name="search" value="{{ request('search') }}" placeholder="Search name, phone, email..." class="bg-slate-950 border border-white/10 rounded-xl pl-8 pr-3 py-1.5 text-white text-xs focus:border-red-500 focus:outline-none">
            </div>
            <button type="submit" class="px-3 py-1.5 rounded-xl bg-white/10 hover:bg-white/20 text-white font-bold text-xs transition">
                Search
            </button>
        </form>
    </div>

    <!-- Customers Table -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Customer Name & Code</th>
                        <th class="p-4">Contact Info</th>
                        <th class="p-4">Location</th>
                        <th class="p-4">Total Bids Placed</th>
                        <th class="p-4">Confirmed Orders</th>
                        <th class="p-4">Status</th>
                        <th class="p-4 text-right">360 View</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @forelse($customers as $customer)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-8 h-8 rounded-lg bg-red-600/20 text-red-400 font-bold flex items-center justify-center text-xs">
                                        {{ strtoupper(substr($customer->full_name ?? 'C', 0, 1)) }}
                                    </div>
                                    <div>
                                        <a href="{{ route('admin.customers.show', $customer->id) }}" class="font-bold text-white hover:text-red-400">
                                            {{ $customer->full_name }}
                                        </a>
                                        <span class="text-[10px] text-slate-500 block font-mono">{{ $customer->customer_code }}</span>
                                    </div>
                                </div>
                            </td>
                            <td class="p-4">
                                <p class="font-semibold text-white">{{ $customer->phone }}</p>
                                <span class="text-[10px] text-slate-400">{{ $customer->email }}</span>
                            </td>
                            <td class="p-4 text-slate-300">
                                {{ $customer->city ?? 'Dhaka' }}
                            </td>
                            <td class="p-4 font-bold text-amber-400">
                                {{ $customer->user->bids->count() ?? 0 }} bids
                            </td>
                            <td class="p-4 font-bold text-emerald-400">
                                {{ $customer->user->orders->count() ?? 0 }} orders
                            </td>
                            <td class="p-4">
                                <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase {{ $customer->status === 'verified' ? 'bg-emerald-500/20 text-emerald-400' : 'bg-amber-500/20 text-amber-400' }}">
                                    {{ $customer->status }}
                                </span>
                            </td>
                            <td class="p-4 text-right">
                                <a href="{{ route('admin.customers.show', $customer->id) }}" class="px-3 py-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-white font-bold transition">
                                    Profile & CRM
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="p-8 text-center text-slate-400">
                                No customer records found.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-white/10">
            {{ $customers->links() }}
        </div>
    </div>

</div>
@endsection
