<x-app-layout>
    <x-slot name="title">Leads</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">Leads Management</h1>
            <p class="text-sm text-gray-500 mt-1">Manage and track your sales leads.</p>
        </div>
        
        <div class="flex items-center gap-3" x-data="{ showImport: false }">
            <a href="{{ route('leads.pipeline') }}" class="inline-flex items-center justify-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-lg text-gray-700 bg-white hover:bg-gray-50 focus:outline-none transition-colors">
                <svg class="h-4 w-4 mr-1.5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17V7m0 10a2 2 0 01-2 2H5a2 2 0 01-2-2V7a2 2 0 012-2h2a2 2 0 012 2m0 10a2 2 0 002 2h2a2 2 0 002-2M9 7a2 2 0 012-2h2a2 2 0 012 2m0 10V7m0 10a2 2 0 002 2h2a2 2 0 002-2V7a2 2 0 00-2-2h-2a2 2 0 00-2 2"></path></svg>
                Kanban View
            </a>
            <button @click="showImport = true" class="inline-flex items-center justify-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-lg text-gray-700 bg-white hover:bg-gray-50 focus:outline-none transition-colors">
                <svg class="h-4 w-4 mr-1.5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
                Import
            </button>

            <a href="{{ route('leads.create') }}" 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-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors">
                + Add Lead
            </a>
            
            <!-- Import Modal -->
            <div x-show="showImport" class="fixed inset-0 z-50 overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true" style="display: none;">
                <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
                    <div x-show="showImport" @click="showImport = false" class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
                    <span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">&#8203;</span>
                    <div x-show="showImport" class="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full sm:p-6">
                        <div>
                            <div class="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-blue-100">
                                <svg class="h-6 w-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"></path></svg>
                            </div>
                            <div class="mt-3 text-center sm:mt-5">
                                <h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">Import Leads</h3>
                                <div class="mt-2">
                                    <p class="text-sm text-gray-500">Upload a CSV file containing your leads. Make sure the headers match the required format.</p>
                                    <a href="{{ route('import.sample', 'leads') }}" class="text-primary-600 hover:text-primary-800 text-sm font-medium mt-2 inline-block">Download Sample CSV</a>
                                </div>
                            </div>
                        </div>
                        <form action="{{ route('import', 'leads') }}" method="POST" enctype="multipart/form-data" class="mt-5 sm:mt-6">
                            @csrf
                            <input type="file" name="file" accept=".csv,.xlsx" required class="w-full mb-4 text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-sm file:font-semibold file:bg-primary-50 file:text-primary-700 hover:file:bg-primary-100">
                            <div class="flex gap-3">
                                <button type="submit" class="inline-flex justify-center w-full rounded-md border border-transparent shadow-sm px-4 py-2 bg-primary-600 text-base font-medium text-white hover:bg-primary-700 focus:outline-none sm:text-sm">Import Now</button>
                                <button type="button" @click="showImport = false" class="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none sm:text-sm">Cancel</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <x-dynamic-grid module="leads" :columns="$columns" :data="$leads">
        @forelse($leads as $lead)
        <tr class="hover:bg-gray-50 transition-colors">
            <td class="px-6 py-4 whitespace-nowrap w-10">
                <input type="checkbox" class="rounded border-gray-300 text-primary-600 focus:ring-primary-500">
            </td>
            
            <template x-if="columns.find(c => c.key === 'name')?.visible">
                <td class="px-6 py-4 whitespace-nowrap">
                    <div class="text-sm font-medium text-gray-900">{{ $lead->name }}</div>
                </td>
            </template>

            <template x-if="columns.find(c => c.key === 'phone')?.visible">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $lead->phone ?? 'N/A' }}
                </td>
            </template>
            
            <template x-if="columns.find(c => c.key === 'email')?.visible">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $lead->email ?? 'N/A' }}
                </td>
            </template>

            <template x-if="columns.find(c => c.key === 'status')?.visible">
                <td class="px-6 py-4 whitespace-nowrap">
                    @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>
                </td>
            </template>

            <template x-if="columns.find(c => c.key === 'priority')?.visible">
                <td class="px-6 py-4 whitespace-nowrap">
                    <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>
                </td>
            </template>
            
            <template x-if="columns.find(c => c.key === 'agent')?.visible">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $lead->agent->name ?? 'Unassigned' }}
                </td>
            </template>
            
            <template x-if="columns.find(c => c.key === 'source')?.visible">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $lead->source->name ?? 'Direct' }}
                </td>
            </template>

            <template x-if="columns.find(c => c.key === 'created_at')?.visible">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $lead->created_at->format('d M, Y') }}
                </td>
            </template>

            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                <a href="{{ route('leads.show', $lead->id) }}" class="text-primary-600 hover:text-primary-900 mr-3">View</a>
                <a href="{{ route('leads.edit', $lead->id) }}" class="text-blue-600 hover:text-blue-900">Edit</a>
            </td>
        </tr>
        @empty
        <tr>
            <td colspan="100%" class="px-6 py-8 text-center text-gray-500">
                No leads found matching your criteria.
            </td>
        </tr>
        @endforelse
    </x-dynamic-grid>
</x-app-layout>
