<?php
$pageTitle = 'Dashboard';
require_once __DIR__ . '/includes/header.php';

// Stats
$stats = [];
$tables = ['blog_posts','pages','services','destinations','universities','gallery','contact_messages','success_stories'];
foreach ($tables as $t) {
    try {
        $r = db()->query("SELECT COUNT(*) as c FROM $t");
        $stats[$t] = $r ? (int)$r->fetch_assoc()['c'] : 0;
    } catch (Exception $e) { $stats[$t] = 0; }
}

// Recent messages
$messages = [];
try {
    $r = db()->query("SELECT * FROM contact_messages ORDER BY created_at DESC LIMIT 5");
    $messages = $r ? $r->fetch_all(MYSQLI_ASSOC) : [];
} catch (Exception $e) {}

// Recent blog posts
$posts = [];
try {
    $r = db()->query("SELECT id,title,status,created_at FROM blog_posts ORDER BY created_at DESC LIMIT 5");
    $posts = $r ? $r->fetch_all(MYSQLI_ASSOC) : [];
} catch (Exception $e) {}
?>

<div class="page-header">
    <div>
        <h1>Dashboard</h1>
        <p style="color:var(--muted);font-size:13px;margin-top:2px">Welcome back, <?= e($admin['full_name'] ?: $admin['username']) ?>! Here's what's happening.</p>
    </div>
    <div class="page-header-actions">
        <a href="<?= ADMIN_URL ?>/pages/blog-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> New Post</a>
    </div>
</div>

<!-- Stats -->
<div class="stats-grid">
    <a href="pages/pages.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#dbeafe;color:#1a56db"><i class="fas fa-file-alt"></i></div>
        <div>
            <div class="stat-num"><?= $stats['pages'] ?></div>
            <div class="stat-label">Website Pages</div>
        </div>
    </a>
    <a href="pages/blog.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#d1fae5;color:#065f46"><i class="fas fa-rss"></i></div>
        <div>
            <div class="stat-num"><?= $stats['blog_posts'] ?></div>
            <div class="stat-label">Blog Posts</div>
        </div>
    </a>
    <a href="pages/services.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#fef3c7;color:#92400e"><i class="fas fa-concierge-bell"></i></div>
        <div>
            <div class="stat-num"><?= $stats['services'] ?></div>
            <div class="stat-label">Services</div>
        </div>
    </a>
    <a href="pages/destinations.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#ede9fe;color:#5b21b6"><i class="fas fa-globe-asia"></i></div>
        <div>
            <div class="stat-num"><?= $stats['destinations'] ?></div>
            <div class="stat-label">Destinations</div>
        </div>
    </a>
    <a href="pages/universities.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#fce7f3;color:#831843"><i class="fas fa-university"></i></div>
        <div>
            <div class="stat-num"><?= $stats['universities'] ?></div>
            <div class="stat-label">Universities</div>
        </div>
    </a>
    <a href="pages/gallery.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#d1fae5;color:#065f46"><i class="fas fa-images"></i></div>
        <div>
            <div class="stat-num"><?= $stats['gallery'] ?></div>
            <div class="stat-label">Gallery Photos</div>
        </div>
    </a>
    <a href="pages/success.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#fef3c7;color:#92400e"><i class="fas fa-star"></i></div>
        <div>
            <div class="stat-num"><?= $stats['success_stories'] ?></div>
            <div class="stat-label">Success Stories</div>
        </div>
    </a>
    <a href="pages/applications.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#dbeafe;color:#1e40af"><i class="fas fa-file-alt"></i></div>
        <div>
            <div class="stat-num"><?= $stats['applications'] ?? 0 ?></div>
            <div class="stat-label">Applications</div>
        </div>
    </a>
    <a href="pages/messages.php" class="stat-card" style="text-decoration:none">
        <div class="stat-icon" style="background:#fee2e2;color:#991b1b"><i class="fas fa-envelope"></i></div>
        <div>
            <div class="stat-num"><?= $stats['contact_messages'] ?></div>
            <div class="stat-label">Messages <?php if($unreadCount>0): ?><span class="badge badge-red" style="font-size:9px"><?= $unreadCount ?> new</span><?php endif; ?></div>
        </div>
    </a>
</div>

<!-- Quick Actions -->
<div class="card" style="margin-bottom:24px">
    <div class="card-header"><i class="fas fa-bolt" style="color:var(--accent)"></i><h2>Quick Actions</h2></div>
    <div class="card-body" style="display:flex;flex-wrap:wrap;gap:10px">
        <a href="pages/blog-add.php" class="btn btn-secondary"><i class="fas fa-plus"></i> New Blog Post</a>
        <a href="pages/page-add.php" class="btn btn-secondary"><i class="fas fa-file-plus"></i> New Page</a>
        <a href="pages/service-add.php" class="btn btn-secondary"><i class="fas fa-plus-circle"></i> Add Service</a>
        <a href="pages/dest-add.php" class="btn btn-secondary"><i class="fas fa-map-marker-plus"></i> Add Destination</a>
        <a href="pages/uni-add.php" class="btn btn-secondary"><i class="fas fa-plus"></i> Add University</a>
        <a href="pages/gallery-add.php" class="btn btn-secondary"><i class="fas fa-upload"></i> Upload Photos</a>
        <a href="pages/settings.php" class="btn btn-secondary"><i class="fas fa-cog"></i> Site Settings</a>
    </div>
</div>

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

<!-- Recent Blog Posts -->
<div class="card">
    <div class="card-header">
        <i class="fas fa-rss" style="color:var(--brand)"></i>
        <h2>Recent Blog Posts</h2>
        <div class="card-actions">
            <a href="pages/blog.php" class="btn btn-secondary btn-sm">View All</a>
        </div>
    </div>
    <div class="table-wrap">
        <?php if (empty($posts)): ?>
            <div style="padding:24px;text-align:center;color:var(--muted)">No blog posts yet. <a href="pages/blog-add.php">Add one</a></div>
        <?php else: ?>
        <table>
            <thead><tr><th>Title</th><th>Status</th><th>Date</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($posts as $p): ?>
            <tr>
                <td style="max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= e($p['title']) ?></td>
                <td><span class="badge <?= $p['status']==='published' ? 'badge-green' : 'badge-yellow' ?>"><?= ucfirst($p['status']) ?></span></td>
                <td style="white-space:nowrap;color:var(--muted);font-size:12px"><?= date('M j, Y', strtotime($p['created_at'])) ?></td>
                <td><a href="pages/blog-edit.php?id=<?= $p['id'] ?>" class="btn btn-secondary btn-sm btn-icon"><i class="fas fa-edit"></i></a></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<!-- Recent Messages -->
<div class="card">
    <div class="card-header">
        <i class="fas fa-envelope" style="color:var(--danger)"></i>
        <h2>Recent Messages</h2>
        <div class="card-actions">
            <a href="pages/messages.php" class="btn btn-secondary btn-sm">View All</a>
        </div>
    </div>
    <div class="table-wrap">
        <?php if (empty($messages)): ?>
            <div style="padding:24px;text-align:center;color:var(--muted)">No messages yet.</div>
        <?php else: ?>
        <table>
            <thead><tr><th>Name</th><th>Subject</th><th>Date</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($messages as $m): ?>
            <tr style="<?= !$m['is_read'] ? 'font-weight:600' : '' ?>">
                <td><?= e($m['full_name']) ?></td>
                <td style="max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--muted)"><?= e($m['subject'] ?: 'No Subject') ?></td>
                <td style="white-space:nowrap;color:var(--muted);font-size:12px"><?= date('M j', strtotime($m['created_at'])) ?></td>
                <td><a href="pages/messages.php?view=<?= $m['id'] ?>" class="btn btn-secondary btn-sm btn-icon"><i class="fas fa-eye"></i></a></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
