<?php
session_start();

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

require_once '../config/database.php';

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

if (!$student) {
    header('Location: ../login.php');
    exit();
}

// Get student subjects
$subjects = [];
$no_fourth_classes = [6, 7, 8];
$is_no_fourth_class = in_array((int)$student['class_id'], $no_fourth_classes);

$subject_stmt = $pdo->prepare("
    SELECT sub.*, ss.is_fourth_subject
    FROM student_subjects ss 
    JOIN subjects sub ON ss.subject_id = sub.id 
    WHERE ss.student_id = ?
    ORDER BY sub.name
");
$subject_stmt->execute([$student['id']]);
$subjects = $subject_stmt->fetchAll();

// Class 6,7,8: override is_fourth_subject to 0
if ($is_no_fourth_class) {
    foreach ($subjects as &$sub) { $sub['is_fourth_subject'] = 0; }
    unset($sub);
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>বিষয়সমূহ - <?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></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-color: #f8f9fa;
        }
        .subject-card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .fourth-subject {
            border-left: 4px solid #FF6B6B;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="../student_dashboard.php"><i class="fas fa-arrow-left"></i> ছাত্র ড্যাশবোর্ড</a>
            <div class="navbar-nav ms-auto">
                <span class="navbar-text me-3">
                    <i class="fas fa-user-graduate"></i> <?php echo htmlspecialchars($_SESSION['user_name']); ?>
                </span>
                <a class="nav-link" href="../logout.php">
                    <i class="fas fa-sign-out-alt"></i> লগআউট
                </a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-12">
                <h2><i class="fas fa-book"></i> নির্ধারিত বিষয়সমূহ</h2>
                <p>ছাত্র নাম: <strong><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></strong> | 
                   ছাত্র আইডি: <strong><?php echo htmlspecialchars($student['student_id']); ?></strong> |
                   ক্লাস: <strong><?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['class_section']); ?></strong></p>
            </div>
        </div>

        <?php if (!empty($subjects)): ?>
            <div class="row">
                <?php foreach ($subjects as $subject): ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="card subject-card <?php echo $subject['is_fourth_subject'] ? 'fourth-subject' : ''; ?>">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <i class="fas fa-book-open text-primary"></i> 
                                    <?php echo htmlspecialchars($subject['name']); ?>
                                    <?php if ($subject['is_fourth_subject']): ?>
                                        <span class="badge bg-danger float-end">৪র্থ বিষয়</span>
                                    <?php endif; ?>
                                </h5>
                                <p class="card-text">
                                    <strong>কোড:</strong> <?php echo htmlspecialchars($subject['code']); ?><br>
                                    <strong>ধরন:</strong> 
                                    <?php 
                                        if ($subject['is_fourth_subject']) {
                                            echo 'ঐচ্ছিক (৪র্থ বিষয়)';
                                        } else {
                                            echo $subject['type'] ?? 'সাধারণ';
                                        }
                                    ?><br>
                                    <strong>বর্ণনা:</strong> <?php echo htmlspecialchars($subject['description'] ?? 'N/A'); ?>
                                </p>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle"></i> এখনও কোন বিষয় নির্ধারণ করা হয়নি।
            </div>
        <?php endif; ?>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>