<?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/print-style.css" type="text/css" media="print" />
    <link rel="stylesheet" href="css/tablestyle.css" type="text/css" />

    <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'); ?>

        <!-- 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">Generate Mark Slip</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>Exam</li>
                                    <li class="active">Generate Mark Slip</li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <!-- COMBINED 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>Mark Slip</h5>
                                        </div>
                                    </div>
                                    <div class="panel-body">
                                        <button type="submit" class="btn btn-success" onclick="printTables()">Print Tables</button>
                                                <!-- Class Information -->
                                                <?php
                                                include("includes/db.php");

                                                // Check if exam_id, class_id, and subject_id are provided in the query parameters
                                                if (isset($_GET['exam_id1']) && isset($_GET['class_id']) && isset($_GET['subject_id'])) {
                                                    $examId = $_GET['exam_id1'];
                                                    $classId = $_GET['class_id'];
                                                    $subjectId = $_GET['subject_id'];

$examquery = "SELECT * FROM examination_type WHERE id = $examId";
        $examresult = $conn->query($examquery);
        $examDetails = $examresult->fetch_assoc();
        $examtype = $examDetails['exam_type'];
        $examyear = $examDetails['exm_year'];
        ?>
                                        <!-- School and Class Information -->
                                        <center>
                                            <div class="container2">
                                                <!-- School Logo and Details -->
                                                <table id='subjectCombinationTable' class='customers' style="font-size:-2%">
                                                    <thead>
                                                        <tr>
                                                            <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 "<b>" . $schoolDetails['school_name'] . "</b>";
                                                        ?>
                                                        <br>
                                                        <?php
                                                        echo $schoolDetails['address'] ;
                                                            ?>
                                                        <br>
                                                          <?php 
                                                          echo "ESTD: " . $schoolDetails['establishment_year'] 
                                                                       ?> <br> 

                                                                       <?php
                                                                        echo "Phone: " . $schoolDetails['phone']; 
                                                                        ?> <br>
                                                                        <?php echo  $examtype . " - " . $examyear
                                                                        ?>

                                                                    </td>
                                                                </center>
                                                            </tbody>
                                                        </tr>
                                                    </thead>
                                                    <tbody></tbody>
                                                </table>
<?php


    $subjectQuery = "SELECT * FROM  subjects WHERE id = $subjectId";

                                                    $subjectResult = $conn->query($subjectQuery);
                                                    if ($subjectResult->num_rows > 0) {
                                                        $subjectrow = $subjectResult->fetch_assoc();
                                                        $subjectName = $subjectrow['subject_name'];


$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;



                                                    // Fetch and display marks based on exam type
        $marksQuery = "SELECT students.name, students.symbol_no, students.regd_id, int_marks.internal_marks
                                                                   FROM int_marks
                INNER JOIN students ON int_marks.student_id = students.student_id
                                                                   WHERE int_marks.exam_type = '$examId'
                                                                   AND int_marks.class_id = '$classId'
                                                                   AND int_marks.subject_id = '$subjectId' ORDER by students.symbol_no ";
                                    $marksResult = $conn->query($marksQuery);

                            if ($marksResult && $marksResult->num_rows > 0) {
           echo "<table id='customers' class='customers'>
        <thead>
            <tr>
         <th colspan='2'>Class: {$className}</th>
         <th colspan='2'>Subject : {$subjectName} </th>
         <th colspan='3'>Teacher:..........................................</th>
            </tr>
            <tr>
                    <th>S.N.</th>
                    <th>Regd ID</th>
                    <th>Symbol No</th>
               
                    <th style='padding: -3px'>Student Name</th>
                    <th style='padding: -3px'> <center>Marks Obt.<br> [TH]</center></th>
                    <th style='padding: -3px'> <center>Marks Obt.<br> [PR]</center></th>
                     <th style='width:80px'> Total </th>
            </tr>
        </thead>
            <tbody style='border: 1px solid #000; border: collaspe;'>";

                                                        $counter = 1;
                                                        while ($row = $marksResult->fetch_assoc()) {
    $studentName = $row['name'];
    $symbol_no = $row['symbol_no'];
    $iemis = $row['regd_id'];
    $internal = $row['internal_marks'];
    
  
    $internal = isset($internal) ? $internal   : '' ;

    echo "<tr>";
    echo "<td style='padding: 8px; border: 1px solid #000; border-collapse: collapse; height:10px'>{$counter}</td>";
    echo "<td style='padding: 5px; border: 1px solid #000;border-collapse: collapse; height:10px'><b>$iemis</b></td>";
    echo "<td style='color:red; padding: 5px; border: 1px solid #000; border-collapse: collapse;'> $symbol_no</td>";
    echo "<td style='padding: 5px; width:500px; border: 1px solid #000; border-collapse: collapse; align: right'>$studentName</td>";

    echo "<td style='padding: 5px;border: 1px solid #000; border-collapse: collapse; '> </td>"; // Empty marks field
    echo "<td style='padding: 5px;border: 1px solid #000; border-collapse: collapse; '><center> $internal</center></td>"; // Empty marks field
    echo "<td style='padding: 5px;border: 1px solid #000; border-collapse: collapse; '> </td>";
    echo "</tr>";
    $counter++;
}

 }
                                                                    }
                                                                    ?>                            
                                                                </tbody>
                                                                <tfoot>
                                                                    <tr>
                                                                        <th colspan="4"> <?php echo "Signature:..............................." ?> </th>
                                                                        <!--<th colspan=""><?php echo "Date:" ?></th>-->
                                                                        <th colspan="3"><?php echo "2080/....../......." ?></th>
                                                                        <!--<th colspan=""><?php echo "2080/.../...." ?></th>-->
                                                                        <!--<th colspan=""><?php echo "2080/..../...." ?></th>-->
                                                                        <!--<th colspan=""><?php echo "2080/..../...." ?></th>-->
                                                                    </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>
                                        </center>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Include necessary scripts -->
        <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="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
        <script src="js/main.js"></script>
        <script>
    $(document).ready(function () {
        // Initialize DataTable for the combined table
        $('.customers').DataTable();
    });

    function printTables() {
        var printWindow = window.open('', '_blank');
        printWindow.document.write('<html><head><title>Tables</title>');
        printWindow.document.write('<link rel="stylesheet" href="css/tablestyle.css" type="text/css" />');
        printWindow.document.write('<link rel="stylesheet" href="css/tablestyle.css" type="text/css" media="print" />');
        printWindow.document.write('</head><body>');
        
        printWindow.document.write(document.getElementById('subjectCombinationTable').outerHTML);
        printWindow.document.write(document.getElementById('customers').outerHTML);
        printWindow.document.write('</body></html>');
        printWindow.document.close();

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

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

    </body>

</html>
