<?php
// Filename: public.php
require 'config.php';

// --- CHECK MASTER SWITCH ---
if (getSetting('public_portal_enabled', '1') !== '1') {
    die("<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width,initial-scale=1.0'><title>Portal Closed</title><link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'></head><body class='bg-light d-flex align-items-center justify-content-center vh-100'><div class='text-center p-5 bg-white shadow rounded-4'><h4><i class='bi bi-cone-striped text-warning display-4 d-block mb-3'></i>Portal Unavailable</h4><p class='text-muted small'>This portal is currently disabled by the administrator.</p><a href='index.php' class='btn btn-outline-primary rounded-pill mt-3 px-4'>Staff Login</a></div></body></html>");
}

$s_name = getSetting('school_name', 'SAS');
$s_logo = getSetting('school_logo', '');
$s_addr = getSetting('school_address', '');
$s_phone = getSetting('school_phone', '');

// Fetch Data
$news = $pdo->query("SELECT * FROM public_content WHERE type='news' ORDER BY created_at DESC LIMIT 10")->fetchAll();
$slides = $pdo->query("SELECT * FROM public_content WHERE type='carousel' ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($s_name) ?> - Portal</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Poppins', sans-serif; background-color: #f0f2f5; padding-bottom: 60px; }
        
        /* SEAMLESS HEADER */
        .header-card {
            background: linear-gradient(135deg, #0d6efd 0%, #004494 100%);
            color: white; border-radius: 0 0 30px 30px;
            padding: 40px 20px 80px 20px; margin-bottom: -60px; text-align: center;
            box-shadow: 0 4px 20px rgba(13, 110, 253, 0.2); position: relative;
        }
        .school-logo { 
            width: 90px; height: 90px; object-fit: contain; margin-bottom: 10px; 
            filter: drop-shadow(0 4px 6px rgba(0,0,0,0.2)); 
        }
        .login-btn {
            position: absolute; top: 20px; right: 20px;
            background: rgba(255,255,255,0.2); color: white; border: 1px solid rgba(255,255,255,0.3);
            backdrop-filter: blur(5px); transition: 0.2s;
        }
        .login-btn:hover { background: white; color: #0d6efd; }

        /* CAROUSEL */
        .hero-carousel {
            border-radius: 20px; overflow: hidden; box-shadow: 0 15px 35px rgba(0,0,0,0.15);
            margin-bottom: 30px; border: 4px solid white;
        }
        .carousel-item img { height: 400px; object-fit: cover; }
        
        /* QUICK LINKS */
        .icon-card {
            background: white; border-radius: 15px; padding: 20px; text-align: center;
            box-shadow: 0 4px 15px rgba(0,0,0,0.05); border: 0; text-decoration: none; color: #495057;
            transition: transform 0.2s, box-shadow 0.2s; display: block; height: 100%;
        }
        .icon-card:hover { transform: translateY(-5px); box-shadow: 0 10px 25px rgba(0,0,0,0.1); color: #0d6efd; }
        .icon-circle {
            width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
            margin: 0 auto 15px auto; font-size: 1.8rem; transition: 0.2s;
        }
        .ic-cal { background: #e7f1ff; color: #0d6efd; } .icon-card:hover .ic-cal { background: #0d6efd; color: white; }
        .ic-par { background: #d1e7dd; color: #198754; } .icon-card:hover .ic-par { background: #198754; color: white; }
        .ic-inf { background: #f8d7da; color: #dc3545; } .icon-card:hover .ic-inf { background: #dc3545; color: white; }

        /* NEWS FEED */
        .news-card {
            background: white; border-radius: 15px; border: 0; box-shadow: 0 2px 10px rgba(0,0,0,0.03);
            margin-bottom: 15px; overflow: hidden; border-left: 5px solid #0d6efd;
        }
        .news-header { padding: 15px 20px; border-bottom: 1px solid #f8f9fa; display: flex; justify-content: space-between; align-items: center; }
        .news-body { padding: 20px; color: #555; line-height: 1.6; }
        
        /* FOOTER */
        .simple-footer { text-align: center; color: #adb5bd; font-size: 0.8rem; margin-top: 40px; }

        @media (max-width: 768px) { 
            .carousel-item img { height: 220px; } 
            .header-card { padding-bottom: 60px; margin-bottom: -40px; }
        }
    </style>
</head>
<body>

    <div class="header-card">
        <a href="index.php" class="btn btn-sm rounded-pill px-3 fw-bold login-btn"><i class="bi bi-lock-fill me-1"></i> Staff</a>
        
        <?php if($s_logo): ?><img src="<?= $s_logo ?>" class="school-logo"><?php endif; ?>
        <h3 class="fw-bold mb-0"><?= htmlspecialchars($s_name) ?></h3>
        <div class="opacity-75 small">Official Portal</div>
    </div>

    <div class="container px-3">
        
        <?php if(!empty($slides)): ?>
        <div id="mainCarousel" class="carousel slide hero-carousel" data-bs-ride="carousel">
            <div class="carousel-inner">
                <?php foreach($slides as $k => $s): ?>
                    <div class="carousel-item <?= $k==0?'active':'' ?>" data-bs-interval="5000">
                        <img src="<?= $s['image_path'] ?>" class="d-block w-100" alt="Slide">
                    </div>
                <?php endforeach; ?>
            </div>
            <button class="carousel-control-prev" type="button" data-bs-target="#mainCarousel" data-bs-slide="prev"><span class="carousel-control-prev-icon"></span></button>
            <button class="carousel-control-next" type="button" data-bs-target="#mainCarousel" data-bs-slide="next"><span class="carousel-control-next-icon"></span></button>
        </div>
        <?php else: ?>
            <div style="height: 40px;"></div> <?php endif; ?>

        <div class="row g-3 mb-5">
            <div class="col-4">
                <a href="public_calendar.php" class="icon-card">
                    <div class="icon-circle ic-cal"><i class="bi bi-calendar-event"></i></div>
                    <h6 class="fw-bold small mb-0">Calendar</h6>
                </a>
            </div>
            <div class="col-4">
                <a href="public_parent.php" class="icon-card">
                    <div class="icon-circle ic-par"><i class="bi bi-people"></i></div>
                    <h6 class="fw-bold small mb-0">Parents</h6>
                </a>
            </div>
            <div class="col-4">
                <a href="#" class="icon-card" onclick="alert('Info: <?= htmlspecialchars($s_phone) ?>\n<?= htmlspecialchars($s_addr) ?>')">
                    <div class="icon-circle ic-inf"><i class="bi bi-info-circle"></i></div>
                    <h6 class="fw-bold small mb-0">Info</h6>
                </a>
            </div>
        </div>

        <h5 class="fw-bold text-secondary mb-3 ps-1"><i class="bi bi-megaphone-fill me-2"></i>Latest News</h5>
        
        <?php if(empty($news)): ?>
            <div class="text-center py-5 text-muted bg-white rounded-4 shadow-sm">
                <i class="bi bi-newspaper display-4 opacity-25"></i>
                <p class="mt-2 small">No announcements yet.</p>
            </div>
        <?php else: ?>
            <?php foreach($news as $n): ?>
                <div class="news-card">
                    <div class="news-header">
                        <span class="fw-bold text-primary"><?= htmlspecialchars($n['title']) ?></span>
                        <span class="badge bg-light text-secondary border"><?= date('d M', strtotime($n['created_at'])) ?></span>
                    </div>
                    <div class="news-body">
                        <?= $n['content'] ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>

        <div class="simple-footer">
            <p class="mb-1 fw-bold text-dark"><?= htmlspecialchars($s_name) ?></p>
            <p class="mb-0"><?= htmlspecialchars($s_addr) ?></p>
            <div class="mt-3 opacity-50">&copy; <?= date('Y') ?> All Rights Reserved</div>
        </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>