<?php
/**
 * CMS Page Handler
 * Digital Marketplace
 */

require_once __DIR__ . '/config/database.php';

$slug = sanitize($_GET['slug'] ?? '');

if (empty($slug)) {
    header('Location: ' . BASE_URL . '/index.php');
    exit;
}

// Get page
$page = get_page_by_slug($slug);

if (!$page) {
    http_response_code(404);
    $page_title = 'Page Not Found';
    include __DIR__ . '/includes/header.php';
    ?>
    <section class="section">
        <div class="container text-center py-5">
            <i class="fas fa-exclamation-circle fa-5x text-muted mb-4"></i>
            <h1>Page Not Found</h1>
            <p class="text-muted mb-4">The page you are looking for does not exist.</p>
            <a href="<?php echo BASE_URL; ?>/index.php" class="btn btn-primary">
                <i class="fas fa-home me-2"></i>Go Home
            </a>
        </div>
    </section>
    <?php
    include __DIR__ . '/includes/footer.php';
    exit;
}

$page_title = $page['title'];

include __DIR__ . '/includes/header.php';
?>

<section class="section">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-10" data-aos="fade-up">
                <h1 class="mb-4"><?php echo sanitize($page['title']); ?></h1>
                
                <?php if ($page['featured_image']): ?>
                <img src="<?php echo UPLOAD_URL . '/' . $page['featured_image']; ?>" 
                     alt="<?php echo sanitize($page['title']); ?>"
                     class="img-fluid rounded mb-4"
                     style="max-height: 400px; width: 100%; object-fit: cover;">
                <?php endif; ?>
                
                <div class="page-content">
                    <?php echo $page['content']; ?>
                </div>
                
                <?php if ($page['updated_at']): ?>
                <div class="mt-5 pt-4 border-top border-secondary">
                    <small class="text-muted">
                        <i class="fas fa-clock me-1"></i>
                        Last updated: <?php echo format_date($page['updated_at']); ?>
                    </small>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

<style>
.page-content {
    color: var(--gray-300);
    line-height: 1.8;
}

.page-content h2,
.page-content h3,
.page-content h4 {
    color: var(--white);
    margin-top: 2rem;
    margin-bottom: 1rem;
}

.page-content p {
    margin-bottom: 1rem;
}

.page-content ul,
.page-content ol {
    margin-bottom: 1rem;
    padding-left: 1.5rem;
}

.page-content li {
    margin-bottom: 0.5rem;
}

.page-content a {
    color: var(--primary);
    text-decoration: none;
}

.page-content a:hover {
    text-decoration: underline;
}

.page-content img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
}

.page-content blockquote {
    border-left: 4px solid var(--primary);
    padding-left: 1rem;
    margin: 1.5rem 0;
    color: var(--gray-400);
    font-style: italic;
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>
