<?php
// my-certificates.php - My Certificates & Lesson Files
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'];
$user = getUserData($pdo, $user_id);

// Get certificates
$stmt = $pdo->prepare("SELECT c.*, cr.title_bn as course_title FROM certificates c LEFT JOIN courses cr ON c.course_id = cr.id WHERE c.user_id = ? ORDER BY c.created_at DESC");
$stmt->execute([$user_id]);
$certificates = $stmt->fetchAll();

// Get enrolled courses with lesson 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.attachment_file, l.attachment_name, l.order_number
    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 l.attachment_file IS NOT NULL AND l.attachment_file != ''
    ORDER BY c.title_bn, l.order_number
");
$stmt->execute([$user_id]);
$lesson_files = $stmt->fetchAll();

// Dynamic settings
$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'] ?? 'ONESBD LMS | সর্বস্বত্ব সংরক্ষিত';

$tab = $_GET['tab'] ?? 'certificates';
?>
<!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">
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <style>
        :root{--primary:<?=$pc?>;--g1:linear-gradient(135deg,<?=$pc?>,<?=$pc?>dd);--g2:linear-gradient(135deg,#f093fb,#f5576c);--g3:linear-gradient(135deg,#4facfe,#00f2fe);--g4:linear-gradient(135deg,#43e97b,#38f9d7);--dark:#1a1a2e;--shadow:0 4px 20px rgba(0,0,0,.06)}
        *{margin:0;padding:0;box-sizing:border-box}
        body{background:#f0f2f8;font-family:'Segoe UI','Hind Siliguri',Arial,sans-serif}
        
        .navbar{background:rgba(255,255,255,.95);backdrop-filter:blur(15px);box-shadow:0 2px 20px rgba(0,0,0,.06);padding:12px 0;position:sticky;top:0;z-index:1000}
        .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:1100px;margin:0 auto;padding:20px}
        
        .page-header{background:var(--g1);color:white;border-radius:20px;padding:30px;margin-bottom:25px;box-shadow:0 10px 40px rgba(108,92,231,.2)}
        .page-header h3{font-weight:700;margin:0}
        
        .nav-pills{gap:8px;margin-bottom:25px}
        .nav-pills .nav-link{border-radius:50px;padding:10px 22px;font-weight:600;border:2px solid #e0e0e0;color:#555;transition:.3s}
        .nav-pills .nav-link.active{background:var(--g1);color:white;border-color:transparent}
        .nav-pills .badge{margin-left:6px}
        
        .cert-card{background:white;border-radius:16px;padding:22px;box-shadow:0 2px 10px rgba(0,0,0,.04);margin-bottom:15px;transition:.3s;border-left:5px solid var(--g4);display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:15px}
        .cert-card:hover{box-shadow:var(--shadow);transform:translateY(-3px)}
        .cert-card.manual{border-left-color:#ffc107}
        .cert-number{font-family:monospace;background:#f0f0f0;padding:4px 10px;border-radius:6px;font-size:.8rem}
        
        .file-card{background:white;border-radius:14px;padding:16px 20px;box-shadow:0 2px 10px rgba(0,0,0,.04);margin-bottom:10px;transition:.3s;display:flex;align-items:center;gap:15px;text-decoration:none;color:inherit}
        .file-card:hover{box-shadow:var(--shadow);transform:translateY(-2px)}
        .file-icon{width:50px;height:50px;border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:1.3rem;color:white;flex-shrink:0}
        
        .course-group{margin-bottom:25px}
        .course-group h6{font-weight:700;color:var(--primary);margin-bottom:10px;padding-bottom:8px;border-bottom:2px solid #e8eaef}
        
        .btn{font-weight:600;cursor:pointer;transition:.3s;border-radius:50px;padding:8px 20px;border:none;text-decoration:none;display:inline-flex;align-items:center;gap:6px;font-size:.85rem}
        .btn-primary{background:var(--g1);color:white}.btn-primary:hover{transform:translateY(-2px);color:white}
        .btn-success{background:var(--g4);color:#0d4d2b}.btn-outline{border:2px solid var(--primary);color:var(--primary);background:white}
        
        .empty-state{text-align:center;padding:60px 20px;color:#888}
        .empty-state i{font-size:4rem;opacity:.2;margin-bottom:15px}
        
        footer{background:var(--dark);color:white;text-align:center;padding:25px;margin-top:40px}
        
        @media(max-width:768px){.cert-card{flex-direction:column;text-align:center}}
    </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="<?=htmlspecialchars($st)?>">
            <?php else: ?>
                <i class="fas <?=$li?> me-2"></i><?=htmlspecialchars($lt)?>
            <?php endif; ?>
        </a>
        <div style="display:flex;gap:12px">
            <a href="dashboard.php" class="btn btn-outline btn-sm"><i class="fas fa-arrow-left me-1"></i> ড্যাশবোর্ড</a>
        </div>
    </div>
</nav>

<div class="container">
    
    <div class="page-header" data-aos="fade-down">
        <h3>🏆 আমার অর্জন</h3>
        <p style="opacity:.9;margin:5px 0 0">সার্টিফিকেট ও কোর্স ফাইলসমূহ</p>
    </div>
    
    <!-- Tabs -->
    <ul class="nav nav-pills" data-aos="fade-up">
        <li class="nav-item">
            <a class="nav-link <?=$tab=='certificates'?'active':''?>" href="?tab=certificates">
                <i class="fas fa-certificate me-1"></i> সার্টিফিকেট <span class="badge bg-<?=$tab=='certificates'?'light text-dark':'secondary'?>"><?=count($certificates)?></span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?=$tab=='files'?'active':''?>" href="?tab=files">
                <i class="fas fa-folder-open me-1"></i> আমার ফাইল <span class="badge bg-<?=$tab=='files'?'light text-dark':'secondary'?>"><?=count($lesson_files)?></span>
            </a>
        </li>
    </ul>
    
    <!-- CERTIFICATES TAB -->
    <?php if($tab == 'certificates'): ?>
        <?php if(count($certificates) > 0): ?>
            <?php foreach($certificates as $cert): ?>
                <div class="cert-card <?=$cert['certificate_type']=='manual'?'manual':''?>" data-aos="fade-up">
                    <div>
                        <h5 style="font-weight:700;color:var(--dark)"><?=htmlspecialchars($cert['course_name'] ?: $cert['course_title'] ?: 'কোর্স')?></h5>
                        <small class="text-muted">
                            📅 <?=date('d M Y',strtotime($cert['completion_date']))?> | 
                            🎯 গ্রেড: <strong><?=$cert['grade']?></strong> | 
                            <?=$cert['certificate_type']=='auto'?'🤖 অটো':'✋ ম্যানুয়াল'?>
                        </small>
                        <br><span class="cert-number"><?=$cert['certificate_number']?></span>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="admin/view-certificate.php?id=<?=$cert['id']?>" target="_blank" class="btn btn-primary btn-sm"><i class="fas fa-eye"></i> দেখুন</a>
                        <button class="btn btn-outline btn-sm" onclick="window.open('admin/view-certificate.php?id=<?=$cert['id']?>','_blank');setTimeout(function(){window.print()},1000)"><i class="fas fa-print"></i></button>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="empty-state"><i class="fas fa-certificate"></i><h5>কোন সার্টিফিকেট নেই</h5><p>কোর্স সম্পন্ন করলেই সার্টিফিকেট পাবেন!</p></div>
        <?php endif; ?>
    <?php endif; ?>
    
    <!-- FILES TAB -->
    <?php if($tab == 'files'): ?>
        <?php if(count($lesson_files) > 0): ?>
            <?php 
            $current_course = '';
            foreach($lesson_files as $file): 
                $ext = strtolower(pathinfo($file['attachment_file'], PATHINFO_EXTENSION));
                $icons = ['pdf'=>'fa-file-pdf text-danger','doc'=>'fa-file-word text-primary','docx'=>'fa-file-word text-primary','ppt'=>'fa-file-powerpoint text-warning','pptx'=>'fa-file-powerpoint text-warning','xls'=>'fa-file-excel text-success','xlsx'=>'fa-file-excel text-success','zip'=>'fa-file-archive text-secondary','rar'=>'fa-file-archive text-secondary','txt'=>'fa-file-alt text-info'];
                $icon = $icons[$ext] ?? 'fa-file text-muted';
                $iconBg = ['pdf'=>'#ff4757','doc'=>'#4facfe','docx'=>'#4facfe','ppt'=>'#ffa502','pptx'=>'#ffa502','xls'=>'#2ed573','xlsx'=>'#2ed573','zip'=>'#747d8c','rar'=>'#747d8c','txt'=>'#1e90ff'];
                $bg = $iconBg[$ext] ?? '#6C5CE7';
                
                if($current_course != $file['course_name']):
                    if($current_course != '') echo '</div>';
                    $current_course = $file['course_name'];
            ?>
                    <div class="course-group" data-aos="fade-up">
                        <h6>📚 <?=htmlspecialchars($file['course_name'])?></h6>
            <?php endif; ?>
                
                <a href="uploads/materials/<?=$file['attachment_file']?>" class="file-card" target="_blank">
                    <div class="file-icon" style="background:<?=$bg?>"><i class="fas <?=$icon?>"></i></div>
                    <div style="flex:1">
                        <strong><?=htmlspecialchars($file['attachment_name'] ?: $file['lesson_title'])?></strong>
                        <br><small class="text-muted">লেসন: <?=htmlspecialchars($file['lesson_title'])?> | #<?=$file['order_number']?></small>
                    </div>
                    <div class="d-flex gap-1">
                        <span class="btn btn-outline btn-xs" style="padding:4px 10px;font-size:.7rem"><i class="fas fa-eye"></i></span>
                        <span class="btn btn-primary btn-xs" style="padding:4px 10px;font-size:.7rem" onclick="event.preventDefault();window.open('uploads/materials/<?=$file['attachment_file']?>')"><i class="fas fa-download"></i></span>
                    </div>
                </a>
            <?php endforeach; ?>
            <?php if($current_course != '') echo '</div>'; ?>
        <?php else: ?>
            <div class="empty-state"><i class="fas fa-folder-open"></i><h5>কোন ফাইল নেই</h5><p>কোর্সের লেসনে ফাইল সংযুক্ত করা হলে এখানে দেখতে পাবেন</p></div>
        <?php endif; ?>
    <?php endif; ?>
</div>

<footer><p>&copy; <?=date('Y')?> <?=htmlspecialchars($ft)?></p></footer>

<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>AOS.init({duration:700,once:true});</script>
</body>
</html>