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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$res = $conn->query("SELECT * FROM notices WHERE id = $id");

if(!$res || $res->num_rows == 0) {
    echo "<div class='container my-5 text-center'><h3>Notice not found.</h3><a href='index.php' class='btn btn-primary mt-3'>Back to Home</a></div>";
    require_once 'includes/footer.php';
    exit();
}

$notice = $res->fetch_assoc();
?>

<div class="container my-5 pt-5">
    <div class="row justify-content-center">
        <div class="col-lg-10">
            <nav aria-label="breadcrumb" class="mb-4">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                    <li class="breadcrumb-item active" aria-current="page"><?php echo  h($notice['category']) ?></li>
                </ol>
            </nav>

            <div class="card border-0 shadow-lg rounded-4 overflow-hidden">
                <?php if(!empty($notice['image'])): ?>
                <img src="uploads/notices/<?php echo  $notice['image'] ?>" class="card-img-top w-100" style="max-height: 500px; object-fit: cover;" alt="<?php echo  h($notice['title']) ?>">
                <?php endif; ?>
                
                <div class="card-body p-4 p-md-5">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <span class="badge bg-primary px-3 py-2 fs-6"><?php echo  h($notice['category']) ?></span>
                        <span class="text-muted"><i class="far fa-calendar-alt me-2"></i> Published on: <?php echo  date('d M, Y', strtotime($notice['created_at'])) ?></span>
                    </div>
                    
                    <h1 class="fw-bold mb-4" style="color: #1e3a8a;"><?php echo  h($notice['title']) ?></h1>
                    
                    <div class="content-area mt-5" style="line-height: 1.8; font-size: 1.1rem; color: #334155;">
                        <!-- This content is from CKEditor and may contain HTML tags -->
                        <?php echo  $notice['content'] ?>
                    </div>

                    <?php if(!empty($notice['pdf_file'])): ?>
                    <div class="mt-5 p-4 bg-light rounded-3 d-flex align-items-center justify-content-between">
                        <div>
                            <h5 class="mb-1 fw-bold">Official Document Attached</h5>
                            <p class="mb-0 text-muted small">Download the original circular/order in PDF format.</p>
                        </div>
                        <a href="uploads/notices/<?php echo  $notice['pdf_file'] ?>" target="_blank" class="btn btn-danger btn-lg px-4 shadow">
                            <i class="fas fa-file-pdf me-2"></i> Download PDF
                        </a>
                    </div>
                    <?php endif; ?>

                    <div class="mt-5 text-center border-top pt-4">
                        <a href="index.php" class="btn btn-outline-secondary px-5"><i class="fas fa-arrow-left me-2"></i> Back to Home</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .content-area ul, .content-area ol { padding-left: 20px; margin-bottom: 20px; }
    .content-area h1, .content-area h2, .content-area h3 { color: #1e293b; margin-top: 30px; font-weight: 700; }
    .content-area p { margin-bottom: 20px; }
</style>

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