<?php
session_start();
require_once '../config/database.php';
require_once '../includes/site_settings.php';

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

$exam_id = $_GET['exam_id'] ?? '';
$student_id = $_GET['student_id'] ?? '';

// Get site settings
$settings = get_all_settings($pdo);
$site_name = $settings['site_name'] ?? 'প্রতিষ্ঠানের নাম';
$site_address = $settings['site_address'] ?? 'ঠিকানা, ফোন: ০১৭xxxxxxxx';
$logo_image = $settings['logo_image'] ?? '';
$logo_size = isset($settings['logo_size']) ? intval($settings['logo_size']) : 100;

// Get exam details
$exam_details = null;
$exam_subjects = [];

if ($exam_id) {
    $stmt = $pdo->prepare("
        SELECT e.*, c.name as class_name, c.section
        FROM exams e
        JOIN classes c ON e.class_id = c.id
        WHERE e.id = ?
    ");
    $stmt->execute([$exam_id]);
    $exam_details = $stmt->fetch();
    
    if ($exam_details) {
        // Get exam subjects that the student is enrolled in
        if ($student_id) {
            // Modified query to only get subjects that the student is enrolled in
            $stmt = $pdo->prepare("
                SELECT es.*, s.name as subject_name, s.code as subject_code
                FROM exam_subjects es
                JOIN subjects s ON es.subject_id = s.id
                JOIN student_subjects ss ON es.subject_id = ss.subject_id
                WHERE es.exam_id = ? AND ss.student_id = ?
                ORDER BY es.exam_date, es.start_time
            ");
            $stmt->execute([$exam_id, $student_id]);
        } else {
            // Fallback to original query if no student_id (though this shouldn't happen in normal flow)
            $stmt = $pdo->prepare("
                SELECT es.*, s.name as subject_name, s.code as subject_code
                FROM exam_subjects es
                JOIN subjects s ON es.subject_id = s.id
                WHERE es.exam_id = ?
                ORDER BY es.exam_date, es.start_time
            ");
            $stmt->execute([$exam_id]);
        }
        $exam_subjects = $stmt->fetchAll();
    }
}

// Get student details
$student = null;
if ($student_id) {
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name, c.section
        FROM students s
        JOIN classes c ON s.class_id = c.id
        WHERE s.id = ?
    ");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch();
}

// Get seat arrangement if exists
$seat_arrangement = null;
if ($exam_id && $student_id) {
    $stmt = $pdo->prepare("
        SELECT sa.*
        FROM seat_arrangements sa
        WHERE sa.exam_id = ? AND sa.student_id = ?
        LIMIT 1
    ");
    $stmt->execute([$exam_id, $student_id]);
    $seat_arrangement = $stmt->fetch();
}

// Get all exams for dropdown
$all_exams = $pdo->query("
    SELECT e.id, e.name, c.name as class_name, c.section, e.session, e.group_name
    FROM exams e
    JOIN classes c ON e.class_id = c.id
    ORDER BY e.created_at DESC
")->fetchAll();

// Get students for dropdown (based on selected exam)
$students = [];
if ($exam_id && $exam_details) {
    $params = [$exam_details['class_id'], $exam_details['session']];
    
    if ($exam_details['group_name']) {
        $stmt = $pdo->prepare("
            SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number, s.guardian_name
            FROM students s
            WHERE s.class_id = ? AND s.session = ? AND (s.group_name = ? OR UPPER(s.group_name) = UPPER(?)) AND s.status = 'active'
            ORDER BY s.roll_number
        ");
        $params[] = $exam_details['group_name'];
        $params[] = $exam_details['group_name'];
    } else {
        $stmt = $pdo->prepare("
            SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number, s.guardian_name
            FROM students s
            WHERE s.class_id = ? AND s.session = ? AND s.status = 'active'
            ORDER BY s.roll_number
        ");
    }
    
    $stmt->execute($params);
    $students = $stmt->fetchAll();
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>এডমিট কার্ড - স্কুল ম্যানেজমেন্ট সিস্টেম</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Hind Siliguri', sans-serif;
            background: linear-gradient(180deg, #f0f5ff 0%, #eef7fd 100%);
            color: #323a4f;
        }

        .navbar {
            box-shadow: 0 2px 14px rgba(13, 110, 253, 0.15);
            padding: 0.35rem 0.75rem;
            min-height: 44px;
            background: linear-gradient(90deg, #0d6efd 0%, #6610f2 100%);
        }

        .navbar-brand,
        .nav-link {
            color: #ffffff !important;
        }

        .navbar-brand {
            font-size: 0.95rem;
            font-weight: 600;
        }

        .nav-link {
            padding: 0.35rem 0.5rem;
            font-size: 0.82rem;
        }

        .card {
            border: 1px solid rgba(100, 116, 139, 0.16);
            box-shadow: 0 2px 18px rgba(15, 23, 42, 0.06);
            border-radius: 10px;
        }

        .card-header {
            padding: 8px 12px;
            background: #ffffff;
            border-bottom: 1px solid #d7e1ec;
        }

        .card-header h5 {
            margin: 0;
            font-size: 0.92rem;
            color: #0d6efd;
        }

        .card-body {
            padding: 10px 14px;
        }

        .admit-card {
            background: linear-gradient(180deg, #ffffff 0%, #eef4ff 100%);
            border: 1px solid #b6d7ff;
            border-radius: 10px;
            padding: 12px;
            margin-bottom: 14px;
            box-shadow: inset 0 0 0 1px rgba(13, 110, 253, 0.16);
        }

        .btn-primary {
            background: linear-gradient(90deg, #0d6efd 0%, #6610f2 100%) !important;
            border-color: transparent !important;
        }

        .btn-info {
            background: linear-gradient(90deg, #20c997 0%, #0dcaf0 100%) !important;
            border-color: transparent !important;
        }

        .header-section {
            display: flex;
            align-items: center;
            gap: 10px;
            border-bottom: 1px solid #0d6efd;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }

        .header-section img {
            max-height: 68px;
            width: auto;
            border-radius: 12px;
            border: 1px solid #dfe7ef;
            background: #fff;
        }

        .header-title {
            flex: 1;
            line-height: 1.3;
        }

        .header-title h2 {
            margin: 0;
            font-size: 1.05rem;
            font-weight: 700;
            color: #0d6efd;
        }

        .header-title p {
            margin: 3px 0 0;
            font-size: 0.75rem;
            color: #495057;
        }

        .header-title .exam-name {
            margin-top: 4px;
            font-size: 0.82rem;
            font-weight: 600;
            color: #212529;
        }

        .student-info {
            display: grid;
            grid-template-columns: minmax(0, 1fr) 100px;
            gap: 10px;
            background: linear-gradient(135deg, #fff8f0 0%, #f4f7fb 100%);
            padding: 10px 12px;
            border-radius: 10px;
            margin-bottom: 14px;
            border: 1px solid rgba(13, 110, 253, 0.12);
        }

        .info-block {
            display: grid;
            gap: 6px;
        }

        .info-row {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            font-size: 0.78rem;
            color: #30384a;
        }

        .info-label {
            font-weight: 700;
            color: #0d6efd;
            min-width: 80px;
        }

        .student-photo-wrap {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 6px;
            padding: 4px;
            border: 1px solid #d7e0ea;
            border-radius: 10px;
            background: #ffffff;
        }

        .student-photo {
            width: 78px;
            height: 94px;
            object-fit: cover;
            border-radius: 10px;
            border: 1px solid #cfd8e4;
            background: #f8f9fa;
        }

        .exam-details-table th {
            background-color: #6610f2;
            color: white;
            text-align: center;
            font-size: 0.72rem;
            padding: 6px;
            vertical-align: middle;
        }

        .exam-details-table td {
            text-align: center;
            font-size: 0.74rem;
            padding: 6px;
            vertical-align: middle;
            color: #2e3a59;
        }

        .exam-details-table tbody tr:nth-child(odd) {
            background: #f3f0ff;
        }

        .exam-details-table tbody tr:nth-child(even) {
            background: #ffffff;
        }

        .instructions {
            background-color: #fff3cd;
            border: 1px solid #ffe8a1;
            padding: 10px 12px;
            border-radius: 10px;
            font-size: 0.78rem;
            margin-top: 12px;
            color: #856404;
        }

        .instructions h6 {
            margin-bottom: 6px;
            font-size: 0.86rem;
            color: #664d03;
        }

        .instructions li {
            color: #5c4b1a;
        }

        .instructions ul {
            margin: 0;
            padding-left: 18px;
        }

        .instructions li {
            margin-bottom: 6px;
            line-height: 1.45;
        }

        .signature-section {
            margin-top: 14px;
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 8px;
            font-size: 0.77rem;
            color: #3b4858;
        }

        .signature-block {
            border-top: 1px solid #a9b8cc;
            padding-top: 10px;
            text-align: center;
        }

        .signature-block p {
            margin: 0;
            font-weight: 600;
        }

        .no-print .card-header {
            background: #ffffff;
            border-bottom: none;
        }

        @media (max-width: 767px) {
            .header-section {
                flex-direction: column;
                text-align: center;
            }
            .student-info {
                grid-template-columns: 1fr;
            }
            .signature-section {
                grid-template-columns: 1fr;
            }
            .info-row {
                flex-direction: column;
                align-items: flex-start;
            }
            .card-body,
            .admit-card {
                padding-left: 10px;
                padding-right: 10px;
            }
        }

        @media print {
            .no-print { display: none !important; }
            body {
                background: white;
                margin: 0;
                padding: 0;
                color: #000;
                font-size: 10.5px;
                -webkit-print-color-adjust: exact;
                color-adjust: exact;
            }
            .admit-card {
                box-shadow: none;
                border-color: #000;
                margin: 0 0 12px;
                padding: 12px;
                page-break-inside: avoid;
            }
            .header-section {
                border-bottom-color: #000;
            }
            .student-info {
                background: #f8f9fa !important;
            }
            .exam-details-table th {
                background-color: #000 !important;
                color: #fff !important;
            }
            .exam-details-table td,
            .exam-details-table th {
                font-size: 0.78rem !important;
                padding: 5px !important;
            }
            .instructions {
                background: #f0f8ff !important;
                border-color: #b0c4de !important;
            }
            .signature-block {
                border-top-color: #000;
            }
        }
    </style>
</head>
<body class="bengali-text">
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary no-print">
        <div class="container-fluid">
            <a class="navbar-brand" href="../index.php">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="../index.php">
                    <i class="fas fa-home"></i> হোম
                </a>
                <a class="nav-link" href="routine.php">
                    <i class="fas fa-calendar"></i> পরীক্ষার রুটিন
                </a>
                <a class="nav-link" href="bulk_admit_card.php">
                    <i class="fas fa-users"></i> বাল্ক এডমিট কার্ড
                </a>
                <a class="nav-link" href="seat_card.php">
                    <i class="fas fa-id-card"></i> সীট কার্ড
                </a>
                <a class="nav-link" href="attendance_certificate.php">
                    <i class="fas fa-file-alt"></i> হাজিরা পত্র
                </a>
                <a class="nav-link" href="bulk_attendance_certificate.php">
                    <i class="fas fa-files-alt"></i> বাল্ক হাজিরা পত্র
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card no-print">
                    <div class="card-header">
                        <h5><i class="fas fa-file-alt"></i> এডমিট কার্ড প্রিন্ট</h5>
                    </div>
                    <div class="card-body">
                        <form method="GET" class="row gy-2 gx-2 align-items-end">
                            <div class="col-sm-6 col-md-4">
                                <label for="quick_exam_id" class="form-label">পরীক্ষা আইডি</label>
                                <input type="number" class="form-control" id="quick_exam_id" name="exam_id" placeholder="ID" value="<?php echo htmlspecialchars($exam_id); ?>">
                            </div>
                            <div class="col-sm-6 col-md-4">
                                <label for="quick_student_id" class="form-label">ছাত্র আইডি</label>
                                <input type="number" class="form-control" id="quick_student_id" name="student_id" placeholder="ID" value="<?php echo htmlspecialchars($student_id); ?>">
                            </div>
                            <div class="col-sm-12 col-md-4">
                                <label class="form-label">&nbsp;</label>
                                <button type="submit" class="btn btn-primary w-100">
                                    <i class="fas fa-search"></i> দ্রুত খুঁজুন
                                </button>
                            </div>
                        </form>

                        <form method="GET">
                            <div class="row gy-2 gx-2 mt-3 align-items-end">
                                <div class="col-sm-12 col-md-5">
                                    <label for="exam_id" class="form-label">পরীক্ষা নির্বাচন করুন</label>
                                    <select class="form-select" id="exam_id" name="exam_id" required>
                                        <option value="">পরীক্ষা নির্বাচন করুন</option>
                                        <?php foreach ($all_exams as $exam): ?>
                                            <option value="<?php echo $exam['id']; ?>" <?php echo ($exam_id == $exam['id']) ? 'selected' : ''; ?> >
                                                <?php echo htmlspecialchars($exam['name'] . ' - ' . $exam['class_name'] . ' ' . $exam['section']); ?>
                                                <?php if ($exam['group_name']): ?> (<?php echo htmlspecialchars($exam['group_name']); ?>) <?php endif; ?>
                                                [<?php echo htmlspecialchars($exam['session']); ?>]
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-4">
                                    <label for="student_id" class="form-label">ছাত্র/ছাত্রী নির্বাচন করুন</label>
                                    <select class="form-select" id="student_id" name="student_id" required <?php echo !$exam_id ? 'disabled' : ''; ?> >
                                        <option value="">প্রথমে পরীক্ষা নির্বাচন করুন</option>
                                        <?php if ($exam_id && !empty($students)): ?>
                                            <?php foreach ($students as $stu): ?>
                                                <option value="<?php echo $stu['id']; ?>" <?php echo ($student_id == $stu['id']) ? 'selected' : ''; ?> >
                                                    <?php echo htmlspecialchars($stu['first_name'] . ' ' . $stu['last_name']); ?> (রোল: <?php echo htmlspecialchars($stu['roll_number']); ?>)
                                                </option>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-3 text-end">
                                    <label class="form-label">&nbsp;</label>
                                    <button type="submit" class="btn btn-primary w-100">
                                        <i class="fas fa-search"></i> দেখুন
                                    </button>
                                </div>
                            </div>
                        </form>
                        <?php if ($exam_id && $student_id && $exam_details && $student): ?>
                            <div class="mt-3 text-end">
                                <a href="bulk_admit_card.php" class="btn btn-info me-2">
                                    <i class="fas fa-users"></i> বাল্ক এডমিট
                                </a>
                                <button type="button" class="btn btn-success" onclick="window.print()">
                                    <i class="fas fa-print"></i> প্রিন্ট
                                </button>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <?php if ($exam_id && $student_id && $exam_details && $student): ?>
                    <div class="admit-card">
                        <!-- Header Section -->
                        <div class="header-section">
                            <?php if ($logo_image): ?>
                                <img src="../<?php echo htmlspecialchars($logo_image); ?>" alt="Logo">
                            <?php endif; ?>
                            <div class="header-title">
                                <h2><?php echo htmlspecialchars($site_name); ?></h2>
                                <p><?php echo htmlspecialchars($site_address); ?></p>
                                <div class="exam-name">এডমিট কার্ড - <?php echo htmlspecialchars($exam_details['name']); ?></div>
                            </div>
                        </div>
                        
                        <!-- Student Information -->
                        <div class="student-info">
                            <div class="info-block">
                                <div class="info-row">
                                    <span class="info-label">নাম</span>
                                    <span><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">শ্রেণি</span>
                                    <span><?php echo htmlspecialchars($student['class_name'] . ' ' . $student['section']); ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">গ্রুপ</span>
                                    <span><?php echo htmlspecialchars($student['group_name'] ?? 'সাধারণ'); ?></span>
                                </div>
                            </div>
                            <div class="info-block">
                                <div class="info-row">
                                    <span class="info-label">রোল নম্বর</span>
                                    <span><?php echo htmlspecialchars($student['roll_number']); ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">আইডি নম্বর</span>
                                    <span><?php echo htmlspecialchars($student['student_id']); ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">অভিভাবক</span>
                                    <span><?php echo htmlspecialchars($student['guardian_name']); ?></span>
                                </div>
                            </div>
                            <div class="student-photo-wrap">
                                <?php if (!empty($student['photo'])): ?>
                                    <?php 
                                    $photo_paths = [
                                        '../' . $student['photo'],  
                                        $student['photo'],          
                                        '../uploads/students/' . basename($student['photo']) 
                                    ];
                                    $valid_photo_path = null;
                                    foreach ($photo_paths as $path) {
                                        if (file_exists($path)) {
                                            $valid_photo_path = $path;
                                            break;
                                        }
                                    }
                                    ?>
                                    <?php if ($valid_photo_path): ?>
                                        <img src="<?php echo htmlspecialchars($valid_photo_path); ?>" alt="ছাত্র/ছাত্রীর ছবি" class="student-photo">
                                    <?php else: ?>
                                        <div class="student-photo d-flex align-items-center justify-content-center">
                                            <i class="fas fa-user" style="font-size: 1.4rem; color: #6c757d;"></i>
                                        </div>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <div class="student-photo d-flex align-items-center justify-content-center">
                                        <i class="fas fa-user" style="font-size: 1.4rem; color: #6c757d;"></i>
                                    </div>
                                <?php endif; ?>
                                <div style="font-size: 0.78rem; font-weight: 600; color: #495057;">ছবি</div>
                            </div>
                        </div>
                        
                        <!-- Exam Details -->
                        <h5 class="text-center mb-2" style="font-size: 0.9rem;">পরীক্ষার বিবরণ</h5>
                        <div class="table-responsive">
                            <table class="table table-bordered exam-details-table">
                                <thead>
                                    <tr>
                                        <th style="width: 15%;">তারিখ</th>
                                <th style="width: 13%;">দিন</th>
                                <th style="width: 32%;">বিষয়</th>
                                <th style="width: 18%;">সময়</th>
                                <th style="width: 10%;">মার্কস</th>
                                <?php if ($seat_arrangement): ?>
                                    <th style="width: 12%;">রুম/সিট</th>
                                <?php endif; ?>
                            </tr>
                        </thead>
                        <tbody>
                                    <?php 
                                    // Group subjects by date
                                    $grouped_by_date = [];
                                    foreach ($exam_subjects as $row) {
                                        $d = date('Y-m-d', strtotime($row['exam_date']));
                                        if (!isset($grouped_by_date[$d])) { $grouped_by_date[$d] = []; }
                                        $grouped_by_date[$d][] = $row;
                                    }
                                    // Preserve order as fetched (already ordered by exam_date, start_time)
                                    $ordered_dates = array_keys($grouped_by_date);
                                    
                                    foreach ($ordered_dates as $dateKey):
                                        $rows = $grouped_by_date[$dateKey];
                                        // Build subjects string with slash
                                        $subject_parts = [];
                                        $time_parts = [];
                                        $marks_parts = [];
                                        foreach ($rows as $r) {
                                            $label = $r['subject_name'] . ($r['subject_code'] ? ' ('.$r['subject_code'].')' : '');
                                            $subject_parts[] = $label;
                                            $time_parts[] = date('h:i A', strtotime($r['start_time'])) . ' - ' . date('h:i A', strtotime($r['end_time']));
                                            $marks_parts[] = $r['marks'] ?? '-';
                                        }
                                        // Unique values
                                        $time_parts = array_values(array_unique($time_parts));
                                        $marks_parts = array_values(array_unique($marks_parts));
                                        $date_display = date('d/m/Y', strtotime($dateKey));
                                        $days = [
                                            'Sunday' => 'রবিবার',
                                            'Monday' => 'সোমবার',
                                            'Tuesday' => 'মঙ্গলবার',
                                            'Wednesday' => 'বুধবার',
                                            'Thursday' => 'বৃহস্পতিবার',
                                            'Friday' => 'শুক্রবার',
                                            'Saturday' => 'শনিবার'
                                        ];
                                        $day_display = $days[date('l', strtotime($dateKey))];
                                        ?>
                                        <tr>
                                            <td><?php echo $date_display; ?></td>
                                            <td><?php echo $day_display; ?></td>
                                            <td style="text-align: left; padding-left: 10px;">
                                                <strong><?php echo implode(' / ', $subject_parts); ?></strong>
                                            </td>
                                            <td><?php echo !empty($time_parts) ? implode(' / ', $time_parts) : '-'; ?></td>
                                            <td><?php echo !empty($marks_parts) ? implode(' / ', $marks_parts) : '-'; ?></td>
                                            <?php if ($seat_arrangement): ?>
                                                <td><?php echo htmlspecialchars(trim((($seat_arrangement['room_number'] ?? '-') . ' / ' . ($seat_arrangement['seat_number'] ?? '-')))); ?></td>
                                            <?php endif; ?>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Instructions -->
                        <div class="instructions">
                            <h6 style="font-size: 0.85rem;"><i class="fas fa-info-circle"></i> নির্দেশাবলী:</h6>
                            <ul>
                                <li>পরীক্ষার্থীকে অবশ্যই এই এডমিট কার্ড সাথে আনতে হবে</li>
                                <li>পরীক্ষাগারে ঢোকার সময় অবশ্যই এই এডমিট কার্ড দেখাতে হবে</li>
                                <li>পরীক্ষার সময় সঠিক সময় মেনে চলতে হবে</li>
                            </ul>
                        </div>
                        
                        <!-- Signature Section -->
                        <div class="signature-section">
                            <div class="row">
                                <div class="col-md-4"><br>
                                    <p>পরীক্ষার্থীর স্বাক্ষর:</p>
                                </div>
                                <div class="col-md-4"><br>
                                    <p>পরীক্ষা নিরীক্ষকের স্বাক্ষর:</p>
                                </div>
                                <div class="col-md-4"><br>
                                    <p class="text-center">প্রধান শিক্ষক/পরীক্ষা নিরীক্ষক</p>
                                    
                                </div>
                            </div>
                        </div>
                    </div>
                <?php elseif ($exam_id && $student_id): ?>
                    <div class="alert alert-warning mt-3">
                        <i class="fas fa-exclamation-triangle"></i> পরীক্ষা বা ছাত্র/ছাত্রীর তথ্য খুঁজে পাওয়া যায়নি।
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Enable student dropdown when exam is selected and fetch students
        document.getElementById('exam_id').addEventListener('change', function() {
            const studentSelect = document.getElementById('student_id');
            studentSelect.disabled = !this.value;
            
            if (!this.value) {
                studentSelect.innerHTML = '<option value="">প্রথমে পরীক্ষা নির্বাচন করুন</option>';
                return;
            }
            
            // In a real implementation, you would make an AJAX call here to fetch students
            // For now, we'll just submit the form to reload the page with the exam selection
            // This will populate the student dropdown with the correct students
            this.form.submit();
        });
        
        // Auto-submit when both exam and student are selected
        document.getElementById('student_id').addEventListener('change', function() {
            if (this.value && document.getElementById('exam_id').value) {
                this.form.submit();
            }
        });
    </script>
</body>
</html>