<?php
require_once '../includes/config.php';

// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Location: ../auth/login.php');
    exit();
}

// Get student ID from URL
$student_id = $_GET['id'] ?? 0;

if (!$student_id) {
    die('Student ID is required');
}

// Get student information
$sql = "SELECT s.*, c.code as course_code, d.name as department_name 
        FROM students s
        LEFT JOIN courses c ON s.course_id = c.id
        LEFT JOIN departments d ON s.department_id = d.id
        WHERE s.id = $student_id AND s.status = 1";
$result = query($conn, $sql);

if (!$result || mysqli_num_rows($result) === 0) {
    die('Student not found');
}

$student = mysqli_fetch_assoc($result);

// Get the latest QR code file
$qr_files = glob('generated/student_' . $student['student_id'] . '_*.png');
$qr_filepath = !empty($qr_files) ? end($qr_files) : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print QR Code - <?php echo $student['full_name']; ?></title>
    <style>
        @media print {
            @page {
                margin: 0.5in;
                size: letter portrait;
            }
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                -webkit-print-color-adjust: exact !important;
                print-color-adjust: exact !important;
            }
            .print-container {
                text-align: center;
                padding: 20px;
            }
            .school-header {
                margin-bottom: 20px;
            }
            .school-header h2 {
                color: #3498db;
                margin-bottom: 5px;
            }
            .school-header h3 {
                color: #666;
                margin-top: 0;
            }
            .qr-code-container {
                margin: 30px 0;
                padding: 20px;
                border: 1px solid #ddd;
                display: inline-block;
                background: white;
            }
            .qr-code-image {
                width: 300px;
                height: 300px;
            }
            .student-info {
                margin: 20px 0;
                padding: 15px;
                border: 1px solid #eee;
                display: inline-block;
                text-align: left;
            }
            .student-info h4 {
                margin-top: 0;
                color: #333;
            }
            .footer {
                margin-top: 40px;
                font-size: 12px;
                color: #666;
                border-top: 1px solid #ddd;
                padding-top: 10px;
            }
        }
        
        @media screen {
            body {
                background: #f8f9fa;
                padding: 20px;
            }
            .print-container {
                max-width: 800px;
                margin: 0 auto;
                background: white;
                padding: 30px;
                border-radius: 10px;
                box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            }
            .print-actions {
                text-align: center;
                margin-bottom: 20px;
                padding-bottom: 20px;
                border-bottom: 1px solid #dee2e6;
            }
        }
    </style>
</head>
<body>
    <?php if (!isset($_GET['auto'])): ?>
    <div class="print-actions">
        <button onclick="window.print()" class="btn btn-primary" style="padding: 10px 20px; font-size: 16px;">
            <i class="bi bi-printer"></i> Print Now
        </button>
        <button onclick="window.close()" class="btn btn-secondary" style="padding: 10px 20px; font-size: 16px;">
            <i class="bi bi-x-circle"></i> Close
        </button>
    </div>
    <?php endif; ?>
    
    <div class="print-container">
        <!-- School Header -->
        <div class="school-header">
            <h2><?php echo SITE_NAME; ?></h2>
            <h3>Student QR Code</h3>
        </div>
        
        <!-- QR Code -->
        <div class="qr-code-container">
            <?php if ($qr_filepath && file_exists($qr_filepath)): ?>
                <img src="<?php echo $qr_filepath; ?>" alt="QR Code" class="qr-code-image">
            <?php else: ?>
                <div style="width: 300px; height: 300px; background: #f0f0f0; display: flex; align-items: center; justify-content: center;">
                    <p>QR Code not generated</p>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Student Information -->
        <div class="student-info">
            <h4><?php echo $student['full_name']; ?></h4>
            <table style="border-collapse: collapse;">
                <tr>
                    <td style="padding: 5px 10px 5px 0;"><strong>Student ID:</strong></td>
                    <td style="padding: 5px 0;"><?php echo $student['student_id']; ?></td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px 5px 0;"><strong>Year Level:</strong></td>
                    <td style="padding: 5px 0;">Year <?php echo $student['year_level']; ?></td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px 5px 0;"><strong>Course:</strong></td>
                    <td style="padding: 5px 0;"><?php echo $student['course_code']; ?></td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px 5px 0;"><strong>Department:</strong></td>
                    <td style="padding: 5px 0;"><?php echo $student['department_name']; ?></td>
                </tr>
            </table>
        </div>
        
        <!-- Footer -->
        <div class="footer">
            <p>Generated on: <?php echo date('F j, Y'); ?></p>
            <p>© <?php echo date('Y'); ?> <?php echo SITE_NAME; ?> - QR Attendance System</p>
            <p><small>Present this QR code at attendance stations for scanning</small></p>
        </div>
    </div>
    
    <?php if (isset($_GET['auto'])): ?>
    <script>
        window.onload = function() {
            window.print();
            setTimeout(function() {
                window.close();
            }, 1000);
        }
    </script>
    <?php endif; ?>
</body>
</html>