<?php
session_start();

// Include access control functions
require_once '../includes/access_control.php';
require_once '../config/database.php';

// Check if user is logged in and is a teacher
require_teacher();

$user_role = $_SESSION['user_role'];
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'] ?? $_SESSION['username'] ?? 'শিক্ষক';

// Only teachers can access this page
if ($user_role !== 'teacher') {
    header('Location: ../index.php');
    exit();
}

// Get teacher ID
$teacher_id = null;
$stmt = $pdo->prepare("SELECT id FROM teachers WHERE user_id = ?");
$stmt->execute([$user_id]);
$teacher = $stmt->fetch();
if ($teacher) {
    $teacher_id = $teacher['id'];
} else {
    // Redirect if not a valid teacher
    header('Location: dashboard.php');
    exit();
}

$success_message = '';
$error_message = '';

// Get teacher's assigned classes
$assigned_classes = [];
if ($teacher_id) {
    $assigned_class_ids = get_teacher_assigned_classes($teacher_id);

    // Get detailed class information
    if (!empty($assigned_class_ids)) {
        $placeholders = str_repeat('?,', count($assigned_class_ids) - 1) . '?';
        $stmt = $pdo->prepare("SELECT DISTINCT c.id, c.name, c.section
            FROM classes c
            WHERE c.id IN ($placeholders) AND c.status = 'active'
            ORDER BY c.name, c.section
        ");
        $stmt->execute($assigned_class_ids);
        $assigned_classes = $stmt->fetchAll();
    }
}

// Get class IDs for filtering
$class_ids = array_column($assigned_classes, 'id');

// Get classes for dropdown (only assigned classes)
$classes = $assigned_classes;

// Get academic sessions
$stmt = $pdo->query("SELECT session_name FROM academic_sessions WHERE status = 'active' ORDER BY session_name DESC");
$sessions = $stmt->fetchAll();

// Get student groups
$stmt = $pdo->query("SELECT group_name, group_code, description FROM student_groups WHERE status = 'active' ORDER BY group_name");
$groups = $stmt->fetchAll();

// Get all subjects for the classes
$class_subjects_map = [];
if (!empty($class_ids)) {
    $placeholders = str_repeat('?,', count($class_ids) - 1) . '?';
    $stmt = $pdo->prepare("
        SELECT cs.class_id, cs.subject_id, s.name, s.code 
        FROM class_subjects cs 
        JOIN subjects s ON cs.subject_id = s.id 
        WHERE cs.class_id IN ($placeholders) AND s.status = 'active'
        ORDER BY s.name
    ");
    $stmt->execute($class_ids);
    $class_subjects_data = $stmt->fetchAll();
    foreach ($class_subjects_data as $cs) {
        $class_subjects_map[$cs['class_id']][] = $cs;
    }
}

// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $first_name = trim($_POST['first_name']);
    $last_name = trim($_POST['last_name'] ?? '');
    $father_name = trim($_POST['father_name'] ?? '');
    $mother_name = trim($_POST['mother_name'] ?? '');
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $address = trim($_POST['address']);
    $date_of_birth = $_POST['date_of_birth'];
    $gender = $_POST['gender'];
    $blood_group = $_POST['blood_group'] ?? null;
    $class_id = $_POST['class_id'];
    $session = $_POST['session'];
    $group_name = $_POST['group_name'];
    $roll_number = $_POST['roll_number'];
    $admission_date = $_POST['admission_date'];
    $guardian_name = trim($_POST['guardian_name']);
    $guardian_phone = trim($_POST['guardian_phone']);
    $guardian_email = trim($_POST['guardian_email']);
    $guardian_address = trim($_POST['guardian_address']);
    $status = $_POST['status'];
    $scholarship = $_POST['scholarship'] ?? 'no';
    $selected_subjects = $_POST['subjects'] ?? [];
    
    // Validation
    if (empty($first_name) || empty($class_id) || empty($session) || empty($roll_number)) {
        $error_message = 'প্রয়োজনীয় ক্ষেত্রগুলো পূরণ করুন (প্রথম নাম, ক্লাস, সেশন, রোল নম্বর)';
    } else if (!in_array($class_id, $class_ids)) {
        // Additional validation to ensure teacher can only assign student to their classes
        $error_message = 'আপনি এই ক্লাসে ছাত্র-ছাত্রী নিয়োগ করতে পারবেন না।';
    } else {
        try {
            $pdo->beginTransaction();
            
            // Check if roll number already exists in the class, session and group
            $stmt = $pdo->prepare("SELECT COUNT(*) FROM students WHERE class_id = ? AND session = ? AND COALESCE(group_name, '') = ? AND roll_number = ? AND status = 'active'");
            $stmt->execute([$class_id, $session, $group_name ?: '', $roll_number]);
            if ($stmt->fetchColumn() > 0) {
                $group_text = $group_name ? " ($group_name গ্রুপে)" : '';
                throw new Exception("এই ক্লাস ও সেশনে$group_text এই রোল নম্বর ইতিমধ্যে ব্যবহৃত হয়েছে");
            }
            
            // Generate student ID
            $stmt = $pdo->query("SELECT MAX(CAST(SUBSTRING(student_id, 3) AS UNSIGNED)) as max_id FROM students WHERE student_id LIKE 'ST%'");
            $result = $stmt->fetch();
            $new_id = ($result['max_id'] ?? 0) + 1;
            $student_id = 'ST' . str_pad($new_id, 4, '0', STR_PAD_LEFT);
            
            // Handle photo upload
            $photo_path = null;
            if (isset($_FILES['photo']) && $_FILES['photo']['error'] == 0) {
                $upload_dir = '../uploads/students/';
                if (!is_dir($upload_dir)) {
                    mkdir($upload_dir, 0755, true);
                }
                
                $file_extension = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
                $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];
                
                if (in_array($file_extension, $allowed_extensions)) {
                    $new_filename = 'student_' . $new_id . '_' . time() . '.' . $file_extension;
                    $upload_path = $upload_dir . $new_filename;
                    
                    if (move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path)) {
                        $photo_path = 'uploads/students/' . $new_filename;
                    }
                }
            }
            
            // Insert student
            $stmt = $pdo->prepare("
                INSERT INTO students 
                (student_id, first_name, last_name, father_name, mother_name, email, phone, address,
                date_of_birth, gender, blood_group, class_id, session, group_name, roll_number,
                admission_date, guardian_name, guardian_phone, guardian_email, guardian_address,
                status, scholarship, photo, created_at, updated_at)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
            ");
            
            $stmt->execute([
                $student_id, $first_name, $last_name, $father_name, $mother_name, $email, $phone, $address,
                $date_of_birth ?: null, $gender, $blood_group, $class_id, $session, $group_name ?: null,
                $roll_number, $admission_date ?: null, $guardian_name, $guardian_phone,
                $guardian_email, $guardian_address, $status, $scholarship, $photo_path
            ]);
            
            $new_student_id = $pdo->lastInsertId();
            
            // Insert student subjects
            if (!empty($selected_subjects)) {
                $stmt = $pdo->prepare("INSERT INTO student_subjects (student_id, subject_id, is_fourth_subject) VALUES (?, ?, ?)");
                foreach ($selected_subjects as $subject_id) {
                    $stmt->execute([$new_student_id, $subject_id, 0]);
                }
            }
            
            // Create user account for student
            $username = 'student_' . $new_id;
            $password = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 8);
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            
            $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role, created_at) VALUES (?, ?, ?, 'student', NOW())");
            $stmt->execute([$username, $hashed_password, $email ?: null]);
            $user_id = $pdo->lastInsertId();
            
            // Update student with user_id
            $stmt = $pdo->prepare("UPDATE students SET user_id = ? WHERE id = ?");
            $stmt->execute([$user_id, $new_student_id]);
            
            $pdo->commit();
            
            // Set success message with login details
            $success_message = "ছাত্র-ছাত্রী সফলভাবে যোগ হয়েছে। লগইন তথ্য: ইউজারনেম: $username, পাসওয়ার্ড: $password";
            
            // Reset form values
            $_POST = [];
            
        } catch (Exception $e) {
            $pdo->rollBack();
            $error_message = $e->getMessage();
        }
    }
}
?>

<!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="../assets/css/jessoreboard-theme.css" rel="stylesheet">
    <style>
        .form-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="<?php echo ($user_role == 'teacher') ? 'dashboard.php' : '../index.php'; ?>">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="bulk_upload_students.php">
                    <i class="fas fa-upload"></i> বাল্ক আপলোড
                </a>
                <a class="nav-link" href="<?php echo ($user_role == 'teacher') ? 'my_students.php' : '../students/list.php'; ?>">
                    <i class="fas fa-arrow-left"></i> ছাত্র-ছাত্রী তালিকা
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-user-plus"></i> নতুন ছাত্র-ছাত্রী যোগ</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($success_message): ?>
                            <div class="alert alert-success alert-dismissible fade show" role="alert">
                                <i class="fas fa-check-circle"></i> <?php echo $success_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>
                        <?php if ($error_message): ?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <i class="fas fa-exclamation-triangle"></i> <?php echo $error_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>
                        
                        <div class="mb-3">
                            <a href="bulk_upload_students.php" class="btn btn-success">
                                <i class="fas fa-upload"></i> বাল্ক আপলোড (একাধিক ছাত্র-ছাত্রী)
                            </a>
                            <small class="text-muted d-block mt-1">CSV ফাইল ব্যবহার করে একাধিক ছাত্র-ছাত্রী একসাথে যোগ করুন</small>
                        </div>

                        <form method="POST" action="" enctype="multipart/form-data" class="needs-validation" novalidate>
                            <div class="row">
                                <!-- Basic Information -->
                                <div class="col-md-12">
                                    <div class="form-section">
                                        <h6><i class="fas fa-user"></i> মৌলিক তথ্য</h6>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="first_name" class="form-label">নাম (প্রথম - ইংরেজিতে) <span class="text-danger">*</span></label>
                                                <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo htmlspecialchars($_POST['first_name'] ?? ''); ?>" required>
                                                <div class="form-text">ইংরেজিতে লিখুন (যেমন: John, Ahmed, etc)</div>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="last_name" class="form-label">নাম (শেষ - বাংলায়)</label>
                                                <input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo htmlspecialchars($_POST['last_name'] ?? ''); ?>">
                                                <div class="form-text">বাংলায় লিখুন (যেমন: রহমান, হাসান, etc) - ঐচ্ছিক</div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="father_name" class="form-label">পিতার নাম</label>
                                                <input type="text" class="form-control" id="father_name" name="father_name" value="<?php echo htmlspecialchars($_POST['father_name'] ?? ''); ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="mother_name" class="form-label">মাতার নাম</label>
                                                <input type="text" class="form-control" id="mother_name" name="mother_name" value="<?php echo htmlspecialchars($_POST['mother_name'] ?? ''); ?>">
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="date_of_birth" class="form-label">জন্ম তারিখ</label>
                                                <input type="date" class="form-control" id="date_of_birth" name="date_of_birth" value="<?php echo $_POST['date_of_birth'] ?? ''; ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="gender" class="form-label">লিঙ্গ</label>
                                                <select class="form-select" id="gender" name="gender">
                                                    <option value="">নির্বাচন করুন</option>
                                                    <option value="male" <?php echo (($_POST['gender'] ?? '') == 'male') ? 'selected' : ''; ?>>পুরুষ</option>
                                                    <option value="female" <?php echo (($_POST['gender'] ?? '') == 'female') ? 'selected' : ''; ?>>মহিলা</option>
                                                    <option value="other" <?php echo (($_POST['gender'] ?? '') == 'other') ? 'selected' : ''; ?>>অন্যান্য</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="blood_group" class="form-label">রক্তের গ্রুপ</label>
                                                <select class="form-select" id="blood_group" name="blood_group">
                                                    <option value="">নির্বাচন করুন</option>
                                                    <option value="A+" <?php echo (($_POST['blood_group'] ?? '') == 'A+') ? 'selected' : ''; ?>>A+</option>
                                                    <option value="A-" <?php echo (($_POST['blood_group'] ?? '') == 'A-') ? 'selected' : ''; ?>>A-</option>
                                                    <option value="B+" <?php echo (($_POST['blood_group'] ?? '') == 'B+') ? 'selected' : ''; ?>>B+</option>
                                                    <option value="B-" <?php echo (($_POST['blood_group'] ?? '') == 'B-') ? 'selected' : ''; ?>>B-</option>
                                                    <option value="O+" <?php echo (($_POST['blood_group'] ?? '') == 'O+') ? 'selected' : ''; ?>>O+</option>
                                                    <option value="O-" <?php echo (($_POST['blood_group'] ?? '') == 'O-') ? 'selected' : ''; ?>>O-</option>
                                                    <option value="AB+" <?php echo (($_POST['blood_group'] ?? '') == 'AB+') ? 'selected' : ''; ?>>AB+</option>
                                                    <option value="AB-" <?php echo (($_POST['blood_group'] ?? '') == 'AB-') ? 'selected' : ''; ?>>AB-</option>
                                                </select>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="photo" class="form-label">ছবি</label>
                                                <input type="file" class="form-control" id="photo" name="photo" accept="image/*">
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Contact Information -->
                                    <div class="form-section">
                                        <h6><i class="fas fa-phone"></i> যোগাযোগের তথ্য</h6>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="email" class="form-label">ইমেইল</label>
                                                <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="phone" class="form-label">ফোন</label>
                                                <input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>">
                                            </div>
                                        </div>
                                        <div class="mb-3">
                                            <label for="address" class="form-label">ঠিকানা</label>
                                            <textarea class="form-control" id="address" name="address" rows="2"><?php echo htmlspecialchars($_POST['address'] ?? ''); ?></textarea>
                                        </div>
                                    </div>

                                    <!-- Academic Information -->
                                    <div class="form-section">
                                        <h6><i class="fas fa-graduation-cap"></i> একাডেমিক তথ্য</h6>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="class_id" class="form-label">ক্লাস <span class="text-danger">*</span></label>
                                                <select class="form-select" id="class_id" name="class_id" required>
                                                    <option value="">নির্বাচন করুন</option>
                                                    <?php foreach ($classes as $class): ?>
                                                        <option value="<?php echo $class['id']; ?>" <?php echo (($_POST['class_id'] ?? '') == $class['id']) ? 'selected' : ''; ?>>
                                                            <?php echo htmlspecialchars($class['name'] . ' - ' . $class['section']); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="session" class="form-label">সেশন <span class="text-danger">*</span></label>
                                                <select class="form-select" id="session" name="session" required>
                                                    <option value="">নির্বাচন করুন</option>
                                                    <?php foreach ($sessions as $sess): ?>
                                                        <option value="<?php echo $sess['session_name']; ?>" <?php echo (($_POST['session'] ?? '') == $sess['session_name']) ? 'selected' : ''; ?>>
                                                            <?php echo htmlspecialchars($sess['session_name']); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="group_name" class="form-label">গ্রুপ</label>
                                                <select class="form-select" id="group_name" name="group_name">
                                                    <option value="">নির্বাচন করুন</option>
                                                    <option value="" <?php echo (($_POST['group_name'] ?? '') === '') ? 'selected' : ''; ?>>কোন গ্রুপ নেই</option>
                                                    <?php foreach ($groups as $group): ?>
                                                        <option value="<?php echo $group['group_name']; ?>" <?php echo (($_POST['group_name'] ?? '') == $group['group_name']) ? 'selected' : ''; ?>>
                                                            <?php echo htmlspecialchars($group['group_name']); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="roll_number" class="form-label">রোল নম্বর <span class="text-danger">*</span></label>
                                                <input type="text" class="form-control" id="roll_number" name="roll_number" value="<?php echo htmlspecialchars($_POST['roll_number'] ?? ''); ?>" required>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="admission_date" class="form-label">ভর্তি তারিখ</label>
                                                <input type="date" class="form-control" id="admission_date" name="admission_date" value="<?php echo $_POST['admission_date'] ?? date('Y-m-d'); ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="status" class="form-label">অবস্থা</label>
                                                <select class="form-select" id="status" name="status">
                                                    <option value="active" <?php echo (($_POST['status'] ?? 'active') == 'active') ? 'selected' : ''; ?>>সক্রিয়</option>
                                                    <option value="inactive" <?php echo (($_POST['status'] ?? '') == 'inactive') ? 'selected' : ''; ?>>নিষ্ক্রিয়</option>
                                                    <option value="graduated" <?php echo (($_POST['status'] ?? '') == 'graduated') ? 'selected' : ''; ?>>স্নাতক</option>
                                                    <option value="transferred" <?php echo (($_POST['status'] ?? '') == 'transferred') ? 'selected' : ''; ?>>স্থানান্তরিত</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="scholarship" class="form-label">উপবৃত্তি</label>
                                                <select class="form-select" id="scholarship" name="scholarship">
                                                    <option value="no" <?php echo (($_POST['scholarship'] ?? 'no') == 'no') ? 'selected' : ''; ?>>না</option>
                                                    <option value="yes" <?php echo (($_POST['scholarship'] ?? '') == 'yes') ? 'selected' : ''; ?>>হ্যাঁ</option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <!-- Subjects will be populated by JavaScript based on class selection -->
                                        <div id="subjects-container"></div>
                                    </div>

                                    <!-- Guardian Information -->
                                    <div class="form-section">
                                        <h6><i class="fas fa-user-shield"></i> অভিভাবকের তথ্য</h6>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="guardian_name" class="form-label">অভিভাবকের নাম</label>
                                                <input type="text" class="form-control" id="guardian_name" name="guardian_name" value="<?php echo htmlspecialchars($_POST['guardian_name'] ?? ''); ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="guardian_phone" class="form-label">অভিভাবকের ফোন</label>
                                                <input type="text" class="form-control" id="guardian_phone" name="guardian_phone" value="<?php echo htmlspecialchars($_POST['guardian_phone'] ?? ''); ?>">
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="guardian_email" class="form-label">অভিভাবকের ইমেইল</label>
                                                <input type="email" class="form-control" id="guardian_email" name="guardian_email" value="<?php echo htmlspecialchars($_POST['guardian_email'] ?? ''); ?>">
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label for="guardian_address" class="form-label">অভিভাবকের ঠিকানা</label>
                                                <textarea class="form-control" id="guardian_address" name="guardian_address" rows="2"><?php echo htmlspecialchars($_POST['guardian_address'] ?? ''); ?></textarea>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="d-flex justify-content-between">
                                        <a href="my_students.php" class="btn btn-secondary">
                                            <i class="fas fa-arrow-left"></i> তালিকায় ফিরুন
                                        </a>
                                        <button type="submit" class="btn btn-primary">
                                            <i class="fas fa-plus"></i> ছাত্র-ছাত্রী যোগ করুন
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Form validation
        (function () {
            'use strict'
            var forms = document.querySelectorAll('.needs-validation')
            Array.prototype.slice.call(forms)
                .forEach(function (form) {
                    form.addEventListener('submit', function (event) {
                        if (!form.checkValidity()) {
                            event.preventDefault()
                            event.stopPropagation()
                        }
                        form.classList.add('was-validated')
                    }, false)
                })
        })()
        
        // Populate subjects based on class selection
        document.getElementById('class_id').addEventListener('change', function() {
            var classId = this.value;
            var container = document.getElementById('subjects-container');
            container.innerHTML = '';
            
            if (classId) {
                // In a real implementation, we would fetch subjects via AJAX
                // For now, we'll just show a message
                container.innerHTML = '<div class="alert alert-info">বিষয়সমূহ স্বয়ংক্রিয়ভাবে পূরণ হবে যখন ফরমটি জমা দেওয়া হবে</div>';
            }
        });
    </script>
</body>
</html>