<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
$pageTitle = 'Dashboard';
require __DIR__ . '/includes/admin-header.php';
require __DIR__ . '/includes/ui.php';
$counts = [
'Services' => db()->query('SELECT COUNT(*) c FROM services')->fetch()['c'],
'Downloads' => db()->query('SELECT COUNT(*) c FROM downloads')->fetch()['c'],
'Testimonials' => db()->query('SELECT COUNT(*) c FROM testimonials')->fetch()['c'],
'Industries' => db()->query('SELECT COUNT(*) c FROM industries')->fetch()['c'],
'Gallery Photos' => db()->query('SELECT COUNT(*) c FROM gallery_items')->fetch()['c'],
'Team Members' => db()->query('SELECT COUNT(*) c FROM team_members')->fetch()['c'],
];
$unreadMessages = db()->query('SELECT COUNT(*) c FROM contact_messages WHERE is_read = 0')->fetch()['c'];
$recentMessages = db()->query('SELECT * FROM contact_messages ORDER BY created_at DESC LIMIT 5')->fetchAll();
?>
<div class="grid sm:grid-cols-2 lg:grid-cols-4 gap-5 mb-8">
<a href="messages.php" class="bg-white rounded-2xl border border-gray-100 p-6 hover:shadow-md transition">
<p class="text-3xl font-heading font-bold text-primary"><?= (int) $unreadMessages ?></p>
<p class="text-sm text-gray-500 mt-1">Unread Contact Messages</p>
</a>
<?php foreach ($counts as $label => $count): ?>
<div class="bg-white rounded-2xl border border-gray-100 p-6">
<p class="text-3xl font-heading font-bold text-secondary"><?= (int) $count ?></p>
<p class="text-sm text-gray-500 mt-1"><?= e($label) ?></p>
</div>
<?php endforeach; ?>
</div>
<div class="bg-white rounded-2xl border border-gray-100 p-6">
<div class="flex items-center justify-between mb-4">
<h2 class="font-heading font-semibold text-lg">Recent Contact Messages</h2>
<a href="messages.php" class="text-sm text-primary font-semibold">View all <i class="bi bi-arrow-right"></i></a>
</div>
<?php if (!$recentMessages): ?>
<p class="text-sm text-gray-400">No messages yet.</p>
<?php else: ?>
<div class="divide-y divide-gray-100">
<?php foreach ($recentMessages as $m): ?>
<div class="py-3 flex items-center justify-between gap-4">
<div class="min-w-0">
<p class="font-medium text-sm truncate"><?= e($m['name']) ?> <span class="text-gray-400 font-normal"><<?= e($m['email']) ?>></span></p>
<p class="text-sm text-gray-500 truncate"><?= e($m['message']) ?></p>
</div>
<span class="text-xs text-gray-400 shrink-0"><?= e($m['created_at']) ?></span>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>