<?php
$admin_page = 'dashboard';
$admin_title = 'Dashboard';
include 'admin_header.php';

try {
    $total_products = db()->query("SELECT COUNT(*) FROM vs_products")->fetchColumn();
    $total_cats     = db()->query("SELECT COUNT(*) FROM vs_categories")->fetchColumn();
    $total_msgs     = db()->query("SELECT COUNT(*) FROM vs_messages")->fetchColumn();
    $unread_msgs    = db()->query("SELECT COUNT(*) FROM vs_messages WHERE is_read=0")->fetchColumn();
    $recent_msgs    = db()->query("SELECT * FROM vs_messages ORDER BY created_at DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
    $recent_prods   = db()->query("SELECT p.*,c.name as cat_name FROM vs_products p LEFT JOIN vs_categories c ON p.category_id=c.id ORDER BY p.created_at DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
} catch(Exception $e) {
    $total_products = $total_cats = $total_msgs = $unread_msgs = 0;
    $recent_msgs = $recent_prods = [];
}
?>

<div class="page-header">
    <div><h1>Dashboard</h1><p>Genel bakış ve hızlı erişim</p></div>
    <a href="/volker/admin/urunler.php?action=add" class="btn btn-primary">
        <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg>
        Yeni Ürün
    </a>
</div>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-card-num"><?= $total_products ?></div>
        <div class="stat-card-label">Toplam Ürün</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-num"><?= $total_cats ?></div>
        <div class="stat-card-label">Kategori</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-num"><?= $total_msgs ?></div>
        <div class="stat-card-label">Mesaj</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-num" style="color:<?= $unread_msgs > 0 ? 'var(--green)' : 'var(--red)' ?>"><?= $unread_msgs ?></div>
        <div class="stat-card-label">Okunmamış Mesaj</div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:24px;">

    <!-- Recent products -->
    <div class="admin-card">
        <div class="admin-card-head">
            <h2>Son Eklenen Ürünler</h2>
            <a href="/volker/admin/urunler.php" class="btn btn-outline btn-sm">Tümü</a>
        </div>
        <table>
            <thead><tr><th>Ürün</th><th>Kategori</th><th>Durum</th></tr></thead>
            <tbody>
            <?php if (empty($recent_prods)): ?>
            <tr><td colspan="3" style="text-align:center;color:var(--gray);padding:32px;">Henüz ürün yok</td></tr>
            <?php else: ?>
            <?php foreach($recent_prods as $p): ?>
            <tr>
                <td><a href="/volker/admin/urunler.php?action=edit&id=<?= $p['id'] ?>" style="color:#fff;font-weight:600;"><?= clean($p['name']) ?></a></td>
                <td style="color:var(--gray);"><?= clean($p['cat_name'] ?? '—') ?></td>
                <td><span class="badge <?= $p['status']=='active'?'badge-green':'badge-gray' ?>"><?= $p['status']=='active'?'Aktif':'Taslak' ?></span></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

    <!-- Recent messages -->
    <div class="admin-card">
        <div class="admin-card-head">
            <h2>Son Mesajlar</h2>
            <a href="/volker/admin/mesajlar.php" class="btn btn-outline btn-sm">Tümü</a>
        </div>
        <table>
            <thead><tr><th>Gönderen</th><th>Konu</th><th>Tarih</th></tr></thead>
            <tbody>
            <?php if (empty($recent_msgs)): ?>
            <tr><td colspan="3" style="text-align:center;color:var(--gray);padding:32px;">Henüz mesaj yok</td></tr>
            <?php else: ?>
            <?php foreach($recent_msgs as $m): ?>
            <tr style="<?= !$m['is_read'] ? 'background:rgba(224,32,32,0.04);' : '' ?>">
                <td style="font-weight:<?= !$m['is_read']?'700':'400' ?>;"><?= clean($m['name']) ?></td>
                <td style="color:var(--gray);"><?= clean(mb_substr($m['subject'] ?: 'Konu yok', 0, 30)) ?></td>
                <td style="color:var(--gray);font-size:12px;"><?= date('d.m.Y', strtotime($m['created_at'])) ?></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

</div>

<?php include 'admin_footer.php'; ?>
