<!-- Edit Modal Subject -->
<div id="Edit_modal-<?= $subjectId ?>" class="modal">
    <div class="modal-content" style="margin-top:50px;">
        <div class="modal-header text-success">Edit Subject 
            <i class="fa fa-plus-circle"></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="<?=$subject?>" 
                    >
                </div>
                
                <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 inClass FROM subject WHERE subName = ?";
                        $query = $pdo->prepare($sql);
                        $query->execute([$subject]);

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

                        // 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">
                    <button type="button" class="btn btn-danger" onclick="closeModal('Edit_modal-<?= $subjectId ?>')">Cancel <i class="fa fa-cancel"></i> &times;</button>
                    <!-- Add button -->
                    <button name="edit-btn" class="btn btn-success">Edit Subject <i class="fa fa-plus-circle"></i></button>
                </div>
                
            </form>
        </div>
        
    </div>
</div>
