@extends('layouts.app')

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

@section('content')
<div class="space-y-6">
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
        <div>
            <h1 class="text-xl font-bold text-slate-900 dark:text-white">Customers Directory</h1>
            <p class="text-xs text-slate-500">Master database of individual customers, sponsors, investors, and dependents.</p>
        </div>
        <div class="flex items-center space-x-2 rtl:space-x-reverse">
            <a href="{{ route('app.customers.families.index') }}" class="px-4 py-2 bg-slate-200 dark:bg-slate-700 text-slate-700 dark:text-slate-200 font-bold text-xs rounded-xl">Families</a>
            <a href="{{ route('app.customers.create') }}" class="px-4 py-2 bg-sky-600 hover:bg-sky-700 text-white font-bold text-xs rounded-xl shadow-sm">+ New Customer</a>
        </div>
    </div>

    <form method="GET" class="grid gap-3 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm md:grid-cols-[minmax(240px,1fr)_180px_auto_auto]">
        <input type="hidden" name="type" value="{{ $type }}">
        <label class="text-xs font-semibold text-slate-700">Search<input type="search" name="q" value="{{ request('q') }}" class="erp-input mt-1" placeholder="Name, customer code, mobile or identity number"></label>
        <label class="text-xs font-semibold text-slate-700">Created<select name="created" class="erp-input mt-1"><option value="">Any date</option><option value="today" @selected(request('created')==='today')>Today</option></select></label>
        <button class="self-end rounded-xl bg-slate-900 px-4 py-2.5 text-xs font-bold text-white">Apply filters</button>
        <button name="export" value="csv" class="self-end rounded-xl border border-emerald-300 bg-emerald-50 px-4 py-2.5 text-xs font-bold text-emerald-800">Export CSV</button>
    </form>

    <!-- Filter Tabs (Requirements 72-73) -->
    <div class="flex items-center space-x-2 rtl:space-x-reverse text-xs font-bold border-b border-slate-200 dark:border-slate-700 pb-2">
        <a href="{{ route('app.customers.index', ['type' => 'all']) }}" class="px-3 py-1.5 rounded-xl {{ $type === 'all' ? 'bg-sky-600 text-white' : 'text-slate-500 hover:text-slate-700' }}">All Customers</a>
        <a href="{{ route('app.customers.index', ['type' => 'individuals']) }}" class="px-3 py-1.5 rounded-xl {{ $type === 'individuals' ? 'bg-sky-600 text-white' : 'text-slate-500 hover:text-slate-700' }}">Individuals</a>
        <a href="{{ route('app.customers.index', ['type' => 'vip']) }}" class="px-3 py-1.5 rounded-xl {{ $type === 'vip' ? 'bg-sky-600 text-white' : 'text-slate-500 hover:text-slate-700' }}">VIP Customers</a>
    </div>

    <!-- Customers Directory Table (Requirements 72-74) -->
    <div class="bg-white dark:bg-slate-800 rounded-3xl border border-slate-200 dark:border-slate-700/60 shadow-sm overflow-hidden text-xs">
        <div class="overflow-x-auto">
            <table class="w-full text-left rtl:text-right">
                <thead class="bg-slate-50 dark:bg-slate-900/60 border-b border-slate-200 dark:border-slate-700 text-slate-500 uppercase tracking-wider font-bold">
                    <tr>
                        <th class="px-6 py-4">Customer Code</th>
                        <th class="px-6 py-4">Full Name</th>
                        <th class="px-6 py-4">Mobile</th>
                        <th class="px-6 py-4">Nationality</th>
                        <th class="px-6 py-4">Type</th>
                        <th class="px-6 py-4">Status</th>
                        <th class="px-6 py-4 text-right rtl:text-left">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 dark:divide-slate-700/60 font-medium">
                    @forelse($customers as $customer)
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-700/30">
                            <td class="px-6 py-4 font-mono font-bold text-sky-600 dark:text-sky-400">{{ $customer->customer_code }}</td>
                            <td class="px-6 py-4">
                                <div class="font-bold text-slate-900 dark:text-white">{{ $customer->full_name_en }}</div>
                                @if($customer->is_vip)
                                    <span class="px-2 py-0.5 rounded bg-amber-100 dark:bg-amber-950 text-amber-700 font-bold text-[9px]">VIP</span>
                                @endif
                            </td>
                            <td class="px-6 py-4 font-mono text-slate-600 dark:text-slate-300 font-semibold">
                                {{ $customer->contact ? $customer->contact->primary_mobile : 'N/A' }}
                            </td>
                            <td class="px-6 py-4 text-slate-600 dark:text-slate-400 font-semibold">{{ $customer->nationality }}</td>
                            <td class="px-6 py-4 uppercase text-[10px] font-bold text-slate-500">{{ $customer->customer_type }}</td>
                            <td class="px-6 py-4">
                                <span class="px-2.5 py-0.5 rounded-full bg-emerald-100 text-emerald-700 font-bold text-[10px]">Active</span>
                            </td>
                            <td class="px-6 py-4 text-right rtl:text-left">
                                <a href="{{ route('app.customers.show', $customer->id) }}" class="text-sky-600 font-bold hover:underline">View 360</a>
                            </td>
                        </tr>
                    @empty<tr><td colspan="7" class="px-6 py-14 text-center"><strong class="text-slate-700">No customers match these filters</strong><span class="mt-1 block text-xs text-slate-400">Clear the filters or add a new customer.</span></td></tr>@endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-slate-200 dark:border-slate-700">
            {{ $customers->links() }}
        </div>
    </div>
</div>
@endsection
