<x-app-layout>
    <x-slot name="title">Lead Details</x-slot>
    <div class="mb-6 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Lead: {{ $lead->name }}</h1>
            <p class="text-sm text-gray-500 mt-1">Lead ID: #{{ str_pad($lead->id, 5, '0', STR_PAD_LEFT) }}</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{{ route('leads.index') }}" class="text-gray-500 hover:text-gray-700 font-medium text-sm transition-colors">
                &larr; Back to Leads
            </a>
            <a href="{{ route('leads.edit', $lead->id) }}" class="inline-flex items-center justify-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-blue-600 hover:bg-blue-700 transition-colors">
                Edit Lead
            </a>
            @if($lead->status !== 'Won')
            <form action="{{ route('leads.convert', $lead->id) }}" method="POST" class="inline">
                @csrf
                <button type="submit" class="inline-flex items-center justify-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-green-600 hover:bg-green-700 transition-colors">
                    Convert to Customer
                </button>
            </form>
            @endif
            <form action="{{ route('leads.destroy', $lead->id) }}" method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete this lead?');">
                @csrf
                @method('DELETE')
                <button type="submit" class="inline-flex items-center justify-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-red-600 hover:bg-red-700 transition-colors">
                    Delete
                </button>
            </form>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <!-- Lead Information -->
        <div class="col-span-1 md:col-span-2 space-y-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h3 class="text-lg font-medium text-gray-900 border-b border-gray-100 pb-3 mb-4">Personal & Contact Info</h3>
                <dl class="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-6">
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Full Name</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->name }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">CNIC</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->cnic ?? 'N/A' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Phone</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->phone ?? 'N/A' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">WhatsApp</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->whatsapp ?? 'N/A' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Email</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->email ?? 'N/A' }}</dd>
                    </div>
                </dl>
            </div>

            @if($lead->notes || ($lead->custom_data && count($lead->custom_data) > 0))
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h3 class="text-lg font-medium text-gray-900 border-b border-gray-100 pb-3 mb-4">Additional Details</h3>
                
                @if($lead->notes)
                <div class="mb-6">
                    <h4 class="text-sm font-medium text-gray-500 mb-2">Notes</h4>
                    <div class="text-sm text-gray-900 whitespace-pre-wrap">{{ $lead->notes }}</div>
                </div>
                @endif
                
                @if($lead->custom_data && count($lead->custom_data) > 0)
                <h4 class="text-sm font-medium text-gray-500 mb-2">Custom Fields</h4>
                <dl class="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-6">
                    @foreach($lead->custom_data as $key => $value)
                    <div>
                        <dt class="text-sm font-medium text-gray-500 capitalize">{{ str_replace('_', ' ', $key) }}</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $value ?? 'N/A' }}</dd>
                    </div>
                    @endforeach
                </dl>
                @endif
            </div>
            @endif
        </div>

        <!-- Sidebar Info -->
        <div class="col-span-1 space-y-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h3 class="text-lg font-medium text-gray-900 border-b border-gray-100 pb-3 mb-4">Status & Assignment</h3>
                <dl class="space-y-4">
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Status</dt>
                        <dd class="mt-1">
                            @php
                                $statusColors = [
                                    'New' => 'bg-blue-100 text-blue-800',
                                    'Contacted' => 'bg-yellow-100 text-yellow-800',
                                    'Interested' => 'bg-green-100 text-green-800',
                                    'Follow-up' => 'bg-purple-100 text-purple-800',
                                ];
                                $colorClass = $statusColors[$lead->status] ?? 'bg-gray-100 text-gray-800';
                            @endphp
                            <span class="px-2.5 py-1 inline-flex text-xs leading-5 font-semibold rounded-full {{ $colorClass }}">
                                {{ $lead->status }}
                            </span>
                        </dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Priority</dt>
                        <dd class="mt-1">
                            <span class="px-2.5 py-1 inline-flex text-xs leading-5 font-semibold rounded-full {{ $lead->priority == 'High' ? 'bg-red-100 text-red-800' : 'bg-gray-100 text-gray-800' }}">
                                {{ $lead->priority ?? 'Medium' }}
                            </span>
                        </dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Assigned Agent</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->agent->name ?? 'Unassigned' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Lead Source</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->source->name ?? 'Direct' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Preferred Location</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->interested_property ?? 'Any' }}</dd>
                    </div>
                    <div>
                        <dt class="text-sm font-medium text-gray-500">Created At</dt>
                        <dd class="mt-1 text-sm text-gray-900">{{ $lead->created_at->format('M d, Y h:i A') }}</dd>
                    </div>
                </dl>
            </div>
        </div>
    </div>
</x-app-layout>
