<?php
session_start();
require_once '../config/db.php';
require_once 'auth.php';

$pageTitle = 'Dashboard';
$activePage = 'dashboard';

// Count stats
$stats = [];
$tables = ['sliders','news','gallery_photos','gallery_videos','brands','brand_products','our_projects','price_list','catalogs','contact_messages'];
$labels = ['Slider','Berita','Foto','Video','Brand','Produk Brand','Project','Harga','Katalog','Pesan'];

foreach ($tables as $i => $t) {
    $row = $pdo->query("SELECT COUNT(*) as c FROM `$t`")->fetch();
    $stats[$labels[$i]] = $row['c'];
}

$unread = $pdo->query("SELECT COUNT(*) as c FROM contact_messages WHERE is_read=0")->fetch()['c'];
$recentMessages = $pdo->query("SELECT * FROM contact_messages ORDER BY created_at DESC LIMIT 5")->fetchAll();

include 'partials/header.php';
?>

<div class="row g-3 mb-4">
    <?php
    $icons = ['bi-images','bi-newspaper','bi-image','bi-play-circle','bi-award','bi-grid-3x3-gap','bi-building','bi-tags','bi-file-pdf','bi-envelope'];
    $colors = ['#3b82f6','#10b981','#f59e0b','#ef4444','#8b5cf6','#d946ef','#06b6d4','#ec4899','#14b8a6','#f97316'];
    $i = 0;
    foreach ($stats as $label => $count):
    ?>
    <div class="col-6 col-md-4 col-lg-3">
        <div class="card border-0" style="border-left: 3px solid <?= $colors[$i] ?> !important;">
            <div class="card-body py-3">
                <div class="d-flex align-items-center gap-3">
                    <div style="width:42px;height:42px;border-radius:8px;background:<?= $colors[$i] ?>22;display:flex;align-items:center;justify-content:center;">
                        <i class="bi <?= $icons[$i] ?>" style="color:<?= $colors[$i] ?>;font-size:1.2rem;"></i>
                    </div>
                    <div>
                        <div class="fw-bold fs-4 lh-1"><?= $count ?></div>
                        <div class="text-muted small"><?= $label ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php $i++; endforeach; ?>
</div>

<div class="row g-4">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-envelope me-2 text-primary"></i>Pesan Terbaru</span>
                <?php if ($unread > 0): ?>
                    <span class="badge bg-danger"><?= $unread ?> belum dibaca</span>
                <?php endif; ?>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table mb-0">
                        <thead class="table-light">
                            <tr><th>Nama</th><th>Subjek</th><th>Tanggal</th><th>Status</th></tr>
                        </thead>
                        <tbody>
                        <?php foreach ($recentMessages as $msg): ?>
                            <tr>
                                <td class="fw-semibold"><?= htmlspecialchars($msg['name']) ?></td>
                                <td class="text-muted"><?= htmlspecialchars(substr($msg['subject']??'',0,40)) ?></td>
                                <td class="text-muted small"><?= date('d/m/Y', strtotime($msg['created_at'])) ?></td>
                                <td><?php if ($msg['is_read']): ?>
                                    <span class="badge badge-active px-2">Dibaca</span>
                                <?php else: ?>
                                    <span class="badge bg-warning text-dark px-2">Baru</span>
                                <?php endif; ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($recentMessages)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-3">Belum ada pesan masuk</td></tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="card-footer bg-white border-top-0">
                <a href="messages.php" class="btn btn-sm btn-outline-primary">Lihat Semua Pesan</a>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header"><i class="bi bi-lightning me-2 text-warning"></i>Quick Links</div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="sliders.php" class="btn btn-outline-secondary text-start"><i class="bi bi-plus-circle me-2"></i>Tambah Slider</a>
                    <a href="news.php" class="btn btn-outline-secondary text-start"><i class="bi bi-plus-circle me-2"></i>Tambah Berita</a>
                    <a href="gallery-photos.php" class="btn btn-outline-secondary text-start"><i class="bi bi-plus-circle me-2"></i>Tambah Foto Galeri</a>
                    <a href="brand-products.php" class="btn btn-outline-secondary text-start"><i class="bi bi-grid-3x3-gap me-2"></i>Kelola Produk Brand</a>
                    <a href="projects.php" class="btn btn-outline-secondary text-start"><i class="bi bi-plus-circle me-2"></i>Tambah Project</a>
                    <a href="catalogs.php" class="btn btn-outline-secondary text-start"><i class="bi bi-plus-circle me-2"></i>Upload Katalog</a>
                    <a href="settings.php" class="btn btn-outline-dark text-start"><i class="bi bi-gear me-2"></i>Pengaturan Site</a>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'partials/footer.php'; ?>
