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

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

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: index.php?msg=Invalid class id");
    exit;
}

$class_id = (int)$_GET['id'];

$stmt = $pdo->prepare("SELECT * FROM classes WHERE id = ? LIMIT 1");
$stmt->execute([$class_id]);
$class = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$class) {
    header("Location: index.php?msg=Class not found");
    exit;
}

$page_title = 'Edit Class';
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>Edit Class</h1>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title">Update Class Details</h3>
                </div>

                <form action="<?php echo WEB_ROOT; ?>Classes/process.php?action=edit" method="post">
                    <input type="hidden" name="class_id" value="<?php echo (int)$class['id']; ?>">

                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 form-group">
                                <label>Class Name <span class="text-danger">*</span></label>
                                <input type="text" name="class_name" class="form-control" required
                                       value="<?php echo htmlspecialchars($class['class_name'] ?? ''); ?>">
                            </div>

                            <div class="col-md-6 form-group">
                                <label>Section</label>
                                <input type="text" name="section" class="form-control"
                                       value="<?php echo htmlspecialchars($class['section'] ?? ''); ?>">
                            </div>

                            <div class="col-md-6 form-group">
                                <label>House</label>
                                <input type="text" name="house" class="form-control"
                                       value="<?php echo htmlspecialchars($class['house'] ?? ''); ?>">
                            </div>
                        </div>
                    </div>

                    <div class="card-footer">
                        <button type="submit" class="btn btn-primary">Update Class</button>
                        <a href="<?php echo WEB_ROOT; ?>Classes/index.php" class="btn btn-secondary">Back</a>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>

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