 <?php
include('session_validator.php');

?>
<!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 Generate Admit Card</title>
    <!-- Include necessary stylesheets -->
    <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/admitcard.css" media="screen">
    <style>
 
        /* Additional styling for the "Print All Admit Cards" button */
        .print-btn {
            position: fixed;
            bottom: 20px;
            right: 20px;
        }

        
    </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">
                <?php include('includes/leftbar.php'); ?>
                <div class="main-page">
                    <div class="container-fluid">
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">Generate Admit Card</h2>
                            </div>
                        </div>
                        <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="">Admit Card</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <section class="section">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="panel">
                                        <div class="panel-heading">
                                            <div class="panel-title">
                                                <h5>Generate Admit Card</h5>
                                            </div>
                                        </div>
                                        <div class="panel-body">
                                            <!-- Updated form to use POST method and open in a new tab -->
                                            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" target="_blank">

<div class="form-row">
    <div class="form-group col-md-4">
        <label for="exam_type">Select Exam Type:</label>
            <select id="exam_type" name="exam_type" class="form-control" required>
                <option value="">Select Exam Type</option>
                    <?php
                        // Include database connection
                        include('includes/db.php');
                        // Fetch exam types from the database
                        $fetchExamTypesQuery = "SELECT id, exam_type FROM examination_type";
                        $examTypesResult = $conn->query($fetchExamTypesQuery);

                        if ($examTypesResult->num_rows > 0) {
                            while ($row = $examTypesResult->fetch_assoc()) {
                                echo '<option value="' . $row['id'] . '">' . $row['exam_type'] . '</option>';
                                                        }
                     } 
                    ?>
            </select>
    </div>
        
    <div class="form-group col-md-3">
                                                    <label for="class_id">Select Class:</label>
                                                    <select id="class_id" name="class_id" class="form-control" required>
                                                        <option value="">Select Class</option>
                                                        <?php
                                                        // Fetch classes from the database
                                                        $fetchClassesQuery = "SELECT class_id, class_name, section FROM classes";
                                                        $classesResult = $conn->query($fetchClassesQuery);

                                                        if ($classesResult->num_rows > 0) {
                                                            while ($row = $classesResult->fetch_assoc()) {
                                                                echo '<option value="' . $row['class_id'] . '">' . $row['class_name'] . '-' . $row['section'] . '</option>';
                                                            }
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                                <button type="submit" name="generate_admit_card" class="btn btn-primary">Generate Admit Card</button>
                                            </div>
                                            </form>

                                            <?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['generate_admit_card'])) {
// Get selected exam type and class
$examTypeId = $_POST['exam_type'];
$classId = $_POST['class_id'];

// Fetch class name based on the selected class ID
$fetchClassNameQuery = "SELECT class_name, section FROM classes WHERE class_id = $classId";
$classNameResult = $conn->query($fetchClassNameQuery);

if ($classNameResult->num_rows > 0) {
    $classData = $classNameResult->fetch_assoc();
    $className = $classData['class_name'] . '-' . $classData['section'];
} else {
    // Handle the case where class name is not found (you can set a default value or show an error)
    $className = "Class Not Found";
}

// Fetch school information (assuming there is only one school)
$fetchSchoolInfoQuery = "SELECT * FROM schools LIMIT 1";
$schoolInfoResult = $conn->query($fetchSchoolInfoQuery);
$schoolInfo = $schoolInfoResult->fetch_assoc();

// Fetch principal name
$principalName = "SHER BAHADUR SHRESTHA"; // You mentioned it's a static name

// Fetch exam type and routine based on the selected options
$fetchExamTypeQuery = "SELECT * FROM examination_type WHERE id = $examTypeId";
$examTypeResult = $conn->query($fetchExamTypeQuery);
$examType = $examTypeResult->fetch_assoc();

// Fetch students in the selected class
$fetchStudentsQuery = "SELECT * FROM students WHERE Class_id = $classId";
$studentsResult = $conn->query($fetchStudentsQuery);

// Display admit card details for each student
while ($student = $studentsResult->fetch_assoc()) {
?>


                                                    <table class="admit-card-table">
                                                        <tr>
                                                            <td class="" style="width: 50px; border:none" rowspan="2">
                                                                <!-- Add your school logo here -->
                                                                <img src="uploads/logo.png" alt="School Logo" style="height:80px">
                                                            </td>
                                                            <td colspan="2" style="width:600px; border:none;" class="school-details-cell">
                                                                <center><span style="font-size:32px;border:none; font-weight:bold; "><?php echo $schoolInfo['school_name']; ?></span></center>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="2" style="border:none; font-size: 20px">
                                                                <center>
                                                                    <?php echo $schoolInfo['address']; ?> <br>
                                                                    Estd: <?php echo $schoolInfo['establishment_year']; ?> B.S.
                                                                </center>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="3" style="border:none;" class="school-info-cell">
                                                                <center> <u><b><span style="font-size:18px"><?php echo $examType['exam_type'].'-'.$examType['exm_year']?>
                                                                            ADMIT CARD </span></b></u></center>
<?php if (isset($student['symbol_no'])) : ?>

<div class="student-card">
    <div style="display: flex; justify-content: space-between;">

        <table style="border: none; ">
            <tr>
                <td style="color: red; font-weight: bold; font-size: 18px; border: none;padding:none;">
                    <span>Symbol Number:</span> <?php echo $student['symbol_no']; ?>
                </td>
                <td style="border: none;padding:none;"></td>
                <td style="border: none;padding:none;"></td>
            </tr>
            <tr style="padding:none;">
                <td style="border: none;">
                    <?php if (isset($student['name'])) : ?>
                        <strong>Name:</strong> <?php echo $student['name']; ?>
                    <?php endif; ?>
                </td>
<td style="border: none;">
    <?php if (!empty($student['registration_no'])) : ?>
        <strong><span style="margin-left:2em">Registration No:</strong> <?php echo $student['registration_no']; ?></span>
    <?php endif; ?>
</td>



                <td style="border: none;">
                    
                </td>
            </tr>
            <tr>
                <td style="border: none;">
                    <?php if (isset($student['Class_id'])) : ?>
                        <strong>Class:</strong> <?php echo $className; ?>
                    <?php endif; ?>
                </td>
                <td style="border: none;">
                    <?php if (isset($student['dob'])) : ?>
                        <span style="margin-left:2em"><strong>DOB:</strong> <?php echo $student['dob']; ?> </span>
                    <?php endif; ?> 
                    
                    <?php if (isset($student['regd_id'])) : ?>
                        <span style="margin-left:2em"><strong>IEMIS No:</strong> <?php echo $student['regd_id']; ?> </span>
                    <?php endif; ?>
                </td>
                <td style="border: none;"></td>
            </tr>
        </table>






            <?php
            // Define $photoPath even if conditions are not met
            $photoPath = isset($student['photo']) ? "uploads/" . $student['photo'] : '';
            ?>
            <div style="margin-top:-3%">
                <?php if (!empty($photoPath) && file_exists($photoPath)) : ?>
                    <img src="<?php echo $photoPath; ?>" alt="Student Photo" style="max-width: 100px; max-height: 120px; margin-left: 10px;margin-left: 20px;">
                <?php else : ?>
                    <p>photo </p>
                <?php endif; ?>
          </div>

    <div>
    </div>
</div>        
        
        
        
        <table class="routine-table">
                                                                    <tr>
                                                                        <td colspan="4"><b> EXAM SCHEDULE </b></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <th><center>Subject Code</center></th>
                                                                        <th><center>Subject</center></th>
                                                                        <th><center>Date</center></th>
                                                                        <th><center>Remarks</center></th>
                                                                    </tr>
                                                                    <?php
                                                                    // Fetch routine for the student based on class and exam type (adjust query based on your actual schema)
                                                                    $fetchRoutineQuery = "SELECT exam_date, subject_code, subject_name FROM examination_routine 
                                                                    INNER JOIN subjects ON examination_routine.subject_id = subjects.id
                                                                    WHERE class_id = $classId AND exam_type = $examTypeId
                                                                    ORDER BY exam_date";
;

                                                                    $routineResult = $conn->query($fetchRoutineQuery);

                                                                    while ($routine = $routineResult->fetch_assoc()) :
                                                                    ?>
                                                                        <tr>
                                                                            <td><center></center><?php echo $routine['subject_code']; ?></center></td>
                                                                            <td>
                                                                                <left><?php echo $routine['subject_name']; ?></left>
                                                                            </td>
                                                                            <td><center><?php echo $routine['exam_date']; ?></center></td>
                                                                            <td></td>
                                                                        </tr>
                                                                    <?php endwhile; ?>
                                                                </table>
    </div>
<?php else : ?>
    <p>No photo or symbol number available</p>
<?php endif; ?>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="4" style="border:none;"></td>
                                                        </tr>
                                                        <tr>
                                                            <td style="border:none;"></td>
                                                            <td style="border:none;"></td>
                                                            
                                                            
                                                            <td colspan="2" style="text-align: center; border-top:dotted; font-size: 15px; border: none; ">
                                                               <center> <img style="margin-left:-70%" src='uploads/sign.png' alt='Logo' width='40' height='30'></center>
                                                                <!--<br><br> <!-- Add some space for the signature -->
                                                                <div style="border-top: 1px dotted #000; padding-top: 5px; margin-left:-70%">
                                                                <strong><span ><?php echo $principalName; ?></strong> </div>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="border:none;"></td>
                                                            <td style="border:none;"></td>
                                                            <td colspan="2" style="text-align: center; border-top:dotted; font-size: 15px; border: none; ">
                                                                <span style="margin-left:-70%"> Head Teacher</span>
                                                                </strong>
                                                            </td>
                                                        </tr>
                                                    </table>
                                             
                                            <?php
                                                } // End of while loop for students
                                            } // End of if
                                            ?>
                                            <!-- 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/waypoint/waypoints.min.js"></script>
                                            <script src="js/counterUp/jquery.counterup.min.js"></script>
                                            <script src="js/toastr/toastr.min.js"></script>
                                            <script src="js/icheck/icheck.min.js"></script>
                                            <script src="js/main.js"></script>
                                            <script src="js/production-chart.js"></script>
                                            <script src="js/traffic-chart.js"></script>
                                            <script src="js/task-list.js"></script>
                                           


                                           <script>
    function printAllAdmitCards() {
        // Get all the tables with class 'admit-card-table'
        var tablesToPrint = document.querySelectorAll('.admit-card-table');

        // Open a new window for printing
        var newTab = window.open('', '_blank');

        // Start building the HTML content
        var newTabContent = '<html><head>';
        // Include necessary stylesheets
        newTabContent += '<link rel="stylesheet" href="css/main.css" media="screen">';
        newTabContent += '<link rel="stylesheet" href="css/admitcard.css" media="screen">';

        // Add other necessary stylesheets
        // Add inline styles for additional styling
        newTabContent += '<style>';
        newTabContent += 'body { margin: 2px; }';
        newTabContent += '@media print { .admit-card-table { page-break-after: always; } }'; // Insert page break after each admit card
        newTabContent += '.admit-card-table { width: 100%; border-collapse: collapse; margin-bottom: 3px; border: 4px solid #000000;}';
        newTabContent += '.student-card p { border: none; margin-bottom: -2px;}'; // Apply styles directly to <p> elements within .student-card
        newTabContent += '.admit-card-table td { border: 1px solid #ccc; }';
        newTabContent += '.logo-cell, .school-details-cell, .admit-card-heading-cell, .student-image-cell, .routine-cell { width: 30%; margin-bottom: 3px; }';
        newTabContent += '.routine-table { border: 1px solid #ccc; width: 100%; border-collapse: collapse; }';
        newTabContent += '</style>';

        newTabContent += '<title>Admit Cards</title></head><body>';

        // Add the content of each table to the new window
        tablesToPrint.forEach(function (table) {
            newTabContent += table.outerHTML;
        });

        // Close the HTML content
        newTabContent += '</body></html>';

        // Write the HTML content to the new window
        newTab.document.write(newTabContent);
        newTab.document.close();

        // Add an event listener to close the tab when the print preview is closed
        newTab.addEventListener('beforeunload', function () {
            newTab.close();
        });

        // Print the new window
        newTab.print();
    }
</script>


                                            <!-- Print All Admit Cards Button -->
                                            <button class="btn btn-primary print-btn" onclick="printAllAdmitCards()">Print All Admit Cards</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>

</body>

</html>
