<!-- Edit Modal Subject -->
<?php
if (isset($_GET['edit'])) {
    $edit   = $_GET['edit'];
    $varEdit = explode('*', $edit);
    $varSubj = $varEdit[0];
    $varStaff = $varEdit[1];
}else{
    header('Location:staff.php');
}
$subjectdeal = $varSubj;
?>
<div id="Edit_modal-2" class="modal">
    <div class="modal-content" style="margin-top:50px;">
        <div class="modal-header">Edit subject class 
            <i class="fa fa-edit"></i>
        </div>
        <div class="modal-body">
            <form action="" method="POST">
                <label>Select Subject</label>
                <div class="my-input">
                    <input type="text" 
                    name="subj_name" 
                    placeholder="EnterSubject"
                    value="<?=$subjectdeal?>" 
                    >
                </div>
                <input type="hidden" name="staffId" value="<?=$varStaff?>">
                <h6 class="mt-4 mb-3">Select Class</h6>
                <div class="box-overall">
                    <div class="class-box">
                        <?php
                        // Step 1: Get all classes already assigned to this subject
                        $sql = "SELECT class FROM teacher_assignments 
                            WHERE staffId = ? AND subject = ?";
                        $query = $pdo->prepare($sql);
                        $query->execute([$varStaff, $varSubj]);

                        $assignedClasses = [];
                        while ($minsRow = $query->fetch(PDO::FETCH_ASSOC)) {
                            $assignedClasses[] = $minsRow['class'];
                        }

                        // Step 2: Fetch all available classes
                        $fetchClass = "SELECT * FROM classes";
                        $classStmt = $pdo->prepare($fetchClass);
                        $classStmt->execute();

                        while ($classRow = $classStmt->fetch(PDO::FETCH_ASSOC)) {
                            $allClass = $classRow['className'];

                            // Step 3: Check if class is in the assigned array
                            $checkInput = in_array($allClass, $assignedClasses) ? 'checked' : '';
                            ?>
                            <label class="class-item">
                                <input type="checkbox" 
                                    name="subj_box[]" 
                                    value="<?=$allClass?>" 
                                    <?=$checkInput?>>
                                <span><?=$allClass?></span>
                            </label>
                        <?php } ?>
                    </div>
                </div>

                <div class="modal-footer mt-4">
                    <a href="staff.php" class="btn btn-danger btn-sm">Cancel 
                        <i class="fa fa-cancel"></i> &times;
                    </a>
                    <!-- Add button -->
                    <button name="edit-assign-btn" class="btn btn-success btn-sm">Save Changes 
                        <i class="fa fa-check-circle"></i>
                    </button>
                </div>
                
            </form>
        </div>
        
    </div>
</div>
