@extends('admin.layouts.app')
@section('title', 'Lead-ууд')
@section('content')
<div class="p-6">
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-2xl font-bold">Lead-ууд</h1>
            <p class="text-sm text-gray-500">Гарын авлага татан, survey бөглөсөн хүмүүс</p>
        </div>
        <a href="{{ route('admin.leads.export') }}" class="bg-green-600 text-white px-4 py-2 rounded-lg font-semibold">⬇ CSV татах</a>
    </div>

    <div class="grid grid-cols-5 gap-3 mb-6">
        @foreach([
            ['new', 'Шинэ', 'blue'],
            ['contacted', 'Холбогдсон', 'yellow'],
            ['qualified', 'Сонирхолтой', 'cyan'],
            ['converted', 'Үйлчлүүлэгч', 'green'],
            ['lost', 'Алдсан', 'gray'],
        ] as [$s, $label, $color])
            <a href="?status={{ $s }}" class="bg-white p-4 rounded-lg border hover:shadow">
                <div class="text-xs text-gray-500">{{ $label }}</div>
                <div class="text-2xl font-bold text-{{ $color }}-600">{{ $counts[$s] }}</div>
            </a>
        @endforeach
    </div>

    <form method="GET" class="bg-white p-4 rounded-lg mb-4 flex gap-2">
        <input type="text" name="search" value="{{ request('search') }}" placeholder="Хайх..." class="border px-3 py-2 rounded flex-grow">
        <select name="source" class="border px-3 py-2 rounded">
            <option value="">Бүх эх сурвалж</option>
            <option value="guide_download" @selected(request('source')=='guide_download')>Гарын авлага</option>
            <option value="survey"         @selected(request('source')=='survey')>Судалгаа</option>
            <option value="template_order" @selected(request('source')=='template_order')>Template захиалга</option>
            <option value="contact"        @selected(request('source')=='contact')>Холбоо</option>
        </select>
        <button class="bg-nsma-red text-white px-4 py-2 rounded">Шүүх</button>
    </form>

    <div class="bg-white rounded-lg overflow-hidden">
        <table class="w-full">
            <thead class="bg-gray-50 border-b">
                <tr>
                    <th class="text-left p-3 text-xs font-semibold uppercase">Нэр / Email</th>
                    <th class="text-left p-3 text-xs font-semibold uppercase">Бизнес</th>
                    <th class="text-left p-3 text-xs font-semibold uppercase">Эх сурвалж</th>
                    <th class="text-left p-3 text-xs font-semibold uppercase">Статус</th>
                    <th class="text-left p-3 text-xs font-semibold uppercase">Огноо</th>
                </tr>
            </thead>
            <tbody class="divide-y">
                @forelse($leads as $lead)
                    <tr class="hover:bg-gray-50">
                        <td class="p-3">
                            <a href="{{ route('admin.leads.show', $lead) }}" class="font-semibold hover:text-nsma-red">{{ $lead->name ?: 'Нэргүй' }}</a>
                            <div class="text-xs text-gray-500">{{ $lead->email }}</div>
                        </td>
                        <td class="p-3 text-sm">{{ $lead->business_name ?: '—' }}</td>
                        <td class="p-3 text-sm">{{ $lead->sourceLabel() }}</td>
                        <td class="p-3"><span class="px-2 py-1 rounded text-xs font-semibold bg-{{ $lead->statusColor() }}-100 text-{{ $lead->statusColor() }}-700">{{ $lead->statusLabel() }}</span></td>
                        <td class="p-3 text-xs text-gray-500">{{ $lead->created_at->format('Y-m-d H:i') }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="p-8 text-center text-gray-500">Lead алга байна</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="mt-4">{{ $leads->links() }}</div>
</div>
@endsection
