<!-- Enthic Gender Report.php -->
<?php
include('session_validator.php');
include("includes/db.php");

$query = "SELECT c.class_name, c.section, e.group_name, s.gender, COUNT(s.student_id) as total
          FROM students s
          JOIN classes c ON s.class = c.Class_id
          JOIN std_ent_group se ON s.student_id = se.std_id
          JOIN ent_group e ON se.ethnic_group = e.id
          GROUP BY c.class_name, e.id, s.gender
          ORDER BY c.Class_id, e.id, s.gender";

$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
    $classes = array();
    $data = array();
    $genderTotals = array(); // Store totals for each gender
    $groupTotals = array(); // Store totals for each group

    while ($row = mysqli_fetch_assoc($result)) {
        $class = $row["class_name"];
        // $sectionName = $row["section"];
        $group = $row["group_name"];
        $gender = $row["gender"];
        $total = $row["total"];

        $data[$class][$group][$gender] = $total;

        // Update gender total
        if (!isset($genderTotals[$class][$gender])) {
            $genderTotals[$class][$gender] = 0;
        }
        $genderTotals[$class][$gender] += $total;

        // Update group total
        if (!isset($groupTotals[$group])) {
            $groupTotals[$group] = 0;
        }
        $groupTotals[$group] += $total;

        if (!in_array($class, $classes)) {
            $classes[] = $class;
        }
    }

    $allGroups = array(); // Array to store all possible combinations of group and gender

    // Populate $allGroups with all possible combinations
    foreach ($data as $classData) {
        foreach ($classData as $group => $genders) {
            foreach ($genders as $gender => $total) {
                $allGroups["$group $gender"] = true;
            }
        }
    }

    // Initialize arrays for gender and group totals
    $genderTotals['Total'] = array('Male' => 0, 'Female' => 0);
    $groupTotals['Total'] = 0;

    ?>


    <!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">
            <link rel="stylesheet" href="css/tablestyle.css" type="text/css">
        <script src="js/modernizr/modernizr.min.js"></script>

<style>
    .table-container {
    max-height: 500px; /* Set the maximum height as needed */
    overflow-y: auto;
}

#customers {
    font-family: Arial, Helvetica, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

#customers td, #customers th {
    border: 1px solid #ddd;
    padding: 8px;
}

.lower-margin {
    margin-bottom: 2px; /* Adjust the value as needed */
}

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

#customers tr:hover {
    background-color: #ddd;
}

#customers th {
    padding-top: 12px;
    padding-bottom: 12px;
    text-align: left;
    background-color: #d6f5ab;
    position: sticky;
    top: 0;
}

</style>


<script>
function printTable() {
    var printWindow = window.open('', '_blank');
    printWindow.document.write('<html><head><title>Mohanmaya Sec. School, Student Enthic Group Detail</title>');
    printWindow.document.write('<link rel="stylesheet" href="css/tablestyle.css" type="text/css">');
    printWindow.document.write('</head><body>');
    printWindow.document.write(document.getElementById('customers').outerHTML);
    printWindow.document.write('</body></html>');
    printWindow.document.close();

    printWindow.document.documentElement.style.zoom = '75%';

    // Use setTimeout to ensure styles are applied before printing
    setTimeout(function () {
        printWindow.print();
        printWindow.onafterprint = function () {
            printWindow.close();
        };
    }, 500);
}

    
    </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">Student Enthic Group Report</h2>
                            </div>
                        </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>Report</li>
                                    <li class="">Enthic Group Report</li>
                                </ul>
                            </div>
                        </div>
                        <!-- /.row -->
                    </div>

                    <div class="container-fluid">
                        <div class="row">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="panel">
                                        <div class="panel-heading">
                                            <div class="panel-title">
                                                <h5>Student Enthic Group Report</h5>
                                            </div>
                                        </div>
                                        <div class="panel-body">
         <button onclick="printTable()">Print Table</button>
            
                    <div class="panel-body">
                <!-- Display the table here -->
                <table id="customers" style="border-collapse: collapse; border: 1px solid #000000;">
                    <tr><td colspan='30'><span style='border:none'><b>Mohanmaya Secondary School</b> <br> Birtamode-02, Jhapa<br> Student Enthic Detail 2080<br></td></tr>
        <tr>
                    <tr>
                        <th>Class</th>
                        <?php
                        // Display headers for all possible combinations
                        foreach ($allGroups as $group => $exists) {
                            echo "<th><center>$group</center></th>";
                        }
                             echo "<th><center>Total (Female)</center></th>"; // Total column for Female
                        echo "<th><center>Total (Male)</center></th>"; // Total column for Male
                   
                        ?>
                    </tr>
                    <?php
                    
                    // Display data rows
                    foreach ($classes as $class) {
                        echo "<tr>";
                        echo "<td style='width:80px'>$class </td>";

                        foreach ($allGroups as $group => $exists) {
                            $parts = explode(" ", $group);
                            $groupKey = $parts[0];
                            $genderKey = $parts[1];

                            $total = isset($data[$class][$groupKey][$genderKey]) ? $data[$class][$groupKey][$genderKey] : 0;
                            echo "<td><center>$total</center></td>";
                        }

                        // Display row total for each gender
                          echo "<td><center>{$genderTotals[$class]['Female']}</center></td>";
                        echo "<td><center>{$genderTotals[$class]['Male']}</center></td>";
                      

                        echo "</tr>";
                    }

                    // Display column total for each group
                    // echo "<tr><td>Total</td>";
                    // foreach ($allGroups as $group => $exists) {
                    //     $parts = explode(" ", $group);
                    //     $groupKey = $parts[0];
                    //     $genderKey = $parts[1];
                    //     echo "<td><center>{$groupTotals[$group]}</center></td>";
                    // }
                    // echo "<td><center>{$genderTotals['Total']['Male']}</center></td>";
                    // echo "<td><center>{$genderTotals['Total']['Female']}</center></td>";
                    // echo "</tr>";
                    ?>
                </table>
<!-- /.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
} else {
    echo "No records found.";
}

mysqli_close($conn);
?>