@extends('admin.layouts.app')
@section('title', 'Lead — ' . ($lead->name ?: $lead->email))
@section('content')
<div class="p-6">
    <a href="{{ route('admin.leads.index') }}" class="text-sm text-gray-500 hover:text-nsma-red mb-4 inline-block">← Lead-ийн жагсаалт руу</a>

    <div class="grid lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2 space-y-6">
            <div class="bg-white p-6 rounded-lg shadow">
                <h1 class="text-2xl font-bold mb-1">{{ $lead->name ?: 'Нэргүй' }}</h1>
                <p class="text-gray-500 mb-4">{{ $lead->email }} · {{ $lead->phone ?: '—' }}</p>
                <div class="grid grid-cols-2 gap-4 text-sm">
                    <div><b>Бизнес:</b> {{ $lead->business_name ?: '—' }}</div>
                    <div><b>Эх сурвалж:</b> {{ $lead->sourceLabel() }}</div>
                    <div><b>UTM source:</b> {{ $lead->utm_source ?: '—' }}</div>
                    <div><b>Үүсгэсэн:</b> {{ $lead->created_at->format('Y-m-d H:i') }}</div>
                </div>
            </div>

            @if($surveys->count())
                <div class="bg-white p-6 rounded-lg shadow">
                    <h2 class="font-bold mb-4">Survey хариулт ({{ $surveys->count() }})</h2>
                    @foreach($surveys as $survey)
                        <div class="border-l-4 border-nsma-red pl-4 mb-4">
                            <p class="text-sm font-semibold mb-1">Зөвлөмж tier: <span class="text-nsma-red uppercase">{{ $survey->recommended_tier }}</span></p>
                            <div class="text-xs text-gray-500 mb-2">{{ $survey->created_at->format('Y-m-d H:i') }}</div>
                            <details class="text-sm">
                                <summary class="cursor-pointer text-nsma-red">Бүх хариулт үзэх</summary>
                                <pre class="bg-gray-50 p-3 mt-2 rounded text-xs overflow-auto">{{ json_encode($survey->raw_responses, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) }}</pre>
                            </details>
                        </div>
                    @endforeach
                </div>
            @endif

            <div class="bg-white p-6 rounded-lg shadow">
                <h2 class="font-bold mb-3">Тэмдэглэл</h2>
                <form action="{{ route('admin.leads.notes', $lead) }}" method="POST">
                    @csrf
                    <textarea name="notes" rows="5" class="w-full border rounded p-3">{{ $lead->notes }}</textarea>
                    <button class="mt-2 bg-nsma-red text-white px-4 py-2 rounded">Хадгалах</button>
                </form>
            </div>
        </div>

        <div class="space-y-4">
            <div class="bg-white p-6 rounded-lg shadow">
                <h2 class="font-bold mb-3">Статус</h2>
                <form action="{{ route('admin.leads.status', $lead) }}" method="POST" class="space-y-2">
                    @csrf
                    @foreach(['new'=>'Шинэ', 'contacted'=>'Холбогдсон', 'qualified'=>'Сонирхолтой', 'converted'=>'Үйлчлүүлэгч', 'lost'=>'Алдсан'] as $s => $label)
                        <button type="submit" name="status" value="{{ $s }}" class="w-full text-left px-3 py-2 rounded border hover:bg-gray-50 {{ $lead->status === $s ? 'bg-nsma-red text-white' : '' }}">{{ $label }}</button>
                    @endforeach
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
