<!-- Enthic Group Report.php -->
<?php
include('session_validator.php');

include("includes/db.php");

// Initialize the report array
$report = array();

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

if ($classResult->num_rows > 0) {
    while ($class = $classResult->fetch_assoc()) {
        $classId = $class['Class_id'];
        $className = $class['class_name'];
        $sectionName = $class['section'];

        // Fetch ethnic group data for each class and section from std_ent_group table
        $ethnicGroupQuery = "SELECT e.classid, c.class_name, c.section, e.ethnic_group, COUNT(*) as count
                            FROM students s
                            LEFT JOIN std_ent_group e ON s.student_id = e.std_id
                            LEFT JOIN classes c ON s.class = c.Class_id
                            WHERE s.class = $classId
                            GROUP BY e.classid, e.ethnic_group, s.class";

        $ethnicGroupResult = $conn->query($ethnicGroupQuery);

        // Store the class, section, and ethnic group count data in the report array
        while ($row = $ethnicGroupResult->fetch_assoc()) {
            $classid = $row['classid'];
            $ethnicGroup = $row['ethnic_group'];
            $count = $row['count'];

            // Store the count of each ethnic group class-wise
            if (!isset($report[$className][$sectionName][$classid])) {
                $report[$className][$sectionName][$classid] = array();
            }

            $report[$className][$sectionName][$classid][$ethnicGroup] = $count;
        }
    }

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

<!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>
    </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">

        <table id="subjectsTable" class="display">
            <thead style="font-size: 120%;">
                <tr>
                    <th style="width: 100px;">Class</th>
                    <th style="width: 100px;">Section</th>
                    <!-- Display columns for each ethnic group -->
                    <?php
                    include("includes/db.php");
                    // Fetch available ethnic groups
                    $ethnicGroupQuery = "SELECT * FROM ent_group";
                    $ethnicGroupResult = $conn->query($ethnicGroupQuery);

                    if ($ethnicGroupResult->num_rows > 0) {
                        while ($ethnicGroup = $ethnicGroupResult->fetch_assoc()) {
                            $groupName = $ethnicGroup['group_name'];
                            echo "<th style='width:100px'><center>$groupName</center></th>";
                        }
                    }
                    ?>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody style="font-size: 125%;">
                <?php
                // Loop through the report array to display data in the table
                foreach ($report as $className => $sections) {
                    foreach ($sections as $sectionName => $classData) {
                        foreach ($classData as $classid => $ethnicGroups) {
                            echo "<tr>";
                            echo "<td>{$className}</td>";
                            echo "<td>{$sectionName}</td>";

                            $totalStudents = 0;

                            // Display counts for each ethnic group
                            foreach ($ethnicGroupResult as $ethnicGroup) {
                                $groupName = $ethnicGroup['id'];
                                echo "<td><center>" . (isset($ethnicGroups[$groupName]) ? $ethnicGroups[$groupName] : 0) . "</center></td>";
                                $totalStudents += isset($ethnicGroups[$groupName]) ? $ethnicGroups[$groupName] : 0;
                            }

                            // Display total students in the class and section
                            echo "<td>{$totalStudents}</td>";
                            echo "</tr>";
                        }
                    }
                }
                ?>
            </tbody>
        </table>
    </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>