<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; }
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

$stmt = $pdo->query("SELECT * FROM pages ORDER BY id ASC");
$pages = $stmt->fetchAll();
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['pages_title']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="sidebar">
        <div class="sidebar-brand"><h2>VAULTIXAI</h2></div>
        <nav><ul>
            <li><a href="dashboard.php"><span class="nav-icon">🏠</span><?php echo $t['admin_dashboard']; ?></a></li>
            <li><a href="pages.php" class="active"><span class="nav-icon">📄</span><?php echo $t['admin_pages']; ?></a></li>
            <li><a href="certifications.php"><span class="nav-icon">🏆</span><?php echo $t['admin_certifications']; ?></a></li>
            <li><a href="services.php"><span class="nav-icon">⚙️</span><?php echo $t['admin_services']; ?></a></li>
            <li><a href="users.php"><span class="nav-icon">👥</span><?php echo $t['admin_users']; ?></a></li>
        </ul></nav>
        <a href="<?php echo lang_switch_url($t['lang_switch_code']); ?>" class="sidebar-lang-btn">🌐 <?php echo $t['lang_switch_label']; ?></a>
        <div class="sidebar-footer"><a href="logout.php"><span class="nav-icon">🚪</span><?php echo $t['admin_logout']; ?></a></div>
    </div>

    <div class="main-content">
        <div class="topbar">
            <h1>📄 <?php echo $t['pages_title']; ?></h1>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="alert alert-success fw-bold mb-4">✅ <?php echo $isRTL ? 'تم تحديث الصفحة بنجاح.' : 'Page updated successfully.'; ?></div>
        <?php endif; ?>

        <div class="table-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th style="width:60px;"><?php echo $t['users_table_id']; ?></th>
                            <th><?php echo $t['pages_table_title']; ?></th>
                            <th style="width:160px;"><?php echo $t['pages_table_slug']; ?></th>
                            <th style="width:180px;text-align:center;"><?php echo $t['pages_table_actions']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $pageIcons = ['home'=>'🏠','about'=>'ℹ️','services'=>'⚙️','certifications'=>'🏆','contact'=>'📞'];
                        foreach ($pages as $p): ?>
                        <tr>
                            <td class="text-muted small fw-bold">#<?php echo $p['id']; ?></td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <span style="font-size:1.2rem;"><?php echo $pageIcons[$p['slug']] ?? '📄'; ?></span>
                                    <span class="fw-bold text-dark"><?php echo htmlspecialchars($p['title']); ?></span>
                                </div>
                            </td>
                            <td><code class="text-muted small"><?php echo htmlspecialchars($p['slug']); ?></code></td>
                            <td style="text-align:center;">
                                <a href="edit_page.php?id=<?php echo $p['id']; ?>" class="btn btn-edit btn-sm px-3 py-1">✏️ <?php echo $t['pages_edit_btn']; ?></a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
