<?php
/**
 * Admin Pages Management
 */
require_once 'includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'save') {
        $pages = ['about', 'terms', 'privacy', 'faq'];
        foreach ($pages as $page) {
            $content = $_POST[$page] ?? '';
            $exists = dbFetch("SELECT id FROM settings WHERE setting_key = ?", ["page_$page"]);
            if ($exists) {
                dbUpdate('settings', ['setting_value' => $content], 'setting_key = ?', ["page_$page"]);
            } else {
                dbInsert('settings', ['setting_key' => "page_$page", 'setting_value' => $content]);
            }
        }
        echo '<div class="alert alert-success"><i class="fas fa-check"></i> Pages updated!</div>';
    }
}

$about = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'page_about'")['setting_value'] ?? '';
$terms = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'page_terms'")['setting_value'] ?? '';
$privacy = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'page_privacy'")['setting_value'] ?? '';
$faq = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'page_faq'")['setting_value'] ?? '';
?>

<div class="page-header">
    <h1 class="page-title">Manage Pages</h1>
</div>

<form method="POST">
    <input type="hidden" name="action" value="save">
    
    <div class="card mb-4">
        <div class="card-header"><h4>About Us</h4></div>
        <div class="card-body">
            <textarea name="about" class="form-control" rows="6"><?= htmlspecialchars($about) ?></textarea>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header"><h4>Terms & Conditions</h4></div>
        <div class="card-body">
            <textarea name="terms" class="form-control" rows="6"><?= htmlspecialchars($terms) ?></textarea>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header"><h4>Privacy Policy</h4></div>
        <div class="card-body">
            <textarea name="privacy" class="form-control" rows="6"><?= htmlspecialchars($privacy) ?></textarea>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header"><h4>FAQ</h4></div>
        <div class="card-body">
            <textarea name="faq" class="form-control" rows="6"><?= htmlspecialchars($faq) ?></textarea>
        </div>
    </div>
    
    <button type="submit" class="btn btn-primary btn-lg">
        <i class="fas fa-save"></i> Save All Pages
    </button>
</form>

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