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

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

// Get user info
$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['user_role'];
$user_name = $_SESSION['user_name'];

// Get system name for branding
$system_name = get_system_name($pdo);

// Get dashboard statistics
$stats = [];

// Student statistics
$stmt = $pdo->query("
    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
");
$stats['students'] = $stmt->fetch();

// Teacher statistics
$stmt = $pdo->query("
    SELECT 
        COUNT(*) as total_teachers,
        SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as active_teachers
    FROM teachers
");
$stats['teachers'] = $stmt->fetch();

// Class statistics
$stmt = $pdo->query("
    SELECT 
        COUNT(*) as total_classes,
        SUM(capacity) as total_capacity
    FROM classes WHERE status = 'active'
");
$stats['classes'] = $stmt->fetch();

// Recent activities
$stmt = $pdo->query("
    SELECT 'student' as type, CONCAT(first_name, ' ', last_name) as name, created_at as date
    FROM students 
    WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
    UNION ALL
    SELECT 'teacher' as type, CONCAT(first_name, ' ', last_name) as name, created_at as date
    FROM teachers 
    WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
    ORDER BY date DESC
    LIMIT 10
");
$recent_activities = $stmt->fetchAll();
?>

<!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($system_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="assets/css/jessoreboard-theme.css" rel="stylesheet">
    <style>
        .dashboard-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(0,0,0,0.2);
        }
        
        .dashboard-card.card-students {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .dashboard-card.card-teachers {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        
        .dashboard-card.card-classes {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }
        
        .dashboard-card.card-exams {
            background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
        }
        
        .card-icon {
            font-size: 3rem;
            opacity: 0.8;
            margin-bottom: 15px;
        }
        
        .card-number {
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .card-title {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .activity-item {
            background: white;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            border-left: 4px solid #667eea;
        }
        
        .activity-icon {
            width: 40px;
            height: 40px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            margin-right: 15px;
        }
        
        .quick-action-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
            padding: 15px 25px;
            border-radius: 10px;
            font-weight: 600;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin: 5px;
        }
        
        .quick-action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
            color: white;
        }
        
        .welcome-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            text-align: center;
        }
        
        .welcome-title {
            font-size: 2rem;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .welcome-subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }
    </style>
</head>
<body>
    <div class="sidebar-container">
        <?php echo renderSidebar('index_with_sidebar.php', $user_role, $user_name); ?>
        
        <div class="main-content">
            <!-- Content Header -->
            <div class="content-header">
                <h1 class="content-title">
                    <i class="fas fa-tachometer-alt me-2"></i>
                    ড্যাশবোর্ড
                </h1>
                <p class="content-breadcrumb">
                    <i class="fas fa-home me-1"></i>
                    হোম / ড্যাশবোর্ড
                </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">
                        <?php echo htmlspecialchars($site_name); ?> ম্যানেজমেন্ট সিস্টেমে আপনাকে স্বাগতম
                    </p>
                </div>
                
                <!-- Statistics Cards -->
                <div class="row mb-4">
                    <div class="col-lg-3 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>
                    </div>
                    
                    <div class="col-lg-3 col-md-6">
                        <div class="dashboard-card card-teachers slide-in">
                            <div class="card-icon">
                                <i class="fas fa-chalkboard-teacher"></i>
                            </div>
                            <div class="card-number"><?php echo $stats['teachers']['active_teachers'] ?: 0; ?></div>
                            <div class="card-title">সক্রিয় শিক্ষক</div>
                        </div>
                    </div>
                    
                    <div class="col-lg-3 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 $stats['classes']['total_classes'] ?: 0; ?></div>
                            <div class="card-title">মোট ক্লাস</div>
                        </div>
                    </div>
                    
                    <div class="col-lg-3 col-md-6">
                        <div class="dashboard-card card-exams slide-in">
                            <div class="card-icon">
                                <i class="fas fa-clipboard-list"></i>
                            </div>
                            <div class="card-number"><?php echo $stats['classes']['total_capacity'] ?: 0; ?></div>
                            <div class="card-title">মোট আসন</div>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <!-- Quick Actions -->
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header bg-primary text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-bolt me-2"></i>
                                    দ্রুত কার্যক্রম
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <?php if ($user_role == 'admin' || $user_role == 'teacher'): ?>
                                    <div class="col-md-6 mb-3">
                                        <a href="students/add.php" class="quick-action-btn w-100">
                                            <i class="fas fa-user-plus me-2"></i>
                                            নতুন ছাত্র ভর্তি
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="marks/entry.php" class="quick-action-btn w-100">
                                            <i class="fas fa-edit me-2"></i>
                                            মার্কস এন্ট্রি
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="attendance/mark.php" class="quick-action-btn w-100">
                                            <i class="fas fa-calendar-check me-2"></i>
                                            উপস্থিতি নিন
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="reports/marksheet.php" class="quick-action-btn w-100">
                                            <i class="fas fa-certificate me-2"></i>
                                            মার্কশীট তৈরি
                                        </a>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <div class="col-md-6 mb-3">
                                        <a href="students/list.php" class="quick-action-btn w-100">
                                            <i class="fas fa-list me-2"></i>
                                            ছাত্র তালিকা
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="reports/tabulation_sheet.php" class="quick-action-btn w-100">
                                            <i class="fas fa-table me-2"></i>
                                            টেবুলেশন শীট
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="reports/transposed_tabulation_sheet.php" class="quick-action-btn w-100">
                                            <i class="fas fa-table me-2"></i>
                                            ট্রান্সপোজড টেবুলেশন শীট
                                        </a>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <a href="reports/merit_position_bulk_print.php" class="quick-action-btn w-100">
                                            <i class="fas fa-trophy me-2"></i>
                                            মেরিট পজিশন বাল্ক প্রিন্ট
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Recent Activities -->
                    <div class="col-lg-4">
                        <div class="card">
                            <div class="card-header bg-success text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-history me-2"></i>
                                    সাম্প্রতিক কার্যক্রম
                                </h5>
                            </div>
                            <div class="card-body" style="max-height: 400px; overflow-y: auto;">
                                <?php if (empty($recent_activities)): ?>
                                    <p class="text-muted text-center">কোন সাম্প্রতিক কার্যক্রম নেই</p>
                                <?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' : 'chalkboard-teacher'; ?>"></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>
                    </div>
                </div>
                
                <!-- Additional Info -->
                <div class="row mt-4">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header bg-info text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-info-circle me-2"></i>
                                    সিস্টেম তথ্য
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-3">
                                        <div class="text-center">
                                            <i class="fas fa-users fa-2x text-primary mb-2"></i>
                                            <h6>মোট ছাত্র-ছাত্রী</h6>
                                            <h4 class="text-primary"><?php echo $stats['students']['total_students'] ?: 0; ?></h4>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="text-center">
                                            <i class="fas fa-male fa-2x text-info mb-2"></i>
                                            <h6>ছেলে</h6>
                                            <h4 class="text-info"><?php echo $stats['students']['male_students'] ?: 0; ?></h4>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="text-center">
                                            <i class="fas fa-female fa-2x text-danger mb-2"></i>
                                            <h6>মেয়ে</h6>
                                            <h4 class="text-danger"><?php echo $stats['students']['female_students'] ?: 0; ?></h4>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="text-center">
                                            <i class="fas fa-chalkboard-teacher fa-2x text-success mb-2"></i>
                                            <h6>মোট শিক্ষক</h6>
                                            <h4 class="text-success"><?php echo $stats['teachers']['total_teachers'] ?: 0; ?></h4>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </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 some 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, .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>