<?php require_once __DIR__ . '/_sidebar.php'; ?>
<main class="flex-1 py-16 md:py-0 md:pl-64">
    <div class="max-w-4xl mx-auto px-4 py-8 md:py-16">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div>
                <h1 class="text-3xl font-bold text-slate-950">Lead Details</h1>
                <p class="mt-2 text-slate-600">View submitted project request details.</p>
            </div>
            <div class="flex gap-3">
                <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/download-pdf?id=<?= (int)$lead['id'] ?>" class="inline-flex items-center gap-2 rounded-full bg-blue-100 px-5 py-3 text-sm font-medium text-blue-700 hover:bg-blue-200 transition">
                    <span class="material-symbols-outlined text-sm">download</span>Download PDF
                </a>
                <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads" class="rounded-full border border-slate-300 px-5 py-3 text-sm text-slate-950 hover:bg-slate-100">Back to Leads</a>
            </div>
        </div>

        <div class="mt-10 space-y-8">
            <div class="rounded-3xl border border-slate-200 bg-white p-8 shadow-lg">
                <h2 class="text-xl font-bold text-slate-950 mb-4"><?= htmlspecialchars($lead['name']) ?></h2>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-2">Contact Information</h3>
                        <div class="space-y-2 text-sm">
                            <div><span class="text-slate-500">Email:</span> <?= htmlspecialchars($lead['email']) ?></div>
                            <div><span class="text-slate-500">Phone:</span> <?= htmlspecialchars($lead['phone'] ?? 'Not provided') ?></div>
                            <div><span class="text-slate-500">WhatsApp:</span> <?= htmlspecialchars($lead['whatsapp'] ?? 'Not provided') ?></div>
                            <div><span class="text-slate-500">Preferred Contact:</span> <?= htmlspecialchars($lead['preferred_contact_method'] ?? 'Not specified') ?></div>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-2">Project Info</h3>
                        <div class="space-y-2 text-sm">
                            <div><span class="text-slate-500">Project Type:</span> <?= htmlspecialchars($lead['project_type'] ?? 'Not specified') ?></div>
                            <div><span class="text-slate-500">Project Nature:</span> <?= htmlspecialchars($lead['project_nature'] ?? 'Not specified') ?></div>
                            <div><span class="text-slate-500">Location:</span> <?= htmlspecialchars($lead['location'] ?? 'Not specified') ?></div>
                            <div><span class="text-slate-500">Project Stage:</span> <?= htmlspecialchars($lead['project_stage'] ?? 'Not specified') ?></div>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (!empty($lead['services'])): ?>
            <div class="rounded-3xl border border-slate-200 bg-white p-8 shadow-lg">
                <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-2">Services Required</h3>
                <p class="text-sm text-slate-700"><?= nl2br(htmlspecialchars($lead['services'])) ?></p>
            </div>
            <?php endif; ?>

            <?php if (!empty($lead['timeline_start']) || !empty($lead['timeline_end'])): ?>
            <div class="rounded-3xl border border-slate-200 bg-white p-8 shadow-lg">
                <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-2">Timeline</h3>
                <div class="space-y-1 text-sm">
                    <div><span class="text-slate-500">Start:</span> <?= htmlspecialchars($lead['timeline_start'] ?? 'Not specified') ?></div>
                    <div><span class="text-slate-500">End:</span> <?= htmlspecialchars($lead['timeline_end'] ?? 'Not specified') ?></div>
                    <?php if (!empty($lead['site_visit']) && $lead['site_visit'] !== 'No'): ?>
                    <div><span class="text-slate-500">Site Visit:</span> <?= htmlspecialchars($lead['site_visit']) ?></div>
                    <?php if (!empty($lead['preferred_visit_date'])): ?>
                        <div><span class="text-slate-500">Preferred Visit Date:</span> <?= htmlspecialchars($lead['preferred_visit_date']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($lead['preferred_visit_time'])): ?>
                        <div><span class="text-slate-500">Preferred Visit Time:</span> <?= htmlspecialchars($lead['preferred_visit_time']) ?></div>
                    <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>

            <?php if (!empty($lead['uploads'])): ?>
            <div class="rounded-3xl border border-slate-200 bg-white p-8 shadow-lg">
                <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-4">Attachments</h3>
                <div class="flex flex-wrap gap-3">
                    <?php foreach ($lead['uploads'] as $file): ?>
                        <?php $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); ?>
                        <?php if (in_array($ext, ['jpg','jpeg','png','webp','gif'])): ?>
                            <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/download?file=<?= urlencode($file) ?>" target="_blank" class="block w-24 h-24 overflow-hidden rounded-lg border border-slate-200" title="Download image">
                                <img src="<?= htmlspecialchars($baseUrl . '/' . $file) ?>" alt="attachment" class="w-full h-full object-cover" />
                            </a>
                        <?php else: ?>
                            <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/download?file=<?= urlencode($file) ?>" target="_blank" class="inline-flex items-center gap-2 rounded-2xl border border-slate-200 px-4 py-2 text-sm hover:bg-slate-50 transition">
                                <span class="material-symbols-outlined text-slate-600">download</span>
                                <span><?= htmlspecialchars(strtoupper($ext)) ?> File</span>
                            </a>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>

            <div class="rounded-3xl border border-slate-200 bg-white p-8 shadow-lg">
                <h3 class="text-sm font-semibold text-architectural-gray uppercase tracking-wider mb-2">Notes & Additional Details</h3>
                <pre class="text-sm text-slate-700 whitespace-pre-wrap font-sans"><?= htmlspecialchars($lead['notes'] ?? 'No notes provided.') ?></pre>
            </div>
        </div>
    </div>
</main>
</div>