@extends('layouts.app')

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

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

    <!-- Header -->
    <div class="flex items-center justify-between pb-4 border-b border-slate-200">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 tracking-tight">Agent Directory</h1>
            <p class="text-xs text-slate-500 mt-1">Manage Harcourts agents for request dropdown selection. Agents do not require login accounts.</p>
        </div>
        <a href="{{ route('admin.agents.create') }}" class="px-4 py-2 bg-harcourts-blue hover:bg-blue-800 text-white font-bold text-xs uppercase tracking-wider rounded shadow">
            + Add Agent
        </a>
    </div>

    <!-- Agents Table -->
    <div class="bg-white rounded-lg border border-slate-200 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs text-slate-700">
                <thead class="bg-slate-50 text-slate-500 uppercase tracking-wider font-semibold border-b border-slate-200">
                    <tr>
                        <th class="px-6 py-3">Agent Name</th>
                        <th class="px-6 py-3">Email Address</th>
                        <th class="px-6 py-3">Phone Number</th>
                        <th class="px-6 py-3">Branch</th>
                        <th class="px-6 py-3">Status</th>
                        <th class="px-6 py-3 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200">
                    @forelse($agents as $agent)
                        <tr class="hover:bg-slate-50 transition-colors">
                            <td class="px-6 py-4 font-bold text-slate-900">{{ $agent->name }}</td>
                            <td class="px-6 py-4 text-slate-600">{{ $agent->email ?? 'N/A' }}</td>
                            <td class="px-6 py-4 text-slate-600">{{ $agent->phone ?? 'N/A' }}</td>
                            <td class="px-6 py-4 font-semibold text-slate-700">{{ $agent->branch ?? 'N/A' }}</td>
                            <td class="px-6 py-4">
                                @if($agent->active)
                                    <span class="px-2 py-0.5 bg-emerald-100 text-emerald-800 rounded text-[10px] font-bold uppercase">Active</span>
                                @else
                                    <span class="px-2 py-0.5 bg-rose-100 text-rose-800 rounded text-[10px] font-bold uppercase">Inactive</span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-right space-x-2 whitespace-nowrap">
                                <a href="{{ route('admin.agents.edit', $agent->id) }}" class="px-2.5 py-1 bg-slate-100 hover:bg-slate-200 text-slate-700 font-semibold rounded text-xs">
                                    Edit
                                </a>

                                <form method="POST" action="{{ route('admin.agents.toggle-status', $agent->id) }}" class="inline-block">
                                    @csrf
                                    <button type="submit" class="px-2.5 py-1 {{ $agent->active ? 'bg-rose-50 hover:bg-rose-100 text-rose-700' : 'bg-emerald-50 hover:bg-emerald-100 text-emerald-700' }} font-semibold rounded text-xs">
                                        {{ $agent->active ? 'Deactivate' : 'Activate' }}
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-6 py-8 text-center text-slate-400">
                                No agents in directory.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="px-6 py-3 border-t border-slate-200">
            {{ $agents->links() }}
        </div>
    </div>

</div>
@endsection
