<?php
if (isset($_POST['add_fee_structure'])) {
    $class = $_POST['class'] ?? '';
    $term = $_POST['term'] ?? '';
    $sessionVal = $_POST['session'] ?? '';
    $student_type = $_POST['student_type'] ?? '';
    $amount = $_POST['amount'] ?? 0;

    if ($class && $term && $sessionVal && $student_type && $amount > 0) {
        $stmt = $connection->prepare("INSERT INTO fee_structure (class, term, session, student_type, amount) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssd", $class, $term, $sessionVal, $student_type, $amount);
        if ($stmt->execute()) {
            $_SESSION['msg'] = "Fee structure added.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Error: " . $stmt->error;
            $_SESSION['type'] = "danger";
        }
    } else {
        $_SESSION['msg'] = "All fields are required.";
        $_SESSION['type'] = "danger";
    }
    header("Location: allocate_class_fee");
    exit;
}
?>
<div class="page-title">
    <h1><i class="fa fa-money"></i> Add Fee Structure</h1>
</div>

<div class="card">
    <div class="panel panel-primary">
        <div class="panel-heading">Define Fee for a Class/Term/Type</div>
        <div class="panel-body">
            <form method="POST">
                <div class="row">
                    <div class="col-md-3">
                        <input name="class" type="text" placeholder="Class (e.g., JSS1)" class="form-control" required>
                    </div>
                    <div class="col-md-2">
                        <select name="term" class="form-control" required>
                            <option disabled selected>Select Term</option>
                            <option>First Term</option>
                            <option>Second Term</option>
                            <option>Third Term</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select name="session" class="form-control" required>
                            <option disabled selected>Select Session</option>
                            <?php
                            $get = $connection->query("SELECT sessions FROM sessions");
                            while ($row = $get->fetch_assoc()) {
                                echo "<option>{$row['sessions']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select name="student_type" class="form-control" required>
                            <option disabled selected>Student Type</option>
                            <option>Boarding</option>
                            <option>Day</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <input type="number" step="0.01" name="amount" class="form-control" placeholder="₦ Amount" required>
                    </div>
                </div>
                <br>
                <button name="add_fee_structure" class="btn btn-primary">Add Fee Structure</button>
            </form>
        </div>
    </div>
</div>