<?php 
require_once 'includes/db.php';

$slug = $_GET['slug'] ?? '';
$slug = $conn->real_escape_string($slug);

$page_query = $conn->query("SELECT * FROM dynamic_pages WHERE slug='$slug'");
if($page_query && $page_query->num_rows > 0) {
    $page_data = $page_query->fetch_assoc();
} else {
    // Redirect to home if page doesn't exist
    header("Location: index.php");
    exit;
}

require_once 'includes/header.php'; 
?>

<!-- Page Hero Banner -->
<div class="position-relative" style="background: linear-gradient(135deg, #A20E37 0%, #7b0a28 60%, #FBBC09 100%); padding: 60px 0; margin-bottom: 0;">
    <div class="container text-white text-center">
        <h1 class="fw-bold mb-2" style="font-size:2.5rem; letter-spacing:1px;"><?php echo htmlspecialchars($page_data['title']); ?></h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb justify-content-center mb-0">
                <li class="breadcrumb-item"><a href="index.php" class="text-warning">Home</a></li>
                <li class="breadcrumb-item active text-white" aria-current="page"><?php echo htmlspecialchars($page_data['title']); ?></li>
            </ol>
        </nav>
    </div>
</div>

<div class="container my-5">
    <div class="row">
        <!-- Main Content Column -->
        <div class="col-lg-<?php echo (!empty($page_data['pdf_file'])) ? '7' : '12'; ?> mb-4">
            <div class="card shadow-sm border-0 rounded-4 p-4 h-100">
                <h3 class="fw-bold text-dark mb-4" style="color: #A20E37 !important;">About <?php echo htmlspecialchars($page_data['title']); ?></h3>
                <div class="page-content" style="font-size: 1.1rem; line-height: 1.8; color: #475569;">
                    <?php echo $page_data['content']; ?>
                </div>
            </div>
        </div>
        
        <!-- PDF Viewer Column (Only show if PDF exists) -->
        <?php if(!empty($page_data['pdf_file'])): ?>
        <div class="col-lg-5 mb-4">
            <div class="card shadow-sm border-0 rounded-4 overflow-hidden h-100">
                <div class="card-header bg-light text-center py-3 border-bottom">
                    <h5 class="fw-bold text-dark mb-0"><i class="fas fa-file-pdf me-2" style="color:#A20E37;"></i> View Document</h5>
                </div>
                <div class="card-body p-0" style="min-height: 600px;">
                    <?php 
                    $pdf_url = 'uploads/' . htmlspecialchars($page_data['pdf_file']);
                    if(file_exists($pdf_url)):
                    ?>
                        <iframe src="<?php echo $pdf_url; ?>" width="100%" height="100%" style="border: none; min-height: 600px;"></iframe>
                    <?php else: ?>
                        <div class="d-flex flex-column justify-content-center align-items-center h-100 p-5 text-center bg-light">
                            <i class="fas fa-exclamation-triangle fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Document file is missing.</h5>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="card-footer bg-white text-center">
                    <a href="<?php echo $pdf_url; ?>" download class="btn btn-primary px-4 rounded-pill shadow-sm"><i class="fas fa-download me-2"></i> Download PDF</a>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<style>
    /* Styling for the rich text content inside page-content */
    .page-content p { margin-bottom: 1.5rem; }
    .page-content ul, .page-content ol { margin-bottom: 1.5rem; padding-left: 2rem; }
    .page-content li { margin-bottom: 0.5rem; }
</style>

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