<?php
/**
 * Student Dashboard — Gmind Institute
 *
 * Shows student's enrolled courses, recent exam results,
 * and quick action links.
 */

$basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$user = $user ?? null;
$student = $student ?? null;
$recentResults = $recentResults ?? [];
$availableExams = $availableExams ?? [];
$settings = $settings ?? [];
include __DIR__ . '/../layouts/header.php';

?>
<section class="inner-page-section">
    <div class="container">
        <div class="inner-page-header">
            <h1><?= __('dashboard.title') ?></h1>
            <p><?= __('dashboard.welcome') ?>, <?= htmlspecialchars($user['name'] ?? 'Student', ENT_QUOTES, 'UTF-8') ?>!</p>
        </div>

        <?php if ($flash_error): ?>
        <div class="alert alert-error"><?= htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8') ?></div>
        <?php endif; ?>
        <?php if ($flash_success): ?>
        <div class="alert alert-success"><?= htmlspecialchars($flash_success, ENT_QUOTES, 'UTF-8') ?></div>
        <?php endif; ?>

        <!-- Quick Stats -->
        <div class="dashboard-stats">
            <div class="dash-stat-card">
                <div class="dash-stat-icon icon-navy">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/></svg>
                </div>
                <div class="dash-stat-info">
                    <span class="dash-stat-number"><?= htmlspecialchars($user['name'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></span>
                    <span class="dash-stat-label"><?= __('dashboard.name_label') ?></span>
                </div>
            </div>
            <div class="dash-stat-card">
                <div class="dash-stat-icon icon-scarlet">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" y1="21" x2="16" y2="21"/><line x1="12" y1="17" x2="12" y2="21"/></svg>
                </div>
                <div class="dash-stat-info">
                    <span class="dash-stat-number"><?= count($recentResults) ?></span>
                    <span class="dash-stat-label"><?= __('dashboard.exams_taken') ?></span>
                </div>
            </div>
            <div class="dash-stat-card">
                <div class="dash-stat-icon icon-gold">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                </div>
                <div class="dash-stat-info">
                    <span class="dash-stat-number"><?= $student ? htmlspecialchars($student['enrollment_no'] ?? 'N/A', ENT_QUOTES, 'UTF-8') : 'N/A' ?></span>
                    <span class="dash-stat-label"><?= __('dashboard.enrollment_no') ?></span>
                </div>
            </div>
        </div>

        <!-- Student Details -->
        <?php if ($student): ?>
        <div class="dashboard-card">
            <h2><?= __('dashboard.enrollment_details') ?></h2>
            <div class="dash-info-grid">
                <div class="dash-info-item">
                    <strong><?= __('dashboard.course') ?>:</strong>
                    <span><?= htmlspecialchars($student['course_name'] ?? $student['course_id'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="dash-info-item">
                    <strong><?= __('dashboard.branch') ?>:</strong>
                    <span><?= htmlspecialchars($student['branch_name'] ?? $student['branch_id'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="dash-info-item">
                    <strong><?= __('dashboard.enrolled_on') ?>:</strong>
                    <span><?= htmlspecialchars($student['created_at'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="dash-info-item">
                    <strong><?= __('dashboard.status') ?>:</strong>
                    <span><?= htmlspecialchars(ucfirst($student['status'] ?? 'Active'), ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Recent Exam Results -->
        <div class="dashboard-card">
            <h2><?= __('dashboard.results') ?></h2>
            <?php if (!empty($recentResults)): ?>
            <div class="table-responsive">
                <table class="dash-table">
                    <thead>
                        <tr>
                            <th><?= __('dashboard.exam') ?></th>
                            <th><?= __('dashboard.score') ?></th>
                            <th><?= __('dashboard.percentage') ?></th>
                            <th><?= __('dashboard.status') ?></th>
                            <th><?= __('dashboard.date') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentResults as $result): ?>
                        <tr>
                            <td><?= htmlspecialchars($result['exam_title'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($result['obtained_marks'] ?? $result['score'] ?? '0', ENT_QUOTES, 'UTF-8') ?>/<?= htmlspecialchars($result['total_marks'] ?? $result['exam_total_marks'] ?? '0', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($result['percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>%</td>
                            <td>
                                <span class="badge <?= ($result['status'] ?? '') === 'pass' ? 'badge-success' : 'badge-danger' ?>">
                                    <?= htmlspecialchars(ucfirst($result['status'] ?? 'N/A'), ENT_QUOTES, 'UTF-8') ?>
                                </span>
                            </td>
                            <td><?= htmlspecialchars(date('M d, Y', strtotime($result['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p class="dash-empty"><?= __('dashboard.no_exams_yet') ?></p>
            <?php endif; ?>
        </div>

        <!-- Quick Actions -->
        <div class="dashboard-actions">
            <a href="<?= $basePath ?>/exam" class="btn-primary"><?= __('dashboard.take_exam') ?></a>
            <a href="<?= $basePath ?>/profile" class="btn-outline"><?= __('dashboard.profile') ?></a>
            <form method="POST" action="<?= $basePath ?>/logout" style="display:inline;">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <button type="submit" class="btn-outline" style="color:#e84118; border-color:#e84118;"><?= __('dashboard.logout') ?></button>
            </form>
        </div>
    </div>
</section>

<style>
.dashboard-stats { display:grid; grid-template-columns:repeat(auto-fit, minmax(220px, 1fr)); gap:1.5rem; margin-bottom:2rem; }
.dash-stat-card { background:#fff; border-radius:12px; padding:1.5rem; display:flex; align-items:center; gap:1rem; box-shadow:0 2px 8px rgba(0,0,0,.08); }
.dash-stat-icon { width:48px; height:48px; border-radius:10px; display:flex; align-items:center; justify-content:center; color:#fff; flex-shrink:0; }
.icon-navy { background:#1a4fa0; }
.icon-scarlet { background:#e84118; }
.icon-gold { background:#f39c12; }
.dash-stat-number { display:block; font-size:1.1rem; font-weight:700; color:#0d2b6e; }
.dash-stat-label { display:block; font-size:.85rem; color:#666; }
.dashboard-card { background:#fff; border-radius:12px; padding:2rem; margin-bottom:2rem; box-shadow:0 2px 8px rgba(0,0,0,.08); }
.dashboard-card h2 { font-size:1.3rem; color:#0d2b6e; margin-bottom:1rem; }
.dash-info-grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(200px, 1fr)); gap:1rem; }
.dash-info-item { display:flex; flex-direction:column; gap:.25rem; }
.dash-info-item strong { color:#333; font-size:.9rem; }
.dash-info-item span { color:#555; font-size:1rem; }
.dash-table { width:100%; border-collapse:collapse; }
.dash-table th, .dash-table td { padding:.75rem 1rem; text-align:left; border-bottom:1px solid #eee; font-size:.9rem; }
.dash-table th { background:#f8f9fa; color:#0d2b6e; font-weight:600; }
.badge-success { background:#d4edda; color:#155724; padding:.25rem .5rem; border-radius:4px; font-size:.8rem; }
.badge-danger { background:#f8d7da; color:#721c24; padding:.25rem .5rem; border-radius:4px; font-size:.8rem; }
.dash-empty { color:#888; font-style:italic; }
.dashboard-actions { display:flex; gap:1rem; flex-wrap:wrap; margin-top:1rem; }
.alert { padding:1rem; border-radius:8px; margin-bottom:1.5rem; }
.alert-error { background:#f8d7da; color:#721c24; }
.alert-success { background:#d4edda; color:#155724; }
</style>

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