<?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';

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: pages.php');
    exit;
}

$id = (int)$_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM pages WHERE id = ?");
$stmt->execute([$id]);
$page = $stmt->fetch();

if (!$page) {
    header('Location: pages.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = trim($_POST['title']);
    $content = trim($_POST['content']);

    if (!empty($title)) {
        try {
            $stmt = $pdo->prepare("UPDATE pages SET title = ?, content = ? WHERE id = ?");
            $stmt->execute([$title, $content, $id]);
            header('Location: pages.php?success=edit');
            exit;
        } catch (PDOException $e) {
            $error = $t['error_db_update'] . ' ' . $e->getMessage();
        }
    } else {
        $error = $t['error_title_required'];
    }
}
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<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['edit_page_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="sliders.php"><span class="nav-icon">🖼️</span><?php echo $t['admin_sliders']; ?></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['edit_page_title']; ?>: <?php echo htmlspecialchars($page['title']); ?></h1>
            <a href="pages.php" class="btn btn-outline-secondary px-4 py-2"><?php echo $t['page_back_btn']; ?></a>
        </div>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger fw-bold mb-4">⚠️ <?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <div class="table-card p-4 p-md-5 mx-auto" style="max-width: 800px;">
            <form method="POST">
                <div class="mb-3">
                    <label for="title" class="form-label"><?php echo $t['page_title_field']; ?> *</label>
                    <input type="text" id="title" name="title" class="form-control form-control-lg" required value="<?php echo htmlspecialchars($page['title']); ?>">
                </div>

                <div class="mb-3">
                    <label for="slug" class="form-label text-muted"><?php echo $t['pages_table_slug']; ?> (<?php echo $isRTL ? 'للقراءة فقط' : 'Read-only'; ?>)</label>
                    <input type="text" id="slug" class="form-control" disabled value="<?php echo htmlspecialchars($page['slug']); ?>" style="cursor: not-allowed; font-family: monospace;">
                </div>
                
                <div class="mb-4">
                    <label for="content" class="form-label"><?php echo $t['page_content_field']; ?></label>
                    <textarea id="content" name="content" class="form-control form-control-lg" rows="12" placeholder="<?php echo $isRTL ? 'أدخل محتوى الصفحة هنا...' : 'Enter page content here...'; ?>"><?php echo htmlspecialchars($page['content']); ?></textarea>
                </div>
                
                <div class="d-flex gap-3">
                    <button type="submit" class="btn btn-gold flex-grow-1 py-2 fw-bold fs-6">💾 <?php echo $t['page_save_btn']; ?></button>
                    <a href="pages.php" class="btn btn-outline-secondary py-2 px-4"><?php echo $t['user_cancel_btn']; ?></a>
                </div>
            </form>
        </div>
    </div>
    
    <!-- Bootstrap 5 JS Bundle CDN -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>
</html>
