<?php require_once __DIR__ . '/_sidebar.php'; ?>
<main class="flex-1 py-16 md:py-0 md:pl-64">
    <div class="max-w-7xl 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">Leads</h1>
                <p class="mt-2 text-slate-600">Manage new enquiries and lead statuses for Balorp Ventures.</p>
            </div>
            <a href="<?= htmlspecialchars($baseUrl) ?>/admin" class="rounded-full border border-slate-300 px-5 py-3 text-sm text-slate-950 hover:bg-slate-100">Back to Dashboard</a>
        </div>
        <div class="mt-10 overflow-x-auto rounded-3xl border border-slate-200 bg-white shadow-lg">
            <table class="min-w-full divide-y divide-slate-200 text-sm">
                <thead class="bg-charcoal text-white">
                    <tr>
                        <th class="px-6 py-4 text-left">Lead</th>
                        <th class="px-6 py-4 text-left">Project Type</th>
                        <th class="px-6 py-4 text-left">Services</th>
                        <th class="px-6 py-4 text-left">Attachments</th>
                        <th class="px-6 py-4 text-left">Status</th>
                        <th class="px-6 py-4 text-left">Date</th>
                        <th class="px-6 py-4 text-left">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200">
                    <?php foreach ($leads as $lead): ?>
                    <tr>
                        <td class="px-6 py-4 text-slate-700"><strong><a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/view?id=<?= (int)$lead['id'] ?>" class="text-slate-950 hover:text-blue-600"><?= htmlspecialchars($lead['name']) ?></a></strong><br><?= htmlspecialchars($lead['email']) ?></td>
                        <td class="px-6 py-4 text-slate-700"><?= htmlspecialchars($lead['project_type']) ?></td>
                        <td class="px-6 py-4 text-slate-700 text-xs max-w-xs truncate" title="<?= htmlspecialchars($lead['services'] ?? '') ?>"><?= nl2br(htmlspecialchars($lead['services'] ?? '—')) ?></td>
                        <td class="px-6 py-4 text-slate-700">
                            <?php if (!empty($lead['uploads'])): ?>
                                <div class="flex items-center gap-2">
                                    <?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-12 h-12 overflow-hidden rounded-md" title="Download attachment">
                                                <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-3 py-1 text-xs hover:bg-slate-50 transition">
                                                <?= htmlspecialchars(strtoupper($ext)) ?>
                                            </a>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </div>
                            <?php else: ?>
                                <span class="text-slate-400 text-xs">—</span>
                            <?php endif; ?>
                        </td>
                        <td class="px-6 py-4 text-slate-700"><span class="inline-flex rounded-full bg-amber-100 px-3 py-1 text-xs font-semibold text-amber-800"><?= htmlspecialchars($lead['status']) ?></span></td>
                        <td class="px-6 py-4 text-slate-500"><?= htmlspecialchars($lead['created_at']) ?></td>
                        <td class="px-6 py-4">
                            <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/view?id=<?= (int)$lead['id'] ?>" class="inline-flex items-center gap-1 rounded-full bg-slate-100 px-3 py-1 text-xs font-medium text-slate-700 hover:bg-slate-200 transition" title="View details">
                                <span class="material-symbols-outlined text-sm">visibility</span>View
                            </a>
                            <a href="<?= htmlspecialchars($baseUrl) ?>/admin/leads/download-pdf?id=<?= (int)$lead['id'] ?>" class="inline-flex items-center gap-1 rounded-full bg-blue-100 px-3 py-1 text-xs font-medium text-blue-700 hover:bg-blue-200 transition" title="Download as HTML">
                                <span class="material-symbols-outlined text-sm">download</span>Download
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>
</div>