<?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 (prevent teachers from accessing admin pages)
if ($user_role !== 'teacher') {
    header('Location: ../index.php');
    exit();
}

// Get teacher ID if user is a teacher
$teacher_id = null;
if ($user_role == 'teacher') {
    $stmt = $pdo->prepare("SELECT id FROM teachers WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $teacher = $stmt->fetch();
    if ($teacher) {
        $teacher_id = $teacher['id'];
    }
}

// Get academic sessions for dropdown
$sessions = $pdo->query("SELECT id, session_name FROM academic_sessions WHERE status = 'active' ORDER BY is_current DESC, start_date DESC")->fetchAll();

// Get classes assigned to this teacher (both primary and assistant)
$assigned_classes = [];
if ($user_role == 'teacher' && $teacher_id) {
    $assigned_class_ids = get_teacher_assigned_classes($teacher_id);

    // Get detailed class information with teacher names
    if (!empty($assigned_class_ids)) {
        $placeholders = str_repeat('?,', count($assigned_class_ids) - 1) . '?';
        $stmt = $pdo->prepare("
            SELECT DISTINCT c.id, c.name, c.section, 'assigned' as assignment_type,
                   CONCAT(t.first_name, ' ', t.last_name) as teacher_name
            FROM classes c
            LEFT JOIN teachers t ON c.class_teacher_id = t.id
            WHERE c.id IN ($placeholders) AND c.status = 'active'
            ORDER BY c.name, c.section
        ");
        $stmt->execute($assigned_class_ids);
        $assigned_classes = $stmt->fetchAll();
    }
} else if ($user_role == 'admin') {
    // Admin can see all classes with their class teachers
    $stmt = $pdo->query("
        SELECT c.id, c.name, c.section, 'admin_view' as assignment_type,
               CONCAT(t.first_name, ' ', t.last_name) as teacher_name
        FROM classes c 
        LEFT JOIN teachers t ON c.class_teacher_id = t.id
        WHERE c.status = 'active' 
        ORDER BY c.name, c.section
    ");
    $assigned_classes = $stmt->fetchAll();
}

// Get statistics for the teacher's classes
$stats = [];

// Student statistics for teacher's classes
$class_ids = array_column($assigned_classes, 'id');

if (!empty($class_ids)) {
    $placeholders = str_repeat('?,', count($class_ids) - 1) . '?';
    $stmt = $pdo->prepare("
        SELECT 
            COUNT(*) as total_students,
            SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as active_students,
            SUM(CASE WHEN gender = 'male' THEN 1 ELSE 0 END) as male_students,
            SUM(CASE WHEN gender = 'female' THEN 1 ELSE 0 END) as female_students
        FROM students
        WHERE class_id IN ($placeholders)
    ");
    $stmt->execute($class_ids);
    $stats['students'] = $stmt->fetch();
} else {
    $stats['students'] = [
        'total_students' => 0,
        'active_students' => 0,
        'male_students' => 0,
        'female_students' => 0
    ];
}

// Get recent activities for teacher's classes
$recent_activities = [];
if (!empty($class_ids)) {
    $stmt = $pdo->prepare("
        SELECT 'student' as type, CONCAT(first_name, ' ', last_name) as name, students.created_at as date, class_id
        FROM students 
        WHERE class_id IN ($placeholders) AND students.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
        UNION ALL
        SELECT 'result' as type, 'মার্কস এন্ট্রি' as name, r.created_at as date, NULL as class_id
        FROM results r
        JOIN students s ON r.student_id = s.id
        WHERE s.class_id IN ($placeholders) AND r.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
        ORDER BY date DESC
        LIMIT 10
    ");
    
    $params = array_merge($class_ids, $class_ids);
    $stmt->execute($params);
    $recent_activities = $stmt->fetchAll();
}

// Get upcoming exams for teacher's classes
$upcoming_exams = [];
if (!empty($class_ids)) {
    $stmt = $pdo->prepare("
        SELECT e.*, c.name as class_name, c.section as class_section
        FROM exams e
        JOIN classes c ON e.class_id = c.id
        WHERE e.class_id IN ($placeholders) AND e.exam_date >= CURDATE() 
        ORDER BY e.exam_date ASC 
        LIMIT 5
    ");
    $stmt->execute($class_ids);
    $upcoming_exams = $stmt->fetchAll();
}

// Get recent marks entries for teacher's classes
$recent_marks = [];
if (!empty($class_ids)) {
    $stmt = $pdo->prepare("
        SELECT r.*, s.first_name, s.last_name, sub.name as subject_name, c.name as class_name
        FROM results r
        JOIN students s ON r.student_id = s.id
        JOIN subjects sub ON r.subject_id = sub.id
        JOIN classes c ON s.class_id = c.id
        WHERE s.class_id IN ($placeholders)
        ORDER BY r.created_at DESC
        LIMIT 5
    ");
    $stmt->execute($class_ids);
    $recent_marks = $stmt->fetchAll();
}

// Get teacher's routine
$teacher_routine = [];
if ($teacher_id) {
    $stmt = $pdo->prepare("
        SELECT t.*, 
               c.name as class_name, c.section as class_section,
               s.name as subject_name, s.code as subject_code
        FROM timetable t
        JOIN classes c ON t.class_id = c.id
        JOIN subjects s ON t.subject_id = s.id
        WHERE t.teacher_id = ?
        ORDER BY t.day_of_week, t.start_time
    ");
    $stmt->execute([$teacher_id]);
    $teacher_routine = $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="../assets/css/jessoreboard-theme.css" rel="stylesheet">
    <style>
        .dashboard-card {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 6px 20px rgba(0,103,71,0.2);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
            overflow: hidden;
            border-top: 3px solid #F42A41;
        }
        
        .dashboard-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 100%;
            height: 100%;
            background: rgba(255,255,255,0.1);
            border-radius: 50%;
            transition: all 0.3s ease;
        }
        
        .dashboard-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(0,103,71,0.3);
        }
        
        .dashboard-card:hover::before {
            top: -30%;
            right: -30%;
        }
        
        .dashboard-card.card-students {
            background: linear-gradient(135deg, #006747 0%, #00a86b 100%);
        }
        
        .dashboard-card.card-classes {
            background: linear-gradient(135deg, #1976d2 0%, #2196f3 100%);
        }
        
        .dashboard-card.card-exams {
            background: linear-gradient(135deg, #7b1fa2 0%, #9c27b0 100%);
        }
        
        .card-icon {
            font-size: 2.5rem;
            opacity: 0.9;
            margin-bottom: 12px;
            position: relative;
            z-index: 2;
        }
        
        .card-number {
            font-size: 2rem;
            font-weight: bold;
            margin-bottom: 8px;
            position: relative;
            z-index: 2;
        }
        
        .card-title {
            font-size: 1rem;
            opacity: 0.95;
            position: relative;
            z-index: 2;
        }
        
        .card-subtitle {
            font-size: 0.8rem;
            opacity: 0.8;
            margin-top: 3px;
            position: relative;
            z-index: 2;
        }
        
        .activity-item {
            background: white;
            border-radius: 8px;
            padding: 12px;
            margin-bottom: 8px;
            box-shadow: 0 2px 8px rgba(0,103,71,0.05);
            border-left: 3px solid #006747;
            transition: all 0.3s ease;
        }
        
        .activity-item:hover {
            transform: translateX(3px);
            box-shadow: 0 4px 15px rgba(0,103,71,0.1);
        }
        
        .activity-icon {
            width: 35px;
            height: 35px;
            background: linear-gradient(45deg, #006747, #004d35);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            margin-right: 12px;
            font-size: 0.9rem;
        }
        
        .quick-action-btn {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            border: 2px solid #006747;
            color: white;
            padding: 12px 18px;
            border-radius: 8px;
            font-weight: 600;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin: 4px;
            position: relative;
            overflow: hidden;
            font-size: 0.9rem;
        }
        
        .quick-action-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }
        
        .quick-action-btn:hover::before {
            left: 100%;
        }
        
        .quick-action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 103, 71, 0.4);
            color: white;
            border-color: #F42A41;
        }
        
        .welcome-section {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            text-align: center;
            position: relative;
            overflow: hidden;
            border-bottom: 4px solid #F42A41;
        }
        
        .welcome-section::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: float 6s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px) rotate(0deg); }
            50% { transform: translateY(-15px) rotate(180deg); }
        }
        
        .welcome-title {
            font-size: 1.6rem;
            font-weight: bold;
            margin-bottom: 8px;
            position: relative;
            z-index: 2;
        }
        
        .welcome-subtitle {
            font-size: 1rem;
            opacity: 0.95;
            position: relative;
            z-index: 2;
        }
        
        .info-card {
            background: white;
            border-radius: 12px;
            padding: 16px;
            box-shadow: 0 4px 15px rgba(0,103,71,0.1);
            margin-bottom: 15px;
            transition: all 0.3s ease;
            border-top: 3px solid #006747;
        }
        
        .info-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 20px rgba(0,103,71,0.15);
            border-top-color: #F42A41;
        }
        
        .progress-ring {
            width: 60px;
            height: 60px;
            margin: 0 auto 12px;
        }
        
        .progress-ring circle {
            fill: none;
            stroke-width: 6;
            stroke-linecap: round;
            transform: rotate(-90deg);
            transform-origin: 50% 50%;
        }
        
        .progress-ring .bg {
            stroke: #e9ecef;
        }
        
        .progress-ring .progress {
            stroke: #006747;
            stroke-dasharray: 0 251.2;
            transition: stroke-dasharray 0.3s;
        }
        
        .chart-container {
            position: relative;
            height: 250px;
            margin: 15px 0;
        }
        
        .notification-badge {
            position: absolute;
            top: -6px;
            right: -6px;
            background: #F42A41;
            color: white;
            border-radius: 50%;
            width: 18px;
            height: 18px;
            font-size: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }
        
        .class-list {
            max-height: 200px;
            overflow-y: auto;
        }
        
        .class-item {
            padding: 8px 12px;
            border-radius: 6px;
            margin-bottom: 6px;
            background-color: #f8f9fa;
            border-left: 3px solid #006747;
        }
        
        .routine-item {
            padding: 8px;
            border-bottom: 1px solid #eee;
        }
        
        .routine-item:last-child {
            border-bottom: none;
        }
    </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>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo ($user_role == 'teacher') ? 'dashboard.php' : '../index.php'; ?>">
                            <i class="fas fa-home"></i> হোম
                        </a>
                    </li>
                    
                    <!-- Teacher Menu Dropdown -->
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="teacherMenuDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-chalkboard-teacher"></i> শিক্ষক মেনু
                        </a>
                        <ul class="dropdown-menu" aria-labelledby="teacherMenuDropdown">
                            <li><a class="dropdown-item" href="dashboard.php"><i class="fas fa-tachometer-alt"></i> শিক্ষক ড্যাসবোর্ড</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="my_students.php"><i class="fas fa-user-graduate"></i> আমার ছাত্র-ছাত্রী</a></li>
                            <li><a class="dropdown-item" href="add_student.php"><i class="fas fa-user-plus"></i> নতুন ছাত্র ভর্তি</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="marks_entry.php"><i class="fas fa-edit"></i> মার্কস এন্ট্রি</a></li>
                            <li><a class="dropdown-item" href="my_reports.php"><i class="fas fa-chart-bar"></i> রিপোর্ট দেখুন</a></li>
                            <li><a class="dropdown-item" href="my_reports.php?report_type=tabulation"><i class="fas fa-table"></i> টেবুলেশন শীট</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="attendance.php"><i class="fas fa-calendar-check"></i> উপস্থিতি নিন</a></li>
                            <li><a class="dropdown-item" href="routine.php"><i class="fas fa-calendar-alt"></i> ক্লাস রুটিন</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="../subjects/subject_selection.php"><i class="fas fa-book"></i> বিষয় নির্বাচন</a></li>
                            <li><a class="dropdown-item" href="create_question_paper.php"><i class="fas fa-file-alt"></i> প্রশ্নপত্র তৈরি</a></li>
                            <li><a class="dropdown-item" href="question_bank.php"><i class="fas fa-book"></i> প্রশ্ন ব্যাংক</a></li>
                            <li><a class="dropdown-item" href="bulk_upload_students.php"><i class="fas fa-upload"></i> বাল্ক আপলোড</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="leave_application.php"><i class="fas fa-calendar-plus"></i> ছুটির আবেদন</a></li>
                        </ul>
                    </li>
                    
                    <?php if ($user_role == 'admin'): ?>
                    <li class="nav-item">
                        <a class="nav-link" href="list.php">
                            <i class="fas fa-list"></i> শিক্ষক তালিকা
                        </a>
                    </li>
                    <?php endif; ?>
                    <li class="nav-item">
                        <a class="nav-link" href="../logout.php" onclick="return confirm('আপনি কি লগআউট করতে চান?')">
                            <i class="fas fa-sign-out-alt"></i> লগআউট
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <!-- Content Header -->
        <div class="content-header mb-4">
            <h1 class="content-title">
                <i class="fas fa-chalkboard-teacher me-2"></i>
                শিক্ষক ড্যাশবোর্ড
            </h1>
            <p class="content-breadcrumb">
                <i class="fas fa-home me-1"></i>
                হোম / শিক্ষক ড্যাশবোর্ড
                <span class="ms-3 text-muted">
                    <i class="fas fa-clock me-1"></i>
                    <?php echo date('d F Y, l'); ?>
                </span>
            </p>
        </div>
        
        <!-- Content Body -->
        <div class="content-body">
            <!-- Welcome Section -->
            <div class="welcome-section fade-in">
                <h2 class="welcome-title">
                    স্বাগতম, <?php echo htmlspecialchars($user_name); ?>!
                </h2>
                <p class="welcome-subtitle">
                    আপনার ক্লাস এবং ছাত্র-ছাত্রীদের ব্যবস্থাপনা করুন
                </p>
                
                <?php if ($user_role == 'teacher' && $teacher_id): ?>
                    <?php 
                    $stmt = $pdo->prepare("SELECT * FROM teachers WHERE id = ?");
                    $stmt->execute([$teacher_id]);
                    $teacher_info = $stmt->fetch();
                    ?>
                    
                    <?php if (empty($assigned_classes)): ?>
                        <div class="alert alert-info mt-3">
                            <h5><i class="fas fa-info-circle"></i> স্বাগতম শিক্ষক ড্যাশবোর্ডে!</h5>
                            <p>আপনি এখনও কোন ক্লাসে নির্ধারিত হননি। প্রশাসক আপনাকে ক্লাস নির্ধারণ করার পর আপনি সেই ক্লাসের ছাত্র-ছাত্রীদের ব্যবস্থাপনা করতে পারবেন।</p>
                            <p class="mb-0">প্রশাসক আপনার জন্য ক্লাস নির্ধারণ করেননি। ক্লাস নির্ধারণ করতে প্রধান শিক্ষক বা অধ্যক্ষের সাথে যোগাযোগ করুন </p>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($teacher_info): ?>
                        <div class="mt-3 text-center">
                            <span class="badge bg-light text-dark me-2">
                                <i class="fas fa-id-badge me-1"></i>
                                আইডি: <?php echo htmlspecialchars($teacher_info['employee_id']); ?>
                            </span>
                            <span class="badge bg-light text-dark me-2">
                                <i class="fas fa-briefcase me-1"></i>
                                <?php echo htmlspecialchars($teacher_info['designation'] ?? 'শিক্ষক'); ?>
                            </span>
                            <span class="badge bg-light text-dark">
                                <i class="fas fa-calendar-plus me-1"></i>
                                যোগদান: <?php echo date('d/m/Y', strtotime($teacher_info['joining_date'])); ?>
                            </span>
                        </div>
                    <?php endif; ?>
                <?php elseif ($user_role == 'admin'): ?>
                    <?php if (empty($assigned_classes)): ?>
                        <div class="alert alert-warning mt-3">
                            <h5><i class="fas fa-exclamation-triangle"></i> কোন ক্লাস নির্ধারিত হয়নি</h5>
                            <p>আপনি এখনও কোন ক্লাস নির্ধারণ করেননি। শিক্ষকদের ক্লাস নির্ধারণ করতে প্রধান শিক্ষক বা অধ্যক্ষের সাথে যোগাযোগ করুন।</p>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
            
            <div class="row">
                <!-- Left Column -->
                <div class="col-lg-8">
                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-lg-6 col-md-6">
                            <div class="dashboard-card card-students slide-in">
                                <div class="card-icon">
                                    <i class="fas fa-user-graduate"></i>
                                </div>
                                <div class="card-number"><?php echo $stats['students']['active_students'] ?: 0; ?></div>
                                <div class="card-title">সক্রিয় ছাত্র-ছাত্রী</div>
                                <div class="card-subtitle">
                                    মোট: <?php echo $stats['students']['total_students'] ?: 0; ?> জন
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-lg-6 col-md-6">
                            <div class="dashboard-card card-classes slide-in">
                                <div class="card-icon">
                                    <i class="fas fa-school"></i>
                                </div>
                                <div class="card-number"><?php echo count($assigned_classes) ?: 0; ?></div>
                                <div class="card-title">আপনার ক্লাস</div>
                                <div class="card-subtitle">
                                    অ্যাসাইন করা ক্লাস
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Quick Actions -->
                    <div class="info-card">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="mb-0">
                                <i class="fas fa-bolt me-2 text-primary"></i>
                                দ্রুত কার্যক্রম
                            </h5>
                            <span class="badge bg-primary">জনপ্রিয়</span>
                        </div>
                        <div class="row">
                            <?php if (!empty($assigned_classes)): ?>
                                <!-- If teacher has only one class assigned, pre-select it in links -->
                                <?php 
                                $class_param = '';
                                if (count($assigned_classes) == 1) {
                                    $class_param = '?class_id=' . $assigned_classes[0]['id'];
                                }
                                ?>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="add_student.php<?php echo $class_param; ?>" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-user-plus d-block mb-2" style="font-size: 1.5rem;"></i>
                                        নতুন ছাত্র ভর্তি
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="marks_entry.php" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-edit d-block mb-2" style="font-size: 1.5rem;"></i>
                                        মার্কস এন্ট্রি
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="attendance.php" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-calendar-check d-block mb-2" style="font-size: 1.5rem;"></i>
                                        উপস্থিতি নিন
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="my_reports.php" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-chart-bar d-block mb-2" style="font-size: 1.5rem;"></i>
                                        রিপোর্ট দেখুন
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="my_students.php" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-list d-block mb-2" style="font-size: 1.5rem;"></i>
                                        ছাত্র তালিকা
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="my_reports.php?report_type=tabulation" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-table d-block mb-2" style="font-size: 1.5rem;"></i>
                                        টেবুলেশন শীট
                                    </a>
                                </div>
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <a href="../reports/transposed_tabulation_sheet.php" class="quick-action-btn w-100 text-center">
                                        <i class="fas fa-table d-block mb-2" style="font-size: 1.5rem;"></i>
                                        ট্রান্সপোজড টেবুলেশন শীট
                                    </a>
                                </div>
                                <?php if (!empty($assigned_classes)): ?>
                                    <div class="col-md-6 col-lg-4 mb-3">
                                        <a href="../subjects/subject_selection.php" class="quick-action-btn w-100 text-center">
                                            <i class="fas fa-book d-block mb-2" style="font-size: 1.5rem;"></i>
                                            বিষয় নির্বাচন
                                        </a>
                                    </div>
                                    <div class="col-md-6 col-lg-4 mb-3">
                                        <a href="bulk_upload_students.php" class="quick-action-btn w-100 text-center">
                                            <i class="fas fa-upload d-block mb-2" style="font-size: 1.5rem;"></i>
                                            বাল্ক আপলোড
                                        </a>
                                    </div>
                                    <div class="col-md-6 col-lg-4 mb-3">
                                        <a href="leave_application.php" class="quick-action-btn w-100 text-center">
                                            <i class="fas fa-calendar-plus d-block mb-2" style="font-size: 1.5rem;"></i>
                                            ছুটির আবেদন
                                        </a>
                                    </div>
                                <?php endif; ?>
                            <?php else: ?>
                                <div class="col-12">
                                    <div class="alert alert-info text-center">
                                        <i class="fas fa-info-circle"></i> আপনি এখনও কোন ক্লাসে নির্ধারিত হননি। প্রশাসক আপনাকে ক্লাস নির্ধারণ করার পর দ্রুত কার্যক্রমগুলো সক্রিয় হবে।
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Recent Marks -->
                    <div class="info-card">
                        <h5 class="mb-3">
                            <i class="fas fa-star me-2 text-warning"></i>
                            সাম্প্রতিক মার্কস
                        </h5>
                        <?php if (empty($recent_marks)): ?>
                            <p class="text-muted text-center">কোন মার্কস এন্ট্রি নেই</p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>ছাত্র-ছাত্রী</th>
                                            <th>বিষয়</th>
                                            <th>ক্লাস</th>
                                            <th>গ্রেড</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_marks as $mark): ?>
                                            <tr>
                                                <td><?php echo htmlspecialchars($mark['first_name'] . ' ' . $mark['last_name']); ?></td>
                                                <td><?php echo htmlspecialchars($mark['subject_name']); ?></td>
                                <td><?php echo htmlspecialchars($mark['class_name']); ?></td>
                                                <td>
                                                    <span class="badge bg-<?php echo $mark['grade'] == 'F' ? 'danger' : 'success'; ?>">
                                                        <?php echo $mark['grade']; ?>
                                                    </span>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- Right Column -->
                <div class="col-lg-4">
                    <!-- Assigned Classes -->
                    <div class="info-card">
                        <h5 class="mb-3">
                            <i class="fas fa-chalkboard me-2 text-success"></i>
                            আপনার ক্লাসসমূহ
                        </h5>
                        <div class="class-list">
                            <?php if (empty($assigned_classes)): ?>
                                <p class="text-muted text-center">কোন ক্লাস অ্যাসাইন করা হয়নি</p>
                            <?php else: ?>
                                <?php if ($user_role == 'admin'): ?>
                                    <div class="mb-2">
                                        <span class="badge bg-primary">প্রশাসক হিসাবে সব ক্লাস দেখতে পাচ্ছেন</span>
                                    </div>
                                <?php endif; ?>
                                <?php foreach ($assigned_classes as $class): ?>
                                    <div class="class-item">
                                        <strong><?php echo htmlspecialchars($class['name']); ?></strong> - <?php echo htmlspecialchars($class['section']); ?>
                                        <?php if (isset($class['assignment_type']) && $class['assignment_type'] == 'class_teacher'): ?>
                                            <span class="badge bg-success ms-2">ক্লাস শিক্ষক</span>
                                        <?php elseif (isset($class['assignment_type']) && $class['assignment_type'] == 'subject_teacher'): ?>
                                            <span class="badge bg-info ms-2">বিষয় শিক্ষক</span>
                                        <?php elseif ($user_role == 'admin'): ?>
                                            <span class="badge bg-primary ms-2">প্রশাসক অ্যাক্সেস</span>
                                        <?php endif; ?>
                                        <?php if (!empty($class['teacher_name'])): ?>
                                            <div class="small text-muted mt-1">ক্লাস শিক্ষক: <?php echo htmlspecialchars($class['teacher_name']); ?></div>
                                        <?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Recent Activities -->
                    <div class="info-card">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="mb-0">
                                <i class="fas fa-history me-2 text-success"></i>
                                সাম্প্রতিক কার্যক্রম
                            </h5>
                            <?php if (count($recent_activities) > 0): ?>
                                <span class="notification-badge"><?php echo count($recent_activities); ?></span>
                            <?php endif; ?>
                        </div>
                        <div style="max-height: 300px; overflow-y: auto;">
                            <?php if (empty($recent_activities)): ?>
                                <div class="text-center text-muted py-4">
                                    <i class="fas fa-inbox fa-3x mb-3 opacity-50"></i>
                                    <p>কোন সাম্প্রতিক কার্যক্রম নেই</p>
                                </div>
                            <?php else: ?>
                                <?php foreach ($recent_activities as $activity): ?>
                                    <div class="activity-item d-flex align-items-center">
                                        <div class="activity-icon">
                                            <i class="fas fa-<?php echo $activity['type'] == 'student' ? 'user-graduate' : 'chart-line'; ?>"></i>
                                        </div>
                                        <div>
                                            <div class="fw-bold"><?php echo htmlspecialchars($activity['name']); ?></div>
                                            <small class="text-muted">
                                                <?php echo $activity['type'] == 'student' ? 'নতুন ছাত্র যোগ হয়েছে' : 'মার্কস এন্ট্রি'; ?>
                                                <br>
                                                <i class="fas fa-clock me-1"></i>
                                                <?php echo date('d/m/Y H:i', strtotime($activity['date'])); ?>
                                            </small>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Upcoming Exams -->
                    <div class="info-card">
                        <h5 class="mb-3">
                            <i class="fas fa-calendar-alt me-2 text-info"></i>
                            আসন্ন পরীক্ষা
                        </h5>
                        <?php if (empty($upcoming_exams)): ?>
                            <p class="text-muted text-center">কোন আসন্ন পরীক্ষা নেই</p>
                        <?php else: ?>
                            <?php foreach ($upcoming_exams as $exam): ?>
                                <div class="d-flex justify-content-between align-items-center mb-2 p-2 bg-light rounded">
                                    <div>
                                        <small class="fw-bold"><?php echo htmlspecialchars($exam['name']); ?></small>
                                        <br>
                                        <small class="text-muted"><?php echo htmlspecialchars($exam['class_name'] . ' - ' . $exam['class_section']); ?></small>
                                    </div>
                                    <span class="badge bg-primary">
                                        <?php echo date('d/m/Y', strtotime($exam['exam_date'])); ?>
                                    </span>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Subject Assignments -->
                    <div class="info-card">
                        <h5 class="mb-3">
                            <i class="fas fa-book me-2 text-info"></i>
                            আপনার বিষয় নির্ধারণ
                        </h5>
                        <?php 
                        // Get subjects assigned to this teacher
                        $assigned_subjects = [];
                        if ($teacher_id) {
                            $stmt = $pdo->prepare("
                                SELECT DISTINCT s.name as subject_name, c.name as class_name, c.section
                                FROM class_subjects cs
                                JOIN subjects s ON cs.subject_id = s.id
                                JOIN classes c ON cs.class_id = c.id
                                WHERE cs.teacher_id = ? AND c.status = 'active' AND s.status = 'active'
                                ORDER BY c.name, s.name
                            ");
                            $stmt->execute([$teacher_id]);
                            $assigned_subjects = $stmt->fetchAll();
                        }
                        ?>
                        
                        <?php if (empty($assigned_subjects)): ?>
                            <p class="text-muted text-center">কোন বিষয় অ্যাসাইন করা হয়নি</p>
                        <?php else: ?>
                            <div style="max-height: 200px; overflow-y: auto;">
                                <?php foreach ($assigned_subjects as $assignment): ?>
                                    <div class="class-item">
                                        <strong><?php echo htmlspecialchars($assignment['subject_name']); ?></strong>
                                        <div class="small text-muted">
                                            <?php echo htmlspecialchars($assignment['class_name'] . ' - ' . $assignment['section']); ?>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Class Routine -->
                    <div class="info-card">
                        <h5 class="mb-3">
                            <i class="fas fa-calendar me-2 text-primary"></i>
                            আপনার ক্লাস রুটিন
                        </h5>
                        <?php if (empty($teacher_routine)): ?>
                            <p class="text-muted text-center">কোন রুটিন পাওয়া যায়নি</p>
                        <?php else: ?>
                            <div style="max-height: 300px; overflow-y: auto;">
                                <?php 
                                $days = [
                                    'sunday' => 'রবিবার',
                                    'monday' => 'সোমবার', 
                                    'tuesday' => 'মঙ্গলবার',
                                    'wednesday' => 'বুধবার',
                                    'thursday' => 'বৃহস্পতিবার',
                                    'friday' => 'শুক্রবার',
                                    'saturday' => 'শনিবার'
                                ];
                                
                                // Group routine by day
                                $routine_by_day = [];
                                foreach ($teacher_routine as $routine) {
                                    $day = $routine['day_of_week'];
                                    if (!isset($routine_by_day[$day])) {
                                        $routine_by_day[$day] = [];
                                    }
                                    $routine_by_day[$day][] = $routine;
                                }
                                
                                foreach ($routine_by_day as $day_key => $routines): ?>
                                    <div class="mb-3">
                                        <h6 class="text-primary"><?php echo $days[$day_key] ?? ucfirst($day_key); ?></h6>
                                        <?php foreach ($routines as $routine): ?>
                                            <div class="routine-item">
                                                <div class="d-flex justify-content-between">
                                                    <strong><?php echo htmlspecialchars($routine['subject_name']); ?></strong>
                                                    <span class="badge bg-success">
                                                        <?php echo date('g:i A', strtotime($routine['start_time'])); ?> - 
                                                        <?php echo date('g:i A', strtotime($routine['end_time'])); ?>
                                                    </span>
                                                </div>
                                                <small class="text-muted">
                                                    <?php echo htmlspecialchars($routine['class_name'] . ' - ' . $routine['class_section']); ?>
                                                    <?php if (!empty($routine['room_number'])): ?>
                                                        , রুম: <?php echo htmlspecialchars($routine['room_number']); ?>
                                                    <?php endif; ?>
                                                </small>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Add interactive animations
        document.addEventListener('DOMContentLoaded', function() {
            // Animate cards on scroll
            const observerOptions = {
                threshold: 0.1,
                rootMargin: '0px 0px -50px 0px'
            };
            
            const observer = new IntersectionObserver(function(entries) {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        entry.target.style.opacity = '1';
                        entry.target.style.transform = 'translateY(0)';
                    }
                });
            }, observerOptions);
            
            // Observe all cards
            document.querySelectorAll('.dashboard-card, .info-card').forEach(card => {
                card.style.opacity = '0';
                card.style.transform = 'translateY(20px)';
                card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
                observer.observe(card);
            });
            
            // Add hover effects to quick action buttons
            document.querySelectorAll('.quick-action-btn').forEach(btn => {
                btn.addEventListener('mouseenter', function() {
                    this.style.transform = 'translateY(-3px) scale(1.02)';
                });
                
                btn.addEventListener('mouseleave', function() {
                    this.style.transform = 'translateY(0) scale(1)';
                });
            });
        });
    </script>
</body>
</html>