<?php
$admin_title = "Dashboard";
require_once 'includes/header.php';

// Fetch Counts
try {
    $count_products = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
    $count_gallery = $pdo->query("SELECT COUNT(*) FROM gallery")->fetchColumn();
    $count_messages = $pdo->query("SELECT COUNT(*) FROM messages")->fetchColumn();
    $count_unread_messages = $pdo->query("SELECT COUNT(*) FROM messages WHERE is_read = 0")->fetchColumn();
} catch (Exception $e) {
    $count_products = 0;
    $count_gallery = 0;
    $count_messages = 0;
    $count_unread_messages = 0;
}

// Fetch recent messages
try {
    $recent_messages = $pdo->query("SELECT * FROM messages ORDER BY id DESC LIMIT 5")->fetchAll();
} catch (Exception $e) {
    $recent_messages = [];
}
?>

<!-- Stat Cards -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-card-details">
            <h3>Total Produk</h3>
            <div class="value"><?php echo $count_products; ?></div>
        </div>
        <div class="stat-card-icon icon-primary">
            <i class="fas fa-boxes"></i>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-card-details">
            <h3>Galeri Foto</h3>
            <div class="value"><?php echo $count_gallery; ?></div>
        </div>
        <div class="stat-card-icon icon-success">
            <i class="fas fa-images"></i>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-card-details">
            <h3>Pesan Masuk</h3>
            <div class="value"><?php echo $count_messages; ?></div>
        </div>
        <div class="stat-card-icon icon-warning">
            <i class="fas fa-envelope"></i>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-card-details">
            <h3>Pesan Belum Dibaca</h3>
            <div class="value"><?php echo $count_unread_messages; ?></div>
        </div>
        <div class="stat-card-icon icon-error">
            <i class="fas fa-envelope-open"></i>
        </div>
    </div>
</div>

<!-- Recent Messages Card -->
<div class="data-card">
    <div class="data-card-header">
        <h2>Pesan Masuk Terbaru</h2>
        <a href="messages.php" class="btn-add" style="background-color: var(--secondary); padding: 8px 14px; text-decoration: none;">Lihat Semua</a>
    </div>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Pengirim</th>
                    <th>Subjek</th>
                    <th>Tanggal</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($recent_messages) > 0): ?>
                    <?php foreach ($recent_messages as $msg): ?>
                        <tr>
                            <td>
                                <strong><?php echo htmlspecialchars($msg['name']); ?></strong><br>
                                <span style="font-size: 11px; color: var(--text-muted);"><?php echo htmlspecialchars($msg['email']); ?></span>
                            </td>
                            <td><?php echo htmlspecialchars($msg['subject']); ?></td>
                            <td><?php echo date('d M Y, H:i', strtotime($msg['created_at'])); ?></td>
                            <td>
                                <?php if ($msg['is_read'] == 1): ?>
                                    <span class="badge badge-success">Dibaca</span>
                                <?php else: ?>
                                    <span class="badge badge-warning">Baru</span>
                                <?php endif; ?>
                            </td>
                            <td class="action-btns">
                                <a href="messages.php?id=<?php echo $msg['id']; ?>" class="btn-action view" title="Lihat Pesan"><i class="fas fa-eye"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" style="text-align: center; color: var(--text-muted); padding: 30px;">Belum ada pesan masuk.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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