<?php
$json_file 
'pages.json';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
$pages = [];
    foreach (
$_POST['title'] as $index => $title) {
        
$pages[] = [
            
'title' => $title,
            
'text' => $_POST['text'][$index],
            
'image' => $_POST['image'][$index],
            
'backgroundColor' => $_POST['background'][$index] ?? ''
        
];
    }
    
    
// Add new empty page if requested
    
if (isset($_POST['add_page'])) {
        
$pages[] = [
            
'title' => 'New Page',
            
'text' => '',
            
'image' => '',
            
'backgroundColor' => ''
        
];
    }

    
file_put_contents($json_filejson_encode($pagesJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE));
}

// Load existing pages
$pages json_decode(file_get_contents($json_file), true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CMS</title>
    <style>
        .page-form { margin: 20px; padding: 20px; border: 1px solid #ccc; }
        .form-group { margin: 10px 0; }
        label { display: block; font-weight: bold; }
        textarea { width: 100%; height: 100px; }
    </style>
</head>
<body>
    <h1>Content Management System</h1>
    <form method="post">
        <?php foreach ($pages as $index => $page): ?>
        <div class="page-form">
            <h2>Page <?= $index 1 ?></h2>
            <div class="form-group">
                <label>Title:</label>
                <input type="text" name="title[]" value="<?= htmlspecialchars($page['title']) ?>" required>
            </div>
            <div class="form-group">
                <label>Text:</label>
                <textarea name="text[]" required><?= htmlspecialchars($page['text']) ?></textarea>
            </div>
            <div class="form-group">
                <label>Image file:</label>
                <input type="text" name="image[]" value="<?= htmlspecialchars($page['image'] ?? ''?>">
            </div>
           
        </div>
        <?php endforeach; ?>

        <div class="form-group">
            <button type="submit">Save All Changes</button>
            <button type="submit" name="add_page">Add New Page</button>
        </div>
    </form>
</body>
</html>