<?php
/** @var array $upcoming — list of publicly visible upcoming/active branch exams */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$upcoming = $upcoming ?? [];
$lang = function_exists('getCurrentLocale') ? getCurrentLocale() : 'en';
$isHindi = $lang === 'hi';

include __DIR__ . '/../layouts/header.php';
?>
<div class="container" style="max-width:1100px;margin:40px auto;padding:0 20px;">
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:32px;align-items:start;">
        <!-- Login form -->
        <div style="background:#fff;border-radius:14px;padding:36px;box-shadow:0 6px 24px rgba(0,0,0,0.07);border-top:5px solid #16a085;">
            <div style="text-align:center;margin-bottom:24px;">
                <div style="width:64px;height:64px;background:linear-gradient(135deg,#16a085,#117a65);border-radius:50%;display:inline-flex;align-items:center;justify-content:center;color:#fff;font-size:1.8rem;margin-bottom:10px;">
                    📝
                </div>
                <h1 style="margin:0;font-size:1.6rem;color:#0d3b3b;font-weight:700;"><?= $isHindi ? 'शाखा परीक्षा पोर्टल' : 'Branch Exam Portal' ?></h1>
                <p style="margin:6px 0 0;color:#666;font-size:0.9rem;"><?= $isHindi ? 'अपनी परीक्षा देने के लिए लॉगिन करें' : 'Login to take your branch exam' ?></p>
            </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; ?>

            <form method="POST" action="<?= $bp ?>/branch-exam/login">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <div style="margin-bottom:14px;">
                    <label style="display:block;font-size:0.84rem;font-weight:600;color:#0d3b3b;margin-bottom:5px;"><?= $isHindi ? 'एनरोलमेंट नंबर या ईमेल' : 'Enrollment No / Email' ?></label>
                    <input type="text" name="login_id" required autofocus
                           placeholder="<?= $isHindi ? 'जैसे GMIND2024001' : 'e.g. GMIND2024001' ?>"
                           style="width:100%;padding:12px;border:1px solid #ddd;border-radius:8px;font-size:0.92rem;box-sizing:border-box;">
                </div>
                <div style="margin-bottom:18px;">
                    <label style="display:block;font-size:0.84rem;font-weight:600;color:#0d3b3b;margin-bottom:5px;"><?= $isHindi ? 'पासवर्ड' : 'Password' ?></label>
                    <input type="password" name="password" required
                           placeholder="••••••••"
                           style="width:100%;padding:12px;border:1px solid #ddd;border-radius:8px;font-size:0.92rem;box-sizing:border-box;">
                </div>
                <button type="submit"
                        style="width:100%;padding:13px;background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.95rem;cursor:pointer;">
                    <?= $isHindi ? 'लॉगिन करें →' : 'Login →' ?>
                </button>
            </form>

            <p style="text-align:center;margin:18px 0 0;font-size:0.8rem;color:#999;">
                <?= $isHindi ? 'अपना पासवर्ड भूल गए? अपनी शाखा से संपर्क करें।' : 'Forgot your password? Contact your branch.' ?>
            </p>
        </div>

        <!-- Public exam list -->
        <div>
            <h2 style="margin:0 0 14px;font-size:1.15rem;color:#0d3b3b;"><?= $isHindi ? 'आगामी / चालू परीक्षाएं' : 'Upcoming / Active Exams' ?></h2>
            <?php if (empty($upcoming)): ?>
                <div style="background:#fff;border-radius:10px;padding:30px;text-align:center;color:#999;box-shadow:0 2px 8px rgba(0,0,0,0.05);">
                    <p style="margin:0;font-size:0.9rem;"><?= $isHindi ? 'अभी कोई सक्रिय परीक्षा उपलब्ध नहीं है।' : 'No active exams right now. Please check back later.' ?></p>
                </div>
            <?php else: foreach ($upcoming as $e):
                $langLabel = ['en'=>'English','hi'=>'हिंदी','both'=>'English + Hindi'][$e['language'] ?? 'en'] ?? 'English';
                $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) {
                    $statusLabel = $isHindi ? 'आगामी' : 'Upcoming';
                    $statusColor = '#f39c12';
                } elseif (!empty($e['end_time']) && $now > $et) {
                    $statusLabel = $isHindi ? 'समाप्त' : 'Ended';
                    $statusColor = '#999';
                } else {
                    $statusLabel = $isHindi ? 'चालू' : 'Live';
                    $statusColor = '#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 <?= $statusColor ?>;">
                    <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:6px;">
                        <h3 style="margin:0;font-size:0.95rem;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>
                        <span style="padding:3px 10px;border-radius:12px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $statusColor ?>;"><?= $statusLabel ?></span>
                    </div>
                    <div style="font-size:0.78rem;color:#666;line-height:1.6;">
                        <span>🏫 <?= htmlspecialchars($e['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($e['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>)</span> ·
                        <span>📝 <?= (int)($e['question_count'] ?? 0) ?> Qs</span> ·
                        <span>⏱ <?= (int)($e['duration_minutes'] ?? 60) ?> min</span> ·
                        <span>🏆 <?= (int)($e['total_marks'] ?? 100) ?> marks</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; ?>
                </div>
            <?php endforeach; endif; ?>

            <div style="margin-top:18px;padding:14px;background:#eaf0fb;border-radius:8px;font-size:0.78rem;color:#1a4fa0;line-height:1.6;">
                <strong>ℹ Note:</strong> To take an exam, you must be a student admitted to the branch that created it. Login with your Enrollment No / Email and password to see all exams available to you.
            </div>
        </div>
    </div>
</div>

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

