<?php
require_once __DIR__ . '/../library/auth.php';

$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'teacher']);




// ✅ FIX: school_id filter
$stmt = $pdo->prepare("SELECT * FROM classes WHERE school_id = ? ORDER BY id DESC");
$stmt->execute([$school_id]);
$classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
$page_title = 'Classes';
require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Class List</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="<?php echo WEB_ROOT; ?>Classes/add.php" class="btn btn-primary">
                        Add Class
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">

            <?php if (isset($_GET['msg'])): ?>
                <div class="alert alert-info">
                    <?php echo htmlspecialchars($_GET['msg']); ?>
                </div>
            <?php endif; ?>

            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Manage Classes</h3>
                </div>

                <div class="card-body table-responsive p-0">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Class Name</th>
                                <th>Section</th>
                                <th>House</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($classes)): ?>
                                <?php $i = 1; foreach ($classes as $class): ?>
                                    <tr>
                                        <td><?php echo $i++; ?></td>
                                        <td><?php echo htmlspecialchars($class['class_name'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($class['section'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($class['house'] ?? ''); ?></td>
                                        <td>
                                            <a href="<?php echo WEB_ROOT; ?>Classes/edit.php?id=<?php echo (int)$class['id']; ?>" class="btn btn-sm btn-warning">Edit</a>
                                            <a href="<?php echo WEB_ROOT; ?>Classes/process.php?action=delete&id=<?php echo (int)$class['id']; ?>"
                                               class="btn btn-sm btn-danger"
                                               onclick="return confirm('Delete this class?')">Delete</a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="5" class="text-center text-muted">No classes found.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>
    </section>
</div>

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