<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SRMS Admin Manage Subjects</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/prism/prism.css" media="screen">
    <link rel="stylesheet" href="css/select2/select2.min.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>
    <script src="js/jquery-3.6.4.min.js"></script>
    <script>
        $(document).ready(function () {
            // Function to toggle all checkboxes for a specific subject
            function toggleAllCheckboxes(subjectCode) {
                var checkboxes = $('input[name="' + subjectCode + '[]"]');
                checkboxes.prop('checked', !checkboxes.prop('checked'));
            }

            // Event handler for header checkboxes
            $('table').on('click', 'input[type="checkbox"].headerCheckbox', function () {
                var subjectCode = $(this).val();
                toggleAllCheckboxes(subjectCode);
            });

            // Event handler for "Select All" checkbox in the header
            $('table th input[type="checkbox"]').click(function () {
                var columnIndex = $(this).data('column-index');
                var checkboxesInColumn = $('input[name^="' + columnIndex + '_"]');
                checkboxesInColumn.prop('checked', $(this).prop('checked'));
            });

            // Event handler for student row checkboxes
            $('table').on('click', 'input[type="checkbox"].selectRowCheckbox', function () {
                var rowIndex = $(this).data('row-index');
                var checkboxesInRow = $('input[name^="subjects[' + rowIndex + ']"]');
                checkboxesInRow.prop('checked', $(this).prop('checked'));
            });

            // Form submission
            $('form').submit(function () {
                // Validate form if needed
            });
        });
    </script>
    <style>
        body {
            background-color: #91ced4;
        }

        body * {
            box-sizing: border-box;
        }

        .header {
            background-color: #327a81;
            color: white;
            font-size: 1.5em;
            padding: 1rem;
            text-align: center;
            text-transform: uppercase;
        }

        .table-users {
            border: 1px solid #327a81;
            border-radius: 10px;
            box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.1);
            max-width: calc(100% - 1em);
            overflow: hidden;
            width: 960em;
        }

        table {
            width: 100%;
            margin-left: 10px;
            border-collapse: collapse;
            table-layout: fixed;
        }

        table td,
        table th {
            color: #2b686e;
            padding: 8px;
            border: 1px solid #327a81;
            position: relative;
        }

        table td {
            text-align: left;
        }

        table td:last-child {
            font-size: 0.95em;
            line-height: 1.4;
            text-align: left;
        }

        table th {
            background-color: #daeff1;
            font-weight: 300;
            position: sticky;
            z-index: 500;
            top: 0;
        }

        table tr:nth-child(2n) {
            background-color: white;
        }

        table tr:nth-child(2n+1) {
            background-color: #edf7f8;
        }

        @media screen and (max-width: 960px) {
            table,
            tr,
            td {
                display: block;
            }

            td:first-child {
                position: absolute;
                top: 80%;
                transform: translateY(-50%);
                
            }

            td:not(:first-child) {
                clear: both;
                margin-left: 100px;
                padding: 4px 20px 4px 90px;
                position: relative;
                text-align: left;
            }

            td:not(:first-child):before {
                color: #91ced4;
                content: "";
                display: block;
                left: 0;
                position: absolute;
            }

            tr {
                padding: 10px 0;
                position: relative;
            }

            tr:first-child {
                display: none;
            }
        }

        @media screen and (max-width: 500px) {
            .header {
                background-color: transparent;
                color: white;
                font-size: 2em;
                font-weight: 700;
                padding: 0;
                text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.1);
            }

            td:first-child {
                background-color: #c8e7ea;
                border-bottom: 1px solid #91ced4;
                border-radius: 10px 10px 0 0;
                position: relative;
                top: 0;
                transform: translateY(0);
                width: 100%;
            }

            td:not(:first-child) {
                margin: 0;
                padding: 5px 1em;
                width: 100%;
            }

            td:not(:first-child):before {
                font-size: 0.8em;
                padding-top: 0.3em;
                position: relative;
            }

            td:last-child {
                padding-bottom: 1rem !important;
            }

            tr {
                background-color: white !important;
                border: 1px solid #6cbec6;
                border-radius: 10px;
                box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.1);
                margin: 0.5rem 0;
                padding: 0;
            }

            .table-users {
                border: none;
                box-shadow: none;
                overflow: visible;
            }
        }
    </style>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <!-- ========== TOP NAVBAR ========== -->
        <?php include('includes/topbar.php'); ?>
        <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
        <div class="content-wrapper">
            <div class="content-container">
                <!-- ========== LEFT SIDEBAR ========== -->
                <?php include('includes/leftbar.php'); ?>
                <!-- /.left-sidebar -->
                <div class="main-page">
                    <div class="container-fluid">
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">Add subject to each student</h2>
                            </div>
                        </div>
                        <div class="row breadcrumb-div">
                            <div class="col-md-6">
                                <ul class="breadcrumb">
                                    <li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                    <li> Subject </li>
                                    <li class="">Student-Subject </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="panel">
                                    <div class="panel-heading">
                                        <div class="panel-title">
                                            <h5>Add subject to each student</h5>
                                        </div>
                                    </div>
                                    <div class="panel-body">
                                        <?php

include("includes/db.php");

// Fetch available classes from the database
$classQuery = "SELECT * FROM classes";
$classResult = $conn->query($classQuery);

if ($classResult->num_rows > 0) {
    echo "<form method='post' action='assignsubtostd.php'>";
    echo " <div class='form-group'>";
    echo "<label for='class' >Select Class:</label>";
    echo "<select style='width:250px' name='class_id' id='class' class='form-control' required>";
    echo "<option value=''>Select Class</option>"; // Default option

    while ($row = $classResult->fetch_assoc()) {
        echo "<option value='{$row['Class_id']}'>{$row['class_name']} - {$row['section']}</option>";
    }

    echo "</select>";
    echo "<br><br>";
    echo "<input type='submit' class='btn btn-primary' value='Fetch Students'>";
    echo "</div>";
    echo "</form>";
} else {
    echo "No classes available.";
}

// Close the database connection
$conn->close();
?>
<!-- 
<div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="panel">
                                    <div class="panel-heading">
                                        <div class="panel-title">
                                            <h5>Select Class to Generate Blank Mark Slip</h5>
                                        </div>
                                    </div> -->
                                    <div class="main-wrapper">

                                          <script src="js/jquery.dataTables.min.js"></script>

    <?php
    // Include database connection
    include("includes/db.php");

    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['class_id']) && !empty($_POST['class_id'])) {
        $classId = $_POST['class_id'];

        // Fetch subjects for the selected class from class_subject_mapping table
        $subjectMappingQuery = "SELECT cs.subject_id, s.subject_code, s.subject_name
                                FROM class_subject_mapping cs
                                JOIN subjects s ON cs.subject_id = s.id
                                WHERE cs.class_id = $classId
                                ORDER BY s.subject_code ASC"; // Sort by subject code
        $subjectMappingResult = $conn->query($subjectMappingQuery);

        if ($subjectMappingResult->num_rows > 0) {
            ?>
            <form method="post" action="process_assign1.php" onsubmit="return validateForm();">
                <input type="hidden" name="class_id" value="<?php echo $classId; ?>"> <!-- Pass class_id to the next page -->

                <!-- Display students for the selected class -->
                <?php
                $studentQuery = "SELECT * FROM students WHERE Class_id = $classId";
                $studentResult = $conn->query($studentQuery);
                

         //Display table headers for subjects -->
                echo "<table>";
                                                echo "<tr>";
                                                echo "<th style='width: 100px;'>Student ID</th>";
                                                echo "<th style='width: 100px;'>Student Regd</th>";
                                                echo "<th style='width: 250px;'>Student Name</th>";

                                                // Display checkboxes for subjects
                                                $subjectCounter = 1;
                                                while ($subject = $subjectMappingResult->fetch_assoc()) {
                                                    $subjectId = $subject['subject_id'];
                                                    $subjectCode = $subject['subject_code'];
                                                    $subjectName = $subject['subject_name'];

                                                    echo "<th style='width:110px'><center>";
                                                    echo "<label for='select_all_$subjectCode'>$subjectName <br> [$subjectCode]</label>";
                                                    echo "<input type='hidden' name='subjectColumn[]' value='$subjectCounter'>";
                                                    echo "</center></th>";

                                                    $subjectCounter++;
                                                }
                                                echo "<th >Select All</th>"; // New column for Select All checkbox for each student
                                                
                                                echo "</tr>";

                                                // Display students and checkboxes for subjects
                                                $studentQuery = "SELECT * FROM students WHERE Class_id = $classId";
                                                $studentResult = $conn->query($studentQuery);

                                                while ($student = $studentResult->fetch_assoc()) {
                                                    $studentId = $student['student_id'];
                                                    $studentregd = $student['regd_id'];
                                                    $studentName = $student['name'];

                                                    echo "<tr>";
                                                    echo "<td>$studentId</td>";
                                                    echo "<td>$studentregd</td>";
                                                    echo "<td style='align:left'>$studentName</td>";

                                                    // Display checkboxes for subjects
                                                    $subjectMappingResult->data_seek(0); // Reset subjects result pointer
                                                    while ($subject = $subjectMappingResult->fetch_assoc()) {
                                                        $subjectCode = $subject['subject_code'];
                                                        echo "<td>";
                                                        echo "<input type='checkbox' name='subjects[$studentId][$subjectCode]' value='$subjectCode'> $subjectCode";
                                                        echo "</td>";
                                                    }

                                                    // New column for Select All checkbox for each student
                                                    echo "<td style='width:110px' class='select-all-cell'>";
                                                    echo "<label  for='select_row_$studentId' class='selectAllLabel'><input type='checkbox' class='selectRowCheckbox' id='select_row_$studentId' data-row-index='$studentId'><b style='color:blue; font-weight:bold;'>   Select All</b></label>";
                                                    echo "</td>";


                                                    echo "</tr>";
                                                }

                                                echo "</table>"; ?>
                <br>
                <button style='margin-left:20px' type='submit' class='btn btn-primary'>Assign Subjects</button>
            </form>
            <?php
        } else {
            echo "No subjects found in the database.";
        }
    } else {
        echo "Please select a class.";
    }

    // Close the database connection
    $conn->close();
    ?>



                                    </div>
                    <!--             </div>
                            </div>
                        </div>
                    </div> -->





                                    </div>
                                </div>
                            </div>
                            <!-- /.col-md-12 -->
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.content-container -->
        </div>
        <!-- /.content-wrapper -->
    </div>
    <!-- /.main-wrapper -->
    <script src="js/jquery/jquery-2.2.4.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <script src="js/pace/pace.min.js"></script>
    <script src="js/lobipanel/lobipanel.min.js"></script>
    <script src="js/iscroll/iscroll.js"></script>
    <script src="js/prism/prism.js"></script>
    <script src="js/select2/select2.min.js"></script>
    <script src="js/main.js"></script>
</body>

</html>
