<?php
/* ---------------------------------------------------------
   SRMS – Dynamic Evaluation Sheet (Fixed with NEB Format)
   --------------------------------------------------------- */
include 'session_validator.php';
include 'includes/db.php';

ini_set('display_errors', 1);
error_reporting(E_ALL);

/* ---------- 0. SCHOOL INFO ---------- */
$school = $conn->query("SELECT * FROM schools LIMIT 1")->fetch_assoc() ?? [];
$logo         = $school['logo'] ?? '';
$schoolCode   = $school['neb_code'] ?? '';
$schoolName   = $school['school_name'] ?? '';

/* ---------- A. CLASS LIST FOR DROPDOWN ---------- */
$classes = [];
$res = $conn->query("
    SELECT c.class_id, c.class, cs.section_id, s.section_name 
    FROM classes c 
    LEFT JOIN class_section cs ON c.class_id = cs.class_id 
    LEFT JOIN sections s ON cs.section_id = s.section_id 
    WHERE c.status = 1 
    ORDER BY c.class_numeric, s.section_name
");
if ($res) {
    while($row = $res->fetch_assoc()) {
        $classes[] = $row;
    }
}

/* ---------- 1. INPUTS (GET) ---------- */
$classId     = isset($_GET['class_id'])     ? (int)$_GET['class_id'] : 0;
$sectionId   = isset($_GET['section_id'])   ? (int)$_GET['section_id'] : 0;
$subjectCode = isset($_GET['subject_code']) ? trim($_GET['subject_code']) : '';
$sheet       = (isset($_GET['sheet']) && $_GET['sheet']==='internal') ? 'internal' : 'theory';

/* ---------- 2. SUBJECT LIST FOR THIS CLASS ---------- */
$subjects = [];
if ($classId) {
    $stmt = $conn->prepare("
        SELECT DISTINCT s.subject_code, s.subject_name 
        FROM class_subject_mapping csm 
        JOIN subjects s ON s.id = csm.subject_id 
        WHERE csm.class_id = ? 
        ORDER BY s.subject_name
    ");
    $stmt->bind_param("i", $classId);
    $stmt->execute();
    $subjects = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}

/* ---------- 3. SUBJECT DETAILS ---------- */
$subjectName=''; $internalFM=0; $theoryFM=0;
if ($subjectCode) {
    $stmt = $conn->prepare("
        SELECT subject_name, internal_full_marks, theory_full_marks 
        FROM subjects 
        WHERE subject_code = ? LIMIT 1
    ");
    $stmt->bind_param("s", $subjectCode);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc() ?? [];
    $subjectName = $row['subject_name'] ?? '';
    $internalFM  = (int)($row['internal_full_marks'] ?? 0);
    $theoryFM    = (int)($row['theory_full_marks']   ?? 0);
}

/* ---------- 4. INTERNAL COLUMNS FROM DB OR DEFAULT ---------- */
$cols = [];
if ($sheet === 'internal' && $subjectCode) {
    $stmt = $conn->prepare("
        SELECT particulars, marks 
        FROM internal_evaluation 
        WHERE subject_code = ? 
        ORDER BY display_seq ASC
    ");
    $stmt->bind_param("s", $subjectCode);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $cols[] = [$row['particulars'], $row['marks']];
        }
    }
    if (empty($cols) && $internalFM > 0) {
        $split = $internalFM / 4;
        $cols = [
            ['Attendance',  $split],
            ['Assignment',  $split],
            ['Class Test',  $split],
            ['Presentation',$split]
        ];
    }
}

/* ---------- 5. STUDENTS IN THIS SUBJECT ---------- */
$students = [];
if ($classId && $subjectCode) {
    $sql = "
        SELECT st.regd_id, st.symbol_no, st.name, st.status
        FROM students st 
        JOIN studentsub ss ON ss.student_id = st.student_id 
        WHERE st.Class_id = ? 
        AND st.status = 'active'
        AND ? IN (ss.sub1, ss.sub2, ss.sub3, ss.sub4, ss.sub5, ss.sub6, ss.sub7) 
        ORDER BY st.regd_id
    ";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("is", $classId, $subjectCode);
    $stmt->execute();
    $students = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
// Get class name for display
$className = '';
if ($classId) {
    $classRes = $conn->query("SELECT class FROM classes WHERE class_id = $classId");
    if ($classRow = $classRes->fetch_assoc()) {
        $className = $classRow['class'];
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Evaluation Sheet - NEB Format</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/main.css">
<style>
    /* ===== GENERAL STYLES ===== */
    .form-control-sm {
        padding: 5px 10px;
        font-size: 14px;
        height: 34px;
        min-width: 150px;
    }
    
    .filter-section {
        background: #f8f9fa;
        padding: 15px;
        border-radius: 5px;
        margin-bottom: 20px;
        border: 1px solid #dee2e6;
    }
    
    .rotate {
        writing-mode: vertical-rl;
        transform: rotate(180deg);
        white-space: nowrap;
        height: 100px;
        text-align: center;
        vertical-align: middle;
        width: 25px;
        font-size: 11px;
    }
    
    /* ===== STUDENT NAME COLUMN - WIDTH ADJUSTABLE ===== */
    .std-name {
        width: 200px;  /* Default width - adjustable */
        min-width: 150px;
        max-width: 300px;
        white-space: normal;
        word-wrap: break-word;
        line-height: 1.3;
    }
    
    /* For screen display */
    th .std-name, td .std-name {
        width: 200px;
    }
    
    /* Override for specific cells */
    td .std-name, td.std-name {
        text-align: left;
        padding: 8px 12px;
        font-size: 14px;
    }
    
    /* ===== NEB TABLE STYLES ===== */
    #customers1 {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    }
    
    #customers1 td {
        border: 1px solid #ddd;
        padding: 8px;
        transition: background-color 0.2s;
    }
    
    #customers1 tr:nth-child(even) {
        background-color: #f9f9f9;
    }
    
    #customers1 tr:hover {
        background-color: #f1f1f1;
    }
    
    #customers1 th {
        padding: 12px 8px;
        text-align: center;
        background-color: #2c3e50;
        color: white;
        font-weight: 600;
        font-size: 12px;
        border: 1px solid #1a252f;
    }
    
    /* Header table (NEB info) */
    .neb-header-table {
        width: 100%;
        border-collapse: collapse;
        font-family: Arial, sans-serif;
        font-size: 14px;
        margin-bottom: 15px;
    }
    
    .neb-header-table td {
        border: none !important;
        padding: 5px;
        background: transparent !important;
    }
    
    /* Signature section - will repeat on each page */
    .signature-table {
        width: 100%;
        border: none;
        text-align: center;
        font-family: Arial, sans-serif;
        margin-top: 30px;
        page-break-inside: avoid;
    }
    
    .signature-table td {
        border: none;
        padding: 10px 5px;
        background: transparent !important;
    }
    
                     
                   .school-seal {
    width: 80px;
    height: 80px;
    margin: 0 auto;
    border: 2px solid #333;
    border-radius: 50% !important;  /* This makes it round */
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    background: #f9f9f9;
    text-align: center;
    line-height: 1.2;
    box-sizing: border-box;
}


    /* ===== PRINT STYLES ===== */
    @media print {
        .no-print {
            display: none !important;
        }
        
        body {
            margin: 0;
            padding: 0.5in;
            font-family: Arial, sans-serif;
            font-size: 11pt;
            line-height: 1.3;
            background: white !important;
            color: black !important;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }
        
        /* Student name column - width adjustable in print */
        .std-name {
            width: 220px !important;
            min-width: 180px !important;
            max-width: 280px !important;
            white-space: normal !important;
            word-wrap: break-word !important;
        }
        
        /* Table styles for print */
        #customers1 {
            border-collapse: collapse;
            width: 100%;
            background: white !important;
        }
        
        #customers1 td, #customers1 th {
            border: 1px solid black !important;
            padding: 6px 4px !important;
            background: white !important;
            color: black !important;
        }
        
        #customers1 th {
            background-color: white !important;
            color: black !important;
            font-weight: bold;
            border-bottom: 2px solid black !important;
        }
        
        /* Remove all background colors and shading */
        #customers1 tr:nth-child(even),
        #customers1 tr:hover,
        #customers1 tr {
            background-color: white !important;
        }
        
        /* NEB Header - No borders */
        .neb-header-table td,
        .neb-header-table tr td,
        table[style*="font-family:Arial, sans-serif"] td {
            border: none !important;
            border-style: hidden !important;
            border-width: 0 !important;
            background: white !important;
        }
        
        /* Signature table - No borders - will repeat on each page */
        .signature-table {
            border: none !important;
            margin-top: 20px;
            page-break-inside: avoid;
            page-break-before: auto;
            page-break-after: auto;
        }
        
        .signature-table td {
            border: none !important;
            background: white !important;
        }
        
       .school-seal {
        border-radius: 50% !important;
        border: 1px solid black !important;
        background: white !important;
        width: 80px;
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
        
        /* Rotated text */
        .rotate {
            writing-mode: vertical-rl;
            transform: rotate(180deg);
            white-space: nowrap;
            height: 100px;
            text-align: center;
            vertical-align: middle;
            width: 20px;
            font-size: 9pt;
        }
        
        /* Page margins */
        @page {
            margin: 0.5in;
            size: A4 portrait;
        }
        
        /* Ensure proper page breaks and repeating elements */
        table {
            page-break-inside: auto;
        }
        
        tr {
            page-break-inside: avoid;
            page-break-after: auto;
        }
        
        thead {
            display: table-header-group;
        }
        
        tfoot {
            display: table-footer-group;
        }
        
        /* Force signature to appear on each page */
        .page-break-signature {
            display: table-footer-group;
            page-break-inside: avoid;
        }
    }
    
    /* ===== RESPONSIVE STYLES ===== */
    @media screen and (max-width: 768px) {
        .std-name {
            width: 150px;
            min-width: 120px;
        }
        
        #customers1 {
            font-size: 12px;
        }
        
        #customers1 td, #customers1 th {
            padding: 5px;
        }
        
        .rotate {
            height: 80px;
            font-size: 10px;
        }
    }
    
    /* Loading indicator */
    .loading {
        display: inline-block;
        width: 20px;
        height: 20px;
        border: 3px solid #f3f3f3;
        border-top: 3px solid #3498db;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }
    
    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    /* Smooth transitions */
    select, button, .btn {
        transition: all 0.3s ease;
    }
    
    .btn-success {
        background: #28a745;
        border-color: #28a745;
    }
    
    .btn-success:hover {
        background: #218838;
        border-color: #1e7e34;
        transform: translateY(-1px);
        box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    }
</style>
</head>
<body class="top-navbar-fixed">
<div class="main-wrapper">
<?php include 'includes/topbar.php'; ?>
<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><i class="fa fa-file-text-o"></i> Evaluation Sheet - NEB Format</h2>
    </div>
    <div class="col-md-6 text-right">
        <?php if (!empty($students)): ?>
            <span class="badge badge-primary">Total Students: <?= count($students) ?></span>
        <?php endif; ?>
    </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><a href="#">Reports</a></li>
            <li class="active">Evaluation Sheet</li>
        </ul>
    </div>
</div>

<div class="panel">
    <div class="panel-heading">
        <h5><i class="fa fa-sliders"></i> NEB Evaluation Sheet Generator</h5>
    </div>
    <div class="panel-body">
        <!-- Filter Form -->
        <form class="form-inline no-print" method="get" id="filterForm">
            <div class="form-group">
                <label>Class&nbsp;</label>
                <select name="class_id" class="form-control form-control-sm" onchange="this.form.submit()">
                    <option value="">-- Select Class --</option>
                    <?php 
                    $groupedClasses = [];
                    foreach ($classes as $c) {
                        $key = $c['class_id'];
                        if (!isset($groupedClasses[$key])) {
                            $groupedClasses[$key] = [
                                'class_id' => $c['class_id'],
                                'class' => $c['class'],
                                'sections' => []
                            ];
                        }
                        if ($c['section_name']) {
                            $groupedClasses[$key]['sections'][] = $c['section_name'];
                        }
                    }
                    
                    foreach ($groupedClasses as $c):
                        $sectionText = !empty($c['sections']) ? ' (Sections: ' . implode(', ', $c['sections']) . ')' : '';
                    ?>
                        <option value="<?= $c['class_id'] ?>" <?= $classId==$c['class_id']?'selected':'' ?>>
                            <?= htmlspecialchars($c['class'] . $sectionText) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <?php if ($classId): ?>
            <div class="form-group">
                <label>&nbsp;Sheet&nbsp;</label>
                <select name="sheet" class="form-control form-control-sm" onchange="this.form.submit()">
                    <option value="theory"   <?= $sheet=='theory'?'selected':'' ?>>Theory</option>
                    <option value="internal" <?= $sheet=='internal'?'selected':'' ?>>Internal</option>
                </select>
            </div>

            <div class="form-group">
                <label>&nbsp;Subject&nbsp;</label>
                <select name="subject_code" class="form-control form-control-sm" onchange="this.form.submit()" required>
                    <option value="">-- Select Subject --</option>
                    <?php foreach ($subjects as $s): ?>
                        <option value="<?= $s['subject_code'] ?>" <?= $subjectCode===$s['subject_code']?'selected':'' ?>>
                            <?= htmlspecialchars($s['subject_name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <?php endif; ?>
            
            <?php if ($classId && $subjectCode && !empty($students)): ?>
            <div class="form-group ml-2">
                <button type="button" onclick="printTable()" class="btn btn-success btn-sm">
                    <i class="fa fa-print"></i> Print Evaluation Sheet
                </button>
            </div>
            <?php endif; ?>
        </form>
        <hr class="no-print">

        <?php
        if (!$classId){
            echo '<div class="alert alert-info"><i class="fa fa-info-circle"></i> Select a class to continue.</div>';
        }elseif (!$subjectCode){
            echo '<div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i> Select a subject to generate evaluation sheet.</div>';
        }elseif (!$students){
            echo '<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> No students found for this subject.</div>';
        }else{
        ?>
        <!-- NEB Evaluation Table -->
        <div id="printableTable">
            <table id="customers1">
                <tr>
                    <td style="padding: 20px;">
                        <!-- NEB Header Table -->
                        <table class="neb-header-table">
                            <tr>
                                <td style="width: 300px;" class="no-print">
                                    <img src="assets/image/nepal_logo.png" width="100" alt="Nepal Logo">
                                </td>
                                <td style="text-align:center; width: 800px;">
                                    <div style="font-weight:bold; font-size:25px;">National Examination Board</div>
                                    <div style="font-size:20px;">Office of the Controller of Examinations</div>
                                    <div style="font-size:20px;">Sanothimi, Bhaktapur</div>
                                    <div style="font-size:19px; font-weight:bold; text-decoration:underline; margin-top: 10px;">
                                        <?= $sheet == 'internal' ? 'Internal (Practical) Marks Evaluation Scheme' : 'Theory Marks Evaluation Scheme' ?>
                                    </div>
                                </td>
                                <td style="width: 150px;" class="no-print">
                                    <img src="assets/image/neb_logo.jpg" width="90" alt="NEB Logo">
                                </td>
                            </tr>
                            
                        </table>

                        <!-- Main Evaluation Table with TFOOT for repeating signature -->
                        <table border="1" style="width:100%; border-collapse:collapse; text-align:center; margin-top: 20px;">
                            <thead>
                                <tr>
                                <td colspan="2" style="border:none"><strong>School Code:</strong> <?= htmlspecialchars($schoolCode) ?></td>
                                <td colspan="7" style="text-align: center;"><strong>School:</strong> <?= htmlspecialchars($schoolName) ?></td>
                                <td colspan="3" style="text-align: right;"><strong>Grade:</strong> <?= htmlspecialchars($className) ?></td>
                            </tr>
                            <tr>
                                <td colspan="2" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                                    <strong>Subject:</strong> <?= htmlspecialchars($subjectName) ?>
                                </td>
                                <td  colspan="7" style="text-align: center;"><strong>Subject Code:</strong> <?= htmlspecialchars($subjectCode) ?></td>
                                <td colspan="3" style="text-align: right;">
                                    <strong>Full Marks:</strong> <?= $sheet == 'internal' ? htmlspecialchars($internalFM) : htmlspecialchars($theoryFM) ?>
                                </td>
                            </tr>
                                <tr>
                                    <th style="width: 40px;">S.N.</th>
                                    <th style="width: 80px;">Regd ID</th>
                                    <th class="std-name">Student Name</th>
                                    <?php if ($sheet == 'internal'): ?>
                                        <?php foreach ($cols as $col): ?>
                                            <th class="rotate" style="white-space: normal; word-wrap: break-word;"><?= htmlspecialchars($col[0]) ?><br>(<?= (int)$col[1] ?>)</th>
                                        <?php endforeach; ?>
                                        <th style="width: 50px;">Total<br>(<?= $internalFM ?>)</th>
                                        <th style="width: 100px;">Marks in Words</th>
                                        <th class="rotate" style="width: 30px;">Re.</th>
                                    <?php else: ?>
                                        <th style="width: 70px;">Marks<br>Obtained<br>(<?= $theoryFM ?>)</th>
                                        <th style="width: 120px;">Marks in Words</th>
                                        <th class="rotate" style="width: 30px;">Re.</th>
                                    <?php endif; ?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($students as $i => $st): ?>
                                    <tr>
                                        <td style="font-size:12px"><?= $i + 1 ?></td>
                                        <td style="font-size:12px"><?= htmlspecialchars($st['regd_id']) ?></td>
                                        <td class="std-name" style="text-align:left;"><?= htmlspecialchars($st['name']) ?></td>
                                        <?php if ($sheet == 'internal'): ?>
                                            <?php foreach ($cols as $_): echo "<td></td>"; endforeach; ?>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                        <?php else: ?>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                        <?php endif; ?>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                            <!-- TFOOT with signature - this will repeat on each page when printed -->
                            <tfoot class="page-break-signature">
                                <tr>
                                    <td colspan="100%" style="border: none !important; background: transparent; padding: 0;">
                                        <table class="signature-table">
                                            <tr>
                                                <td style="width: 25%; text-align: left;">
                                                    <strong><u>Internal Examiner</u></strong><br>
                                                    Name :<br>Signature :<br>Date :
                                                </td>
                                                <td style="width: 17%; text-align: center;">
                                                    <div class="school-seal">
                                                        School<br>Seal
                                                    </div>
                                                </td>
                                                <td style="width: 30%; text-align: left;">
                                                    <strong><u>Head Teacher/Principal</u></strong><br>
                                                    Name : Sher Bahadur Shrestha<br>Signature :<br>Date :
                                                </td>
                                                <?php if ($sheet == 'internal'): ?>
                                                <td style="width: 35%; text-align: left;">
                                                    <strong><u>External Examiner</u></strong><br>
                                                    Name :<br>School/College :<br>Signature :<br>Date :
                                                </td>
                                                <?php endif; ?>
                                            </tr>
                                        </table>
                                        
                                        <!-- Footer -->
                                        <div style="text-align: center; margin-top: 10px; font-size: 11px; color: #666;">
                                            Total Students: <?= count($students) ?> | Page <span class="page-number"></span>
                                        </div>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
        <?php } ?>
    </div>
</div>
</div>
</div>
</div>
</div>
</div>

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

<script>
// Smooth and enhanced print function
function printTable() {
    // Show loading indicator (optional)
    const printBtn = event.target;
    const originalText = printBtn.innerHTML;
    printBtn.innerHTML = '<span class="loading"></span> Preparing...';
    printBtn.disabled = true;
    
    // Get printable table HTML
    var printableTable = document.getElementById('printableTable').innerHTML;
    
    // Create a new window
    var printWindow = window.open('', '_blank');
    
    // Get current date for footer
    const currentDate = new Date().toLocaleString();
    
    // Write the printable content with enhanced styling
    printWindow.document.write(`
        <!DOCTYPE html>
        <html>
        <head>
            <title>NEB Evaluation Sheet - <?= htmlspecialchars($subjectName) ?></title>
            <style>
                /* Print-specific styles */
                @media print {
                    body {
                        margin: 0.5in;
                        font-family: Arial, sans-serif;
                        font-size: 11pt;
                        line-height: 1.3;
                        background: white;
                        color: black;
                        -webkit-print-color-adjust: exact;
                        print-color-adjust: exact;
                    }
                    
                    /* Student name column - adjustable width */
                    .std-name {
                        width: 180px !important;
                        min-width: 150px !important;
                        max-width: 200px !important;
                        white-space: normal !important;
                        word-wrap: break-word !important;
        font-size: 12px !important;
                    }
                    
                    /* Main table borders */
                    table table:last-of-type td,
                    table table:last-of-type th {
                        border: 1px solid black !important;
                        padding: 6px 4px !important;
                        background: white !important;
                        color: black !important;
                    }
                    
                    /* Header table - no borders */
                    table table:first-of-type td,
                    .neb-header-table td {
                        border: none !important;
                        background: white !important;
                    }
                    
                    /* Signature table - no borders - will repeat on each page */
                    .signature-table {
                        border: none !important;
                        margin-top: 20px;
                        page-break-inside: avoid;
                    }
                    
                    .signature-table td {
                        border: none !important;
                        background: white !important;
                        padding: 10px 5px;
                    }
  
    .school-seal {
        border-radius: 50% !important;
        border: 1px solid black !important;
        background: white !important;
        width: 80px;
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

                    
                    /* Rotated text */
                    .rotate {
                        writing-mode: vertical-rl;
                        transform: rotate(180deg);
                        white-space: nowrap;
                        height: 100px;
                        text-align: center;
                        vertical-align: middle;
                        width: 20px;
                        font-size: 9pt;
                    }
                    
                    /* Remove colors */
                    #customers1 th {
                        background: white !important;
                        color: black !important;
                        border-bottom: 2px solid black !important;
                    }
                    
                    #customers1 tr:nth-child(even) {
                        background: white !important;
                    }
                    
                    /* Page setup */
                    @page {
                        margin: 0.5in;
                        size: A4 portrait;
                    }
                    
                    /* Ensure TFOOT repeats on each page */
                    thead {
                        display: table-header-group;
                    }
                    
                    tfoot {
                        display: table-footer-group;
                    }
                    
                    tr {
                        page-break-inside: avoid;
                    }
                    
                    /* Signature styling */
                    .page-break-signature {
                        display: table-footer-group;
                    }
                    
                    .page-number:after {
                        content: counter(page);
                    }
                }
                
                /* Screen styles */
                body {
                    font-family: Arial, sans-serif;
                    font-size: 12px;
                    margin: 20px;
                }
                
                .std-name {
                    width: 200px;
                    min-width: 150px;
                    max-width: 300px;
                }
                
                .signature-table {
                    width: 100%;
                    border: none;
                    text-align: center;
                    font-family: Arial, sans-serif;
                    margin-top: 30px;
                }
                
                .signature-table td {
                    border: none;
                    padding: 10px 5px;
                }
            </style>
        </head>
        <body>
            ${printableTable}
        </body>
        </html>
    `);
    
    printWindow.document.close();
    
    // Print after content loads with smooth transition
    printWindow.onload = function() {
        setTimeout(function() {
            printWindow.focus();
            printWindow.print();
            
            // Restore button after printing
            printBtn.innerHTML = originalText;
            printBtn.disabled = false;
            
            // Close print window after print dialog closes
            printWindow.onafterprint = function() {
                printWindow.close();
            };
        }, 500);
    };
}

// Auto-submit form with smooth loading (optional)
$(document).ready(function() {
    // Add loading indicator to form submits
    $('#filterForm select').change(function() {
        $(this).closest('form').addClass('loading');
    });
    
    // Initialize tooltips if needed
    $('[data-toggle="tooltip"]').tooltip();
});
</script>

</body>
</html>