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

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

$user_role = $_SESSION['user_role'];
$user_name = $_SESSION['user_name'];

// Get 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();

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

// Recent students
$stmt = $pdo->query("
    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.status = 'active' 
    ORDER BY s.created_at DESC 
    LIMIT 5
");
$recent_students = $stmt->fetchAll();

// Get classes for dropdown
$classes_stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name, section");
$classes = $classes_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">
    <link href="../../assets/css/sidebar.css" rel="stylesheet">
    <style>
        .dashboard-card {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: white;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 10px 30px rgba(0,103,71,0.2);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
            overflow: hidden;
            border-top: 4px 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(-5px);
            box-shadow: 0 15px 40px 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%);
        }
        
        .quick-action-btn {
            display: block;
            padding: 15px;
            margin-bottom: 15px;
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 10px;
            text-align: center;
            transition: all 0.3s ease;
            text-decoration: none;
            color: #495057;
        }
        
        .quick-action-btn:hover {
            background: #006747;
            color: white;
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0,103,71,0.2);
        }
        
        .quick-action-btn i {
            font-size: 1.5rem;
            margin-bottom: 10px;
            display: block;
        }
        
        .student-photo {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
        }
        
        .no-photo {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #6c757d;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
    </style>
</head>
<body>
    <div class="sidebar-container">
        <?php echo renderSidebar('index_with_sidebar.php', $user_role, $user_name); ?>
        <div class="main-content">
            <div class="container-fluid mt-4">
                <!-- Page Header -->
                <div class="row mb-4">
                    <div class="col">
                        <h2><i class="fas fa-user-graduate"></i> ছাত্র-ছাত্রী পরিচালনা</h2>
                        <p class="text-muted">স্কুলের সব ছাত্র-ছাত্রী সম্পর্কিত তথ্য ও পরিচালনা</p>
                    </div>
                </div>
                
                <!-- Statistics Cards -->
                <div class="row mb-4">
                    <div class="col-md-6 col-lg-3">
                        <div class="dashboard-card card-students">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h3><?php echo $stats['students']['active_students'] ?: 0; ?></h3>
                                    <p class="mb-0">সক্রিয় ছাত্র-ছাত্রী</p>
                                </div>
                                <i class="fas fa-users fa-2x"></i>
                            </div>
                            <div class="mt-3">
                                <small>মোট: <?php echo $stats['students']['total_students'] ?: 0; ?> জন</small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-lg-3">
                        <div class="dashboard-card card-classes">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h3><?php echo $stats['classes']['total_classes'] ?: 0; ?></h3>
                                    <p class="mb-0">মোট ক্লাস</p>
                                </div>
                                <i class="fas fa-school fa-2x"></i>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-lg-3">
                        <div class="dashboard-card" style="background: linear-gradient(135deg, #d32f2f 0%, #F42A41 100%);">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h3><?php echo $stats['students']['male_students'] ?: 0; ?></h3>
                                    <p class="mb-0">ছেলে</p>
                                </div>
                                <i class="fas fa-mars fa-2x"></i>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-lg-3">
                        <div class="dashboard-card" style="background: linear-gradient(135deg, #f57c00 0%, #ff9800 100%);">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h3><?php echo $stats['students']['female_students'] ?: 0; ?></h3>
                                    <p class="mb-0">মেয়ে</p>
                                </div>
                                <i class="fas fa-venus fa-2x"></i>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <!-- Quick Actions -->
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header">
                                <h5><i class="fas fa-bolt"></i> দ্রুত কার্যক্রম</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-4">
                                        <a href="../add.php" class="quick-action-btn">
                                            <i class="fas fa-user-plus"></i>
                                            <span>নতুন ছাত্র-ছাত্রী</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../list.php" class="quick-action-btn">
                                            <i class="fas fa-list"></i>
                                            <span>সব ছাত্র-ছাত্রী</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="search.php" class="quick-action-btn">
                                            <i class="fas fa-search"></i>
                                            <span>অনুসন্ধান</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../students_with_subjects.php" class="quick-action-btn">
                                            <i class="fas fa-book-reader"></i>
                                            <span>বিষয়সহ তালিকা</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../bulk_id_card.php" class="quick-action-btn">
                                            <i class="fas fa-id-card"></i>
                                            <span>আইডি কার্ড</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../manage_fourth_subject.php" class="quick-action-btn">
                                            <i class="fas fa-star"></i>
                                            <span>চতুর্থ বিষয়</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../../fees/dashboard.php" class="quick-action-btn">
                                            <i class="fas fa-money-bill-wave"></i>
                                            <span>ফি ব্যবস্থাপনা</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../../exams/manage.php" class="quick-action-btn">
                                            <i class="fas fa-clipboard-list"></i>
                                            <span>পরীক্ষা ব্যবস্থাপনা</span>
                                        </a>
                                    </div>
                                    <div class="col-md-4">
                                        <a href="../../reports/marksheet.php" class="quick-action-btn">
                                            <i class="fas fa-file-signature"></i>
                                            <span>মার্কশীট ও টেবুলেশনশীট</span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Recent Students -->
                        <div class="card mt-4">
                            <div class="card-header">
                                <h5><i class="fas fa-history"></i> সাম্প্রতিক ভর্তি</h5>
                            </div>
                            <div class="card-body">
                                <?php if (empty($recent_students)): ?>
                                    <div class="text-center py-4">
                                        <i class="fas fa-user-graduate fa-3x text-muted mb-3"></i>
                                        <p class="text-muted">কোন ছাত্র-ছাত্রী খুঁজে পাওয়া যায়নি</p>
                                    </div>
                                <?php else: ?>
                                    <div class="table-responsive">
                                        <table class="table table-hover">
                                            <thead>
                                                <tr>
                                                    <th>ছবি</th>
                                                    <th>নাম</th>
                                                    <th>ক্লাস</th>
                                                    <th>রোল</th>
                                                    <th>তারিখ</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($recent_students as $student): ?>
                                                    <tr>
                                                        <td>
                                                            <?php if ($student['photo']): ?>
                                                                <img src="../../<?php echo $student['photo']; ?>" alt="<?php echo $student['first_name']; ?>" class="student-photo">
                                                            <?php else: ?>
                                                                <div class="no-photo">
                                                                    <i class="fas fa-user"></i>
                                                                </div>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td>
                                                            <strong><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></strong>
                                                            <br><small class="text-muted"><?php echo $student['student_id']; ?></small>
                                                        </td>
                                                        <td>
                                                            <?php if ($student['class_name']): ?>
                                                                <span class="badge bg-primary"><?php echo $student['class_name'] . ' - ' . $student['class_section']; ?></span>
                                                            <?php else: ?>
                                                                <span class="text-muted">-</span>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td>
                                                            <span class="badge bg-info"><?php echo $student['roll_number']; ?></span>
                                                        </td>
                                                        <td>
                                                            <?php echo date('d/m/Y', strtotime($student['created_at'])); ?>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Additional Information -->
                    <div class="col-lg-4">
                        <div class="card">
                            <div class="card-header">
                                <h5><i class="fas fa-info-circle"></i> তথ্য</h5>
                            </div>
                            <div class="card-body">
                                <h6><i class="fas fa-tasks"></i> ক্লাস তালিকা</h6>
                                <ul class="list-group list-group-flush mb-4">
                                    <?php foreach ($classes as $class): ?>
                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                            <?php echo $class['name'] . ' - ' . $class['section']; ?>
                                            <span class="badge bg-primary rounded-pill">Active</span>
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                                
                                <h6><i class="fas fa-cog"></i> সেটিংস</h6>
                                <div class="list-group">
                                    <a href="../admin/settings.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-cog me-2"></i> সাধারণ সেটিংস
                                    </a>
                                    <a href="../admin/sslcommerz_settings.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-credit-card me-2"></i> পেমেন্ট সেটিংস
                                    </a>
                                    <a href="../admin/gallery.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-images me-2"></i> গ্যালারি
                                    </a>
                                </div>
                            </div>
                        </div>
                        
                        <div class="card mt-4">
                            <div class="card-header">
                                <h5><i class="fas fa-upload"></i> বাল্ক অপারেশন</h5>
                            </div>
                            <div class="card-body">
                                <div class="list-group">
                                    <a href="../../bulk_upload/students_upload_bengali.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-file-upload me-2"></i> ছাত্র-ছাত্রী আপলোড
                                    </a>
                                    <a href="../../bulk_upload/students_with_subjects_upload.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-file-import me-2"></i> ছাত্র-ছাত্রী ও বিষয় আপলোড
                                    </a>
                                    <a href="../bulk_update.php" class="list-group-item list-group-item-action">
                                        <i class="fas fa-edit me-2"></i> বাল্ক আপডেট
                                    </a>
                                </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 src="../../assets/js/script.js"></script>
</body>
</html>