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

$session = $_GET['session'] ?? date('Y');
$exam_type = $_GET['exam_type'] ?? '';
$class_id = $_GET['class_id'] ?? '';
$section = $_GET['section'] ?? '';
$search = $_GET['search'] ?? '';

$stmt = $pdo->query("SELECT DISTINCT session FROM results ORDER BY session DESC");
$sessions = $stmt->fetchAll();
$stmt = $pdo->query("SELECT DISTINCT exam_type FROM results ORDER BY exam_type");
$exam_types = $stmt->fetchAll();
$stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name");
$classes = $stmt->fetchAll();
$stmt = $pdo->query("SELECT DISTINCT section FROM classes WHERE status = 'active' ORDER BY section");
$sections = $stmt->fetchAll(PDO::FETCH_COLUMN);

$sql = "SELECT DISTINCT s.id, s.student_id, s.first_name, s.last_name, s.roll_number, c.name as class_name, c.section, s.group_name, COUNT(DISTINCT r.subject_id) as subjects_count, r.session, r.exam_type FROM students s JOIN results r ON s.id = r.student_id JOIN classes c ON s.class_id = c.id WHERE s.status = 'active'";
$params = [];

if (!empty($session)) { $sql .= " AND r.session = ?"; $params[] = $session; }
if (!empty($exam_type)) { $sql .= " AND r.exam_type = ?"; $params[] = $exam_type; }
if (!empty($class_id)) { $sql .= " AND s.class_id = ?"; $params[] = $class_id; }
if (!empty($section)) { $sql .= " AND c.section = ?"; $params[] = $section; }
if (!empty($search)) { $q = "%$search%"; $sql .= " AND (s.first_name LIKE ? OR s.last_name LIKE ? OR s.student_id LIKE ? OR s.roll_number LIKE ?)"; $params = array_merge($params, [$q, $q, $q, $q]); }

$sql .= " GROUP BY s.id, s.student_id, s.first_name, s.last_name, s.roll_number, c.name, c.section, s.group_name, r.session, r.exam_type ORDER BY c.name, s.roll_number, s.first_name";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$students = $stmt->fetchAll();
$totalStudents = count($students);
?>
<!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="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>*{box-sizing:border-box}body{font-family:'Hind Siliguri',sans-serif;background:#f1f3f5;color:#333}.navbar{background:linear-gradient(135deg,#1a3a5c,#0d2137);box-shadow:0 4px 20px rgba(0,0,0,.15);border-bottom:3px solid #f42a41}.navbar-brand{color:#fff!important;font-weight:700;font-size:1.2rem}.page-header{text-align:center;margin:30px 0;padding:20px 0}.page-header h1{color:#1a3a5c;font-weight:700;margin-bottom:10px}.page-header p{color:#666;font-size:.95rem}.stats-box{background:linear-gradient(135deg,#1a3a5c,#0d2137);color:#fff;border-radius:12px;padding:25px;text-align:center;margin-bottom:30px;box-shadow:0 5px 15px rgba(0,0,0,.1)}.stats-box h2{font-size:2.8rem;font-weight:800;margin:0}.stats-box p{margin:5px 0 0;opacity:.9;font-size:.95rem}.filter-card{background:#fff;border-radius:15px;padding:20px;margin-bottom:25px;box-shadow:0 2px 10px rgba(0,0,0,.05);border-top:3px solid #1a3a5c}.filter-label{font-weight:600;color:#1a3a5c;font-size:.85rem;margin-bottom:6px}.form-control,.form-select{border-radius:8px;border:1px solid #ddd;font-size:.9rem;padding:8px 12px}.form-control:focus,.form-select:focus{border-color:#1a3a5c;box-shadow:0 0 0 .2rem rgba(26,58,92,.1)}.btn-filter{background:linear-gradient(135deg,#1a3a5c,#0d2137);color:#fff;border:none;padding:8px 24px;border-radius:8px;font-weight:600;transition:.3s}.btn-filter:hover{background:linear-gradient(135deg,#0d2137,#1a3a5c);color:#fff;transform:translateY(-2px)}.student-item{background:#fff;border-radius:12px;padding:16px;margin-bottom:12px;box-shadow:0 2px 8px rgba(0,0,0,.05);border-left:4px solid #1a3a5c;transition:.3s;display:flex;align-items:center;justify-content:space-between}.student-item:hover{transform:translateY(-2px);box-shadow:0 4px 15px rgba(0,0,0,.1)}.student-avatar{width:50px;height:50px;background:linear-gradient(135deg,#1a3a5c,#0d2137);border-radius:50%;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:700;font-size:1.3rem;margin-right:15px;flex-shrink:0}.student-details{flex:1}.student-name{font-weight:600;color:#1a3a5c;margin:0 0 4px}.student-meta{font-size:.8rem;color:#666;margin:3px 0}.badge-custom{display:inline-block;padding:3px 10px;border-radius:12px;font-size:.75rem;font-weight:600;margin-right:6px;margin-bottom:4px}.badge-id{background:#f0f7ff;color:#0066cc}.badge-roll{background:#f0fff4;color:#00a86b}.badge-subjects{background:#fff3e6;color:#e65100}.badge-class{background:#fce4ec;color:#c2185b}.btn-marksheet{background:#1a3a5c;color:#fff;border:none;padding:6px 16px;border-radius:6px;font-size:.85rem;font-weight:600;transition:.3s;text-decoration:none;display:inline-block}.btn-marksheet:hover{background:#0d2137;color:#fff;transform:scale(1.05)}.no-results{text-align:center;padding:80px 20px;color:#999}.no-results i{font-size:4rem;margin-bottom:20px;opacity:.3}.no-results h4{color:#666;font-weight:600}.no-results p{color:#999}.text-muted-sm{font-size:.8rem;color:#999;margin-top:15px}@media print{.navbar,.filter-card,.no-print{display:none!important}}</style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container">
            <a class="navbar-brand" href="index.php"><i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট</a>
            <div class="navbar-nav ms-auto"><a class="nav-link text-white" href="index.php"><i class="fas fa-home"></i> হোম</a></div>
        </div>
    </nav>

    <div class="container mt-5">
        <div class="page-header">
            <h1><i class="fas fa-list-alt"></i> রেজাল্ট তালিকা</h1>
            <p>সকল ছাত্র-ছাত্রীর রেজাল্ট তালিকা এবং মার্কশীট</p>
        </div>

        <div class="stats-box">
            <h2><?php echo $totalStudents; ?></h2>
            <p>মোট ছাত্র-ছাত্রী পাওয়া গেছে</p>
        </div>

        <div class="filter-card no-print">
            <form method="GET" action="">
                <div class="row g-2">
                    <div class="col-md-2">
                        <div class="filter-label"><i class="fas fa-calendar"></i> সেশন</div>
                        <select name="session" class="form-select" onchange="this.form.submit()">
                            <option value="">সব সেশন</option>
                            <?php foreach ($sessions as $s): ?>
                                <option value="<?php echo $s['session']; ?>" <?php echo $s['session'] == $session ? 'selected' : ''; ?>><?php echo $s['session']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <div class="filter-label"><i class="fas fa-file-alt"></i> পরীক্ষা</div>
                        <select name="exam_type" class="form-select" onchange="this.form.submit()">
                            <option value="">সব পরীক্ষা</option>
                            <?php foreach ($exam_types as $e): ?>
                                <option value="<?php echo $e['exam_type']; ?>" <?php echo $e['exam_type'] == $exam_type ? 'selected' : ''; ?>><?php echo ucfirst($e['exam_type']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <div class="filter-label"><i class="fas fa-school"></i> ক্লাস</div>
                        <select name="class_id" class="form-select" onchange="this.form.submit()">
                            <option value="">সব ক্লাস</option>
                            <?php foreach ($classes as $c): ?>
                                <option value="<?php echo $c['id']; ?>" <?php echo $c['id'] == $class_id ? 'selected' : ''; ?>><?php echo $c['name'] . ($c['section'] ? ' - ' . $c['section'] : ''); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <div class="filter-label"><i class="fas fa-project-diagram"></i> শাখা</div>
                        <select name="section" class="form-select" onchange="this.form.submit()">
                            <option value="">সব শাখা</option>
                            <?php foreach ($sections as $sec): ?>
                                <option value="<?php echo $sec; ?>" <?php echo $sec == $section ? 'selected' : ''; ?>><?php echo $sec; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <div class="filter-label"><i class="fas fa-search"></i> সার্চ</div>
                        <input type="text" name="search" class="form-control" placeholder="নাম, আইডি, রোল..." value="<?php echo htmlspecialchars($search); ?>">
                    </div>
                    <div class="col-md-1 d-flex align-items-end">
                        <button type="submit" class="btn btn-filter w-100"><i class="fas fa-search"></i> খুঁজুন</button>
                    </div>
                </div>
            </form>
        </div>

        <?php if (empty($students)): ?>
            <div class="no-results">
                <i class="fas fa-search"></i>
                <h4>কোন ছাত্র-ছাত্রী পাওয়া যায়নি</h4>
                <p>নির্বাচিত ফিল্টারের জন্য কোন রেজাল্ট নেই।</p>
            </div>
        <?php else: ?>
            <div class="row">
                <?php foreach ($students as $st): ?>
                    <div class="col-lg-12">
                        <div class="student-item">
                            <div style="display:flex;align-items:center;flex:1">
                                <div class="student-avatar"><?php echo strtoupper(substr($st['first_name'], 0, 1)); ?></div>
                                <div class="student-details">
                                    <div class="student-name"><?php echo htmlspecialchars($st['first_name'] . ' ' . $st['last_name']); ?></div>
                                    <div style="margin-top:5px">
                                        <span class="badge-custom badge-id"><i class="fas fa-id-card"></i> <?php echo $st['student_id']; ?></span>
                                        <span class="badge-custom badge-roll"><i class="fas fa-hashtag"></i> রোল: <?php echo $st['roll_number']; ?></span>
                                        <span class="badge-custom badge-subjects"><i class="fas fa-book"></i> <?php echo $st['subjects_count']; ?> বিষয়</span>
                                        <span class="badge-custom badge-class"><i class="fas fa-school"></i> <?php echo $st['class_name'] . ($st['section'] ? ' - ' . $st['section'] : ''); ?></span>
                                    </div>
                                    <?php if ($st['group_name']): ?>
                                        <div class="student-meta"><i class="fas fa-users"></i> গ্রুপ: <?php echo $st['group_name']; ?></div>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <a href="public_report.php?type=result&roll=<?php echo urlencode($st['roll_number']); ?>&class=<?php echo urlencode($st['class_name']); ?>&group=<?php echo urlencode($st['group_name'] ?? ''); ?>&session=<?php echo urlencode($st['session']); ?>&exam=<?php echo urlencode($st['exam_type']); ?>" class="btn-marksheet" target="_blank" title="মার্কশীট দেখুন"><i class="fas fa-file-pdf"></i> মার্কশীট</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <div class="text-muted-sm text-center no-print"><i class="fas fa-info-circle"></i> মার্কশীটে ক্লিক করলে নতুন ট্যাবে খুলবে</div>
    </div>

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