<?php
// my-quizzes.php - Student Quizzes & Files Page
require_once 'includes/config.php';
require_once 'includes/site-data.php';

if(!isset($_SESSION['user_id'])) { header('Location: auth.php'); exit; }
$user_id = $_SESSION['user_id'];

// Get enrolled courses with quizzes and files
$stmt = $pdo->prepare("
    SELECT c.id as course_id, c.title_bn as course_name,
           l.id as lesson_id, l.title_bn as lesson_title, l.order_number,
           (SELECT COUNT(*) FROM quizzes WHERE lesson_id = l.id) as quiz_count,
           l.attachment_file, l.attachment_name
    FROM enrollments e
    JOIN courses c ON e.course_id = c.id
    JOIN lessons l ON c.id = l.course_id
    WHERE e.user_id = ? AND e.payment_status = 'completed'
    AND ((SELECT COUNT(*) FROM quizzes WHERE lesson_id = l.id) > 0 OR l.attachment_file IS NOT NULL)
    ORDER BY c.title_bn, l.order_number
");
$stmt->execute([$user_id]);
$items = $stmt->fetchAll();

// Dynamic
$st=$S['site_title']??'ONESBD LMS';$lt=$S['logo_text']??'ONESBD LMS';$li=$S['logo_icon']??'fa-graduation-cap';
$pc=$S['primary_color']??'#6C5CE7';$ui=$S['use_image_logo']??'0';$lim=$S['logo_image']??'';$ft=$S['footer_text']??'';
?>
<!DOCTYPE html>
<html lang="bn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>কুইজ ও ফাইল - <?=htmlspecialchars($st)?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root{--primary:<?=$pc?>;--g1:linear-gradient(135deg,<?=$pc?>,<?=$pc?>dd);--dark:#1a1a2e;--shadow:0 4px 20px rgba(0,0,0,.06)}
body{background:#f0f2f8;font-family:'Segoe UI',Arial,sans-serif}
.navbar{background:white;box-shadow:0 2px 10px rgba(0,0,0,.06);padding:12px 0}
.navbar-brand{font-weight:800;font-size:1.2rem;background:var(--g1);-webkit-background-clip:text;-webkit-text-fill-color:transparent;text-decoration:none;display:flex;align-items:center;gap:8px}
.navbar-brand img{height:34px}
.container{max-width:1000px;margin:0 auto;padding:20px}
.page-header{background:var(--g1);color:white;border-radius:20px;padding:28px;margin-bottom:20px}
.main-card{background:white;border-radius:16px;padding:22px;box-shadow:var(--shadow);margin-bottom:18px}
.item-card{display:flex;align-items:center;padding:14px 18px;border:1px solid #eee;border-radius:12px;margin-bottom:8px;transition:.3s;text-decoration:none;color:#555;gap:12px}
.item-card:hover{box-shadow:var(--shadow);color:var(--primary)}
.item-icon{width:45px;height:45px;border-radius:12px;display:flex;align-items:center;justify-content:center;color:white;font-size:1.2rem;flex-shrink:0}
.course-group{margin-bottom:20px}
.course-group h6{font-weight:700;color:var(--primary);margin-bottom:10px;padding-bottom:8px;border-bottom:2px solid #e8eaef}
.btn{font-weight:600;border-radius:50px;padding:8px 20px;text-decoration:none;display:inline-flex;align-items:center;gap:6px;font-size:.85rem}
.btn-primary{background:var(--g1);color:white}.btn-outline{border:2px solid var(--primary);color:var(--primary)}
.empty-state{text-align:center;padding:50px;color:#888}
</style>
</head>
<body>
<nav class="navbar"><div class="container" style="display:flex;justify-content:space-between;align-items:center">
    <a href="index.php" class="navbar-brand"><?php if($ui=='1'&&!empty($lim)&&file_exists('uploads/logo/'.$lim)):?><img src="uploads/logo/<?=$lim?>" alt=""><?php else:?><i class="fas <?=$li?> me-2"></i><?=htmlspecialchars($lt)?><?php endif;?></a>
    <a href="dashboard.php" class="btn btn-outline btn-sm"><i class="fas fa-arrow-left me-1"></i> ড্যাশবোর্ড</a>
</div></nav>

<div class="container">
    <div class="page-header"><h4><i class="fas fa-tasks me-2"></i>কুইজ ও ফাইল</h4><p style="opacity:.9;margin:0">আপনার কোর্সের সকল কুইজ ও ফাইল এখানে দেখুন</p></div>
    
    <?php if(count($items)>0): $current_course='';
        foreach($items as $item):
            if($current_course!=$item['course_name']): if($current_course!='') echo '</div>'; $current_course=$item['course_name'];?>
                <div class="course-group"><h6>📚 <?=htmlspecialchars($item['course_name'])?></h6>
            <?php endif;?>
            <a href="lesson.php?id=<?=$item['lesson_id']?>&course=<?=$item['course_id']?>" class="item-card">
                <?php if($item['quiz_count']>0):?>
                    <div class="item-icon" style="background:var(--g1)"><i class="fas fa-question-circle"></i></div>
                    <div><strong><?=htmlspecialchars($item['lesson_title'])?></strong><br><small><?=$item['quiz_count']?> টি কুইজ | লেসন #<?=$item['order_number']?></small></div>
                    <span class="badge bg-info ms-auto">কুইজ</span>
                <?php elseif($item['attachment_file']): $ext=strtolower(pathinfo($item['attachment_file'],PATHINFO_EXTENSION));?>
                    <div class="item-icon" style="background:<?=$ext=='pdf'?'#ff4757':'#4facfe'?>"><i class="fas <?=$ext=='pdf'?'fa-file-pdf':'fa-file-download'?>"></i></div>
                    <div><strong><?=htmlspecialchars($item['attachment_name']?:$item['lesson_title'])?></strong><br><small>লেসন #<?=$item['order_number']?></small></div>
                    <span class="badge bg-secondary ms-auto">ফাইল</span>
                <?php endif;?>
            </a>
        <?php endforeach; if($current_course!='') echo '</div>';?>
    <?php else:?>
        <div class="empty-state"><i class="fas fa-folder-open" style="font-size:3rem;opacity:.3;margin-bottom:15px;display:block"></i><h5>কোন কুইজ বা ফাইল নেই</h5><p>কোর্সের লেসনে কুইজ ও ফাইল যোগ করা হলে এখানে দেখতে পাবেন</p></div>
    <?php endif;?>
</div>
</body>
</html>