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

// Fetch school details
$query = "SELECT * FROM schools LIMIT 1";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    $schoolDetails = $result->fetch_assoc();
    $logoPath = $schoolDetails['logo'];
} else {
    $schoolDetails = [];
}

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

    <script>
        $(document).ready(function () {
            $('#subjectsTable').DataTable();
        });

        // function printMarkSlip(markSlip) {
        //     var printContent = document.getElementById(markSlip).innerHTML;
        //     var originalContent = document.body.innerHTML;
        //     document.body.innerHTML = printContent;
        //     window.print();
        //     document.body.innerHTML = originalContent;
        // }

        function printTable() {
        var printWindow = window.open('', '_blank');
        printWindow.document.write('<html><head><title>markSlip</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'); ?>

        <!-- SIDEBARS & MAIN CONTENT WRAPPER -->
        <div class="content-wrapper">
            <div class="content-container">
                <!-- LEFT SIDEBAR -->
                <?php include('includes/leftbar.php'); ?>

                <div class="main-page">
                    <div class="container-fluid">
                        <!-- PAGE TITLE -->
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">Add Subject Combination</h2>
                            </div>
                        </div>

                        <!-- BREADCRUMBS -->
                        <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>Subjects</li>
                                    <li class="active">Add Subject Combination</li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <!-- SUBJECT COMBINATION TABLE -->
                    <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 Combination</h5>
                                        </div>
                                    </div>
                                    <div class="panel-body">
                                        <!-- Mark Slip Print Button -->
                                        <!-- <button onclick="printMarkSlip('markslip')">Print Mark Slip</button> -->
                                

                                <button type="submit" class="btn btn-success" onclick="printTable()">Print Table</button>
                                        <!-- School Information -->
                                        <div>
                                            <!-- Your School Information goes here... -->
                                            <center>
                                                <div class="container2">

                                                    <!-- School Logo and Details -->
                                                  <table id='customers' class='customers' style="font-size:-3%">
                                                        <thead style="border:none;">
                                                            <th style="border:none;"></th>
                                                            <th style="border:none;"></th>
                                                            <th style="border:none;"></th>
                                                            <th style="border:none;"></th>

                                                        <tbody style="border:none;">
                                                            <tr>
                                                                <td colspan="6" style="border:none"> <span style="margin-top:-28%;" ><?php echo "<left >IEMIS CODE: " . $schoolDetails['school_code'] . "</left>";?>
                                                            </span>
                                                        </td>
                                                    </tr>
                                                            <td colspan="2" style="border:none;">
                                                                <?php echo "<span><img src='$logoPath' alt='Logo' width='130' height='120'></span>"; ?>
                                                            </td>
                                                            <center>
                                                                <td align="center" style="padding: -9px; border:none;" colspan="6" >
                                                                    <?php
                                                                    echo "<h2 style='margin-bottom:-1%;'><b>" . $schoolDetails['school_name'] . "</b></h2>";
                                                                    echo "<h3 style='margin-bottom:-1%;'>" . $schoolDetails['address'] . "</h3>";
                                                                    echo "<h4 style='margin-bottom:-1%;'> ESTD: " . $schoolDetails['establishment_year'] . "</h4>";
                                                                    echo "Phone: " . $schoolDetails['phone'];
                                                                    ?>
                                                                </td>
                                                            </center>
                                                        </tbody>
                                         
                                                </div>
                                            </center>
                                        </div>

                                        <!-- Class Information -->
                                        <?php
                                        include("includes/db.php");

                                        // Check if class_id is provided in the query parameters
                                        if(isset($_GET['class_id'])) {
                                            $classId = $_GET['class_id'];

                                            // Fetch subject and teacher information for the selected class from the database
                                            // Assume you have a table named 'class_subject_mapping' that maps subjects to classes and a table named 'teachers' for teacher information
                                            $subjectQuery = "SELECT subjects.subject_name FROM class_subject_mapping
                                                            INNER JOIN subjects ON class_subject_mapping.subject_id = subjects.id
                                                            
                                                            WHERE class_subject_mapping.class_id = $classId";

                                            $subjectResult = $conn->query($subjectQuery);

                                            // Fetch students for the selected class from the database
                                            $studentsQuery = "SELECT regd_id, symbol_no, name FROM students WHERE class_id = $classId ORDER BY symbol_no";
                                            $studentsResult = $conn->query($studentsQuery);

                                            // Check if students were found
                                            if ($studentsResult === false) {
                                                die("Error fetching students: " . $conn->error);
                                            }

                                            if ($subjectResult->num_rows > 0) {
                                                $classQuery = "SELECT class_name, section FROM classes WHERE Class_id = $classId";
                                                $classResult = $conn->query($classQuery);

                                                if ($classResult->num_rows > 0) {
                                                    $classRow = $classResult->fetch_assoc();
                                                    $className1 = $classRow['class_name'];
                                                    $className2 = $classRow['section'];
                                                    $className = $className1 . "-" . $className2;
                                                            ?>
    
                        <tr> <th colspan="8" style="border:none;"><center><h4>प्रथम त्रैमासिक परीक्षा अभिभावक उपस्थिति  Class: <?=$className ?></h4></center></th> </tr>
                                    
                                        <!-- Combined Table -->
                                <!--<tr>-->
                                <!--                <th colspan="3"> <?php echo "Subject:..................................................." ?> </th>-->
                                                
                                <!--                <th colspan="5"><?php echo "Teacher:..............................................................................." ?></th>-->
                                <!--           </tr>-->
                                        <!-- Main Data Table -->
                             
                                       
                                                
                                            
                                                <tr>
                                                    <th>S.N.</th>
                                                    <th >Regd ID</th>
                                                     <!--<th>Symbol No</th> -->
                                                    <th style="height:20px">अभिभावकको नाम</th>
                                                    <th> <center>अभिभावकको नाम</th>
                                                    <th> <center> सम्पर्क नं.</center></th>
                                                    <th> <center> हस्ताक्षर</center></th>
                                                    <th> <center> मिति</center></th>
                                                       
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                // Loop to display rows for each student in the class
                                                $counter = 1;
                                                while ($studentRow = $studentsResult->fetch_assoc()) {
                                                    echo "<tr>";
                                                    echo "<td style='padding: 10px; height:50px'>{$counter}</td>";
                                                    echo "<td style='padding: 5px; font-size:10px;'>{$studentRow['regd_id']}</td>";
                                                    // echo "<td style='color:red; padding: 5px; '><b>{$studentRow['symbol_no']}</b></td>";
                                                    echo "<td style='padding: 5px;align: right; width:250px'>{$studentRow['name']}</td>";
                                                    echo "<td style='padding: 5px; width:250px '></td>"; // Empty marks field
                                                    echo "<td style='padding: 5px; width:150px '></td>"; // Empty marks field
                                                    echo "<td style='padding: 5px; '></td>"; // Empty marks field
                                                    echo "<td style='padding: 5px; '></td>"; // Empty marks field
                                              
                                                    echo "</tr>";
                                                    $counter++;
                                                }
                                                ?>
                                            </tbody>
                                            <tfoot>
                                                <th colspan="3"> <?php echo "Signature:............................................." ?> </th>
                                                <!-- <th style="width:200px"></th> -->
                                                <th colspan=""><?php echo "Date:" ?></th>
                                                <th colspan=""></th>
                                                <th colspan=""></th>
                                                <th colspan=""></th>
                                                <th colspan=""></th>
                                                
                                                <!--<tr> <td colspan="8" style="background=color:transparent">विषयगत अङ्क विषय शिक्षक आफैले<span style="color:red; font-size:bold">  Exam2081.mohanmaya.com  </span> मा गई "Enter Marks [Th]" मार्फत परिक्षा , कक्षा र विषयगत रुपमा Entry गरिदिनुहुन अनुरोध छ ।</td> </tr>-->
                                        
                                            </tfoot>
                                          </table>
                                        <?php
                                                } else {
                                                    echo "<p>No subject and teacher information found for the selected class.</p>";
                                                }
                                            } else {
                                                echo "<p>Please select a class to generate the blank mark slip.</p>";
                                            }
                                        }
                                        
                                        // Close the database connection
                                        $conn->close();
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Your JavaScript includes go here... -->
        <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>
