<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$userName     = $userName ?? '';
$enrollmentNo = $enrollmentNo ?? '';
$branchCode   = $branchCode ?? '';
$live         = $live ?? [];
$upcoming     = $upcoming ?? [];
$past         = $past ?? [];
$alwaysAvailable = $alwaysAvailable ?? [];
$lang = function_exists('getCurrentLocale') ? getCurrentLocale() : 'en';
$isHindi = $lang === 'hi';

include __DIR__ . '/../layouts/header.php';

$renderExamCard = function (array $e) use ($bp, $isHindi): void {
    $langLabel = ['en'=>'English','hi'=>'हिंदी','both'=>'English + Hindi'][$e['language'] ?? 'en'] ?? 'English';
    $attempted = !empty($e['already_taken']);
    $st = !empty($e['start_time']) ? strtotime($e['start_time']) : 0;
    $et = !empty($e['end_time']) ? strtotime($e['end_time']) : 0;
    $now = time();
    if (!empty($e['start_time']) && $now < $st) {
        $status = $isHindi ? 'आगामी' : 'Upcoming'; $color = '#f39c12';
    } elseif (!empty($e['end_time']) && $now > $et) {
        $status = $isHindi ? 'समाप्त' : 'Ended'; $color = '#999';
    } else {
        $status = $isHindi ? 'चालू' : 'Live'; $color = '#27ae60';
    }
    ?>
    <div style="background:#fff;border-radius:10px;padding:18px;margin-bottom:12px;box-shadow:0 2px 8px rgba(0,0,0,0.05);border-left:4px solid <?= $color ?>;">
        <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:8px;gap:14px;">
            <div style="flex:1;">
                <h3 style="margin:0;font-size:0.98rem;color:#0d3b3b;font-weight:600;">
                    <?= htmlspecialchars($e['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                    <?php if (!empty($e['title_hindi']) && $e['language'] !== 'en'): ?>
                        <br><span style="font-weight:400;font-size:0.84rem;color:#666;font-family:'Noto Sans Devanagari',sans-serif;"><?= htmlspecialchars($e['title_hindi'], ENT_QUOTES, 'UTF-8') ?></span>
                    <?php endif; ?>
                </h3>
                <div style="font-size:0.78rem;color:#666;margin-top:5px;line-height:1.6;">
                    <span>📝 <?= (int)($e['question_count'] ?? 0) ?> Qs</span> ·
                    <span>⏱ <?= (int)($e['duration_minutes'] ?? 60) ?> min</span> ·
                    <span>🏆 <?= (int)($e['total_marks'] ?? 100) ?> marks (pass: <?= (int)($e['pass_marks'] ?? 40) ?>)</span> ·
                    <span>🌐 <?= htmlspecialchars($langLabel, ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <?php if (!empty($e['start_time'])): ?>
                    <div style="font-size:0.76rem;color:#999;margin-top:4px;">
                        📅 <?= date('d M Y, g:i A', strtotime($e['start_time'])) ?>
                        <?php if (!empty($e['end_time'])): ?> → <?= date('d M Y, g:i A', strtotime($e['end_time'])) ?><?php endif; ?>
                    </div>
                <?php endif; ?>
                <?php if (!empty($e['instructions']) || !empty($e['instructions_hindi'])): ?>
                    <div style="font-size:0.76rem;color:#555;margin-top:6px;padding:8px;background:#f9f9f9;border-radius:4px;">
                        <strong>📋 Instructions:</strong>
                        <?php if ($e['language'] !== 'hi' && !empty($e['instructions'])): ?>
                            <div><?= nl2br(htmlspecialchars($e['instructions'], ENT_QUOTES, 'UTF-8')) ?></div>
                        <?php endif; ?>
                        <?php if ($e['language'] !== 'en' && !empty($e['instructions_hindi'])): ?>
                            <div style="font-family:'Noto Sans Devanagari',sans-serif;"><?= nl2br(htmlspecialchars($e['instructions_hindi'], ENT_QUOTES, 'UTF-8')) ?></div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
            <div style="text-align:right;white-space:nowrap;">
                <span style="display:inline-block;padding:3px 10px;border-radius:12px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $color ?>;margin-bottom:8px;"><?= $status ?></span>
                <br>
                <?php if ($attempted): ?>
                    <span style="display:inline-block;padding:6px 14px;border-radius:6px;font-size:0.78rem;font-weight:600;background:#f0f0f0;color:#666;"><?= $isHindi ? '✓ दिया जा चुका' : '✓ Already taken' ?></span>
                <?php elseif ($status === ($isHindi ? 'चालू' : 'Live')): ?>
                    <a href="<?= $bp ?>/branch-exam/take/<?= (int)$e['id'] ?>"
                       style="display:inline-block;padding:8px 18px;border-radius:6px;font-size:0.82rem;font-weight:600;background:linear-gradient(135deg,#16a085,#117a65);color:#fff;text-decoration:none;">
                        <?= $isHindi ? 'परीक्षा दें →' : 'Start Exam →' ?>
                    </a>
                <?php elseif ($status === ($isHindi ? 'आगामी' : 'Upcoming')): ?>
                    <span style="display:inline-block;padding:6px 14px;border-radius:6px;font-size:0.78rem;font-weight:600;background:#fff5e6;color:#f39c12;"><?= $isHindi ? 'जल्द शुरू' : 'Starts soon' ?></span>
                <?php else: ?>
                    <span style="display:inline-block;padding:6px 14px;border-radius:6px;font-size:0.78rem;font-weight:600;background:#f0f0f0;color:#999;"><?= $isHindi ? 'समय समाप्त' : 'Window closed' ?></span>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php
};
?>
<div class="container" style="max-width:900px;margin:30px auto;padding:0 20px;">
    <!-- Header -->
    <div style="background:linear-gradient(135deg,#0d3b3b,#072525);color:#fff;border-radius:14px;padding:24px;margin-bottom:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;">
            <div>
                <h1 style="margin:0;font-size:1.4rem;font-weight:700;"><?= $isHindi ? 'मेरी परीक्षाएं' : 'My Exams' ?></h1>
                <p style="margin:6px 0 0;opacity:0.85;font-size:0.86rem;">
                    <?= $isHindi ? 'स्वागत है' : 'Welcome' ?>, <strong><?= htmlspecialchars($userName, ENT_QUOTES, 'UTF-8') ?></strong> ·
                    <?= $isHindi ? 'एनरोलमेंट' : 'Enrollment' ?>: <code style="background:rgba(255,255,255,0.15);padding:1px 6px;border-radius:3px;"><?= htmlspecialchars($enrollmentNo, ENT_QUOTES, 'UTF-8') ?></code> ·
                    <?= $isHindi ? 'शाखा' : 'Branch' ?>: <strong><?= htmlspecialchars($branchCode, ENT_QUOTES, 'UTF-8') ?></strong>
                </p>
            </div>
            <form method="POST" action="<?= $bp ?>/branch-exam/logout">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <button type="submit" style="background:rgba(255,255,255,0.15);color:#fff;border:1px solid rgba(255,255,255,0.3);padding:8px 18px;border-radius:6px;font-size:0.82rem;cursor:pointer;font-weight:600;">
                    <?= $isHindi ? 'लॉगआउट' : 'Logout' ?>
                </button>
            </form>
        </div>
    </div>

    <?php if (!empty($flash_error)): ?>
        <div style="background:#fff5f5;border:1px solid #e84118;color:#e84118;padding:10px 14px;border-radius:6px;margin-bottom:14px;font-size:0.86rem;"><?= htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8') ?></div>
    <?php endif; ?>
    <?php if (!empty($flash_success)): ?>
        <div style="background:#eafaf1;border:1px solid #27ae60;color:#1a7a4f;padding:10px 14px;border-radius:6px;margin-bottom:14px;font-size:0.86rem;"><?= htmlspecialchars($flash_success, ENT_QUOTES, 'UTF-8') ?></div>
    <?php endif; ?>

    <!-- Live exams (always-available + currently-open scheduled) -->
    <h2 style="margin:24px 0 10px;font-size:1.05rem;color:#27ae60;display:flex;align-items:center;gap:8px;">
        <span style="width:10px;height:10px;background:#27ae60;border-radius:50%;display:inline-block;animation:pulse 1.5s infinite;"></span>
        <?= $isHindi ? 'चालू परीक्षाएं' : 'Live Exams' ?> (<?= count($live) + count($alwaysAvailable) ?>)
    </h2>
    <style>@keyframes pulse{0%,100%{opacity:1}50%{opacity:0.4}}</style>
    <?php
    if (empty($live) && empty($alwaysAvailable)) {
        echo '<div style="background:#fff;border-radius:10px;padding:20px;text-align:center;color:#999;box-shadow:0 2px 8px rgba(0,0,0,0.05);">' . ($isHindi ? 'अभी कोई चालू परीक्षा नहीं है।' : 'No live exams right now.') . '</div>';
    } else {
        foreach ($alwaysAvailable as $e) $renderExamCard($e);
        foreach ($live as $e) $renderExamCard($e);
    }
    ?>

    <!-- Upcoming -->
    <?php if (!empty($upcoming)): ?>
    <h2 style="margin:24px 0 10px;font-size:1.05rem;color:#f39c12;">⏳ <?= $isHindi ? 'आगामी परीक्षाएं' : 'Upcoming Exams' ?> (<?= count($upcoming) ?>)</h2>
    <?php foreach ($upcoming as $e) $renderExamCard($e); ?>
    <?php endif; ?>

    <!-- Past -->
    <?php if (!empty($past)): ?>
    <h2 style="margin:24px 0 10px;font-size:1.05rem;color:#999;">📜 <?= $isHindi ? 'समाप्त परीक्षाएं' : 'Past Exams' ?> (<?= count($past) ?>)</h2>
    <?php foreach ($past as $e) $renderExamCard($e); ?>
    <?php endif; ?>

    <?php if (empty($live) && empty($upcoming) && empty($past) && empty($alwaysAvailable)): ?>
        <div style="background:#fff;border-radius:10px;padding:40px;text-align:center;color:#999;box-shadow:0 2px 8px rgba(0,0,0,0.05);">
            <p style="margin:0;font-size:0.95rem;"><?= $isHindi ? 'आपकी शाखा ने अभी तक कोई परीक्षा प्रकाशित नहीं की है।' : 'Your branch has not published any exams yet.' ?></p>
        </div>
    <?php endif; ?>
</div>

<?php
include __DIR__ . '/../layouts/footer.php';

