<?php
include('session_validator.php');
include("includes/db.php");

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Check if class_id is set in the URL
    if (isset($_GET['class_id']) && !empty($_GET['class_id'])) {
        $classId = $_GET['class_id'];

        // Fetch class data for the selected class
        $classQuery = "SELECT * FROM classes WHERE Class_id = $classId";
        $classResult = $conn->query($classQuery);

        if ($classResult->num_rows > 0) {
            $classData = $classResult->fetch_assoc();
            $selectedClass = "{$classData['class_name']} - {$classData['section']}";
        } else {
            $selectedClass = 'Class not found';
        }
    } else {
        $selectedClass = 'No class selected';
    }
}

?>

<!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>
        <link rel="stylesheet" href="styles.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 800px;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
        }

        .selected-class {
            text-align: center;
            margin-bottom: 20px;
            font-size: 18px;
            color: #555;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #ddd;
        }
    </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">Select Class to Generate Blank Mark Slip</h2>
                                
                                </div>
                                
                                <!-- /.col-md-6 text-right -->
                            </div>
                            <!-- /.row -->
                            <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> Marks </li>
                                        <li class="">Blank Mark Slip</li>
                                    </ul>
                                </div>
                             
                            </div>
                            <!-- /.row -->
                        </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>Select Class to Generate Blank Mark Slip</h5>
                                                </div>
                                            </div>
                                            <div class="panel-body">
    

        <h1>Assigned Subjects Report</h1>

        <div class="selected-class"><strong>Selected Class:</strong> <?= $selectedClass ?></div>

        <form action="stdsubjectreport.php" method="get">
            <label for="class_id">Select Class:</label>
            <select name="class_id" id="class_id" required>
                <?php
                    // Fetch class data from the "classes" table
                    $classQuery = "SELECT * FROM classes";
                    $classResult = $conn->query($classQuery);

                    // Check if there are classes available
                    if ($classResult->num_rows > 0) {
                        while ($row = $classResult->fetch_assoc()) {
                            // Output options for each class
                            echo "<option value='{$row['Class_id']}'>{$row['class_name']} - {$row['section']}</option>";
                        }
                    } else {
                        // Output a default option if no classes are available
                        echo "<option value='' disabled>No classes available</option>";
                    }
                ?>
            </select>

            <button class="btn btn-primary" type="submit">View Report</button>
        </form>

        <?php
        // If class_id is set, fetch and display the student report
        if (isset($classId) && !empty($classId)) {
            $studentQuery = "SELECT * FROM students WHERE Class_id = $classId ORDER BY name ASC";
            $studentResult = $conn->query($studentQuery);

            if ($studentResult->num_rows > 0) {
                ?>
                <table>
                    <thead>
                        <tr>
                            <th>SN</th>
                            <th>Name</th>
                            <th>Regd No</th>
                            <th>Sub1</th>
                            <th>Sub2</th>
                            <th>Sub3</th>
                            <th>Sub4</th>
                            <th>Sub5</th>
                            <th>Sub6</th>
                            <th>Sub7</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $sn = 1;
                        while ($student = $studentResult->fetch_assoc()) {
                            $studentId = $student['student_id'];
                            $studentName = $student['name'];
                            $regdNo = $student['regd_id'];

                            // Fetch assigned subjects for the student
                            $assignedSubjectsQuery = "SELECT * FROM studentsub WHERE student_id = $studentId";
                            $assignedSubjectsResult = $conn->query($assignedSubjectsQuery);

                            // Initialize an array to store subjects
                            $subjects = array_fill(1, 7, '');

                            while ($row = $assignedSubjectsResult->fetch_assoc()) {
                                // Assign subjects to the array
                                $subjects[1] = $row['sub1'];
                                $subjects[2] = $row['sub2'];
                                $subjects[3] = $row['sub3'];
                                $subjects[4] = $row['sub4'];
                                $subjects[5] = $row['sub5'];
                                $subjects[6] = $row['sub6'];
                                $subjects[7] = $row['sub7'];
                            }

                            echo "<tr>";
                            echo "<td>$sn</td>";
                            echo "<td>$studentName</td>";
                            echo "<td>$regdNo</td>";

                            // Display assigned subjects (sub1 to sub6)
                            for ($i = 1; $i <= 7; $i++) {
                                echo "<td>{$subjects[$i]}</td>";
                            }

                            echo "</tr>";
                            $sn++;
                        }
                        ?>
                    </tbody>
                </table>
                <?php
            } else {
                echo "<p>No students found for the selected class.</p>";
            }
        }
        ?>

</div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.col-md-12 -->
                                </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>
<?php
$conn->close();
?>
