<?php
/**
 * IBVTE College - Dynamic Dashboard
 */

require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

$collegePage = 'dashboard';
Auth()->requireRole(['college']);

$db = DB();
$user = Auth()->getUser();

// Stats
$totalStudents = $db->fetchOne("SELECT COUNT(*) FROM students WHERE institution_id = ?", [$user['id']])['COUNT(*)'] ?? 0;
$wallet = $db->fetchOne("SELECT balance FROM wallets WHERE user_id = ?", [$user['id']]);
$walletBalance = $wallet['balance'] ?? 0;
$pendingResults = $db->fetchOne("SELECT COUNT(*) FROM students WHERE institution_id = ? AND status = 'pending'", [$user['id']])['COUNT(*)'] ?? 0;

// Recent Students
$recentStudents = $db->fetchAll("SELECT s.*, c.course_name FROM students s JOIN courses c ON s.course_id = c.id WHERE s.institution_id = ? ORDER BY s.created_at DESC LIMIT 5", [$user['id']]);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>College Dashboard - IBVTE</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>
        /* ── College Sidebar ─────────────────────────── */
        :root { --primary:#3b0b7c; --secondary:#6c4ab6; }
        .college-sidebar {
            width: 250px;
            background: white;
            min-height: 100vh;
            position: fixed;
            top: 0; left: 0;
            display: flex;
            flex-direction: column;
            box-shadow: 3px 0 15px rgba(59,11,124,0.08);
            z-index: 1040;
            transition: transform .3s;
        }
        .sidebar-brand {
            padding: 28px 20px 20px;
            text-align: center;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
        }
        .sidebar-logo {
            width: 68px; height: 68px;
            border-radius: 50%;
            border: 3px solid rgba(255,255,255,.35);
            object-fit: cover;
            margin-bottom: 12px;
        }
        .sidebar-college-name {
            color: white;
            font-weight: 700;
            font-size: .9rem;
            line-height: 1.3;
        }
        .sidebar-code {
            color: rgba(255,255,255,.65);
            font-size: .75rem;
            margin-top: 4px;
        }
        .sidebar-nav {
            flex: 1;
            padding: 16px 12px;
            overflow-y: auto;
        }
        .sidebar-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 11px 16px;
            border-radius: 10px;
            color: #555;
            text-decoration: none;
            font-size: .9rem;
            font-weight: 500;
            margin-bottom: 4px;
            transition: background .2s, color .2s, transform .15s;
        }
        .sidebar-link:hover {
            background: #f0ebf8;
            color: var(--primary);
            transform: translateX(3px);
        }
        .sidebar-link.active {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white !important;
            box-shadow: 0 4px 12px rgba(59,11,124,.25);
        }
        .sidebar-icon { width: 18px; text-align: center; flex-shrink: 0; font-size: .95rem; }
        .sidebar-footer { padding: 12px; border-top: 1px solid #f0f0f0; }
        .sidebar-logout { color: #dc3545 !important; }
        .sidebar-logout:hover { background: #fff0f0; color: #dc3545 !important; }
        .college-main { margin-left: 250px; padding: 30px; min-height: 100vh; background: #f8f9fa; }
        @media (max-width: 768px) {
            .college-sidebar { transform: translateX(-100%); }
            .college-sidebar.open { transform: translateX(0); }
            .college-main { margin-left: 0; padding: 20px 15px; }
            .mobile-menu-btn { display: flex !important; }
        }
        .stat-card { border-radius: 15px; border: none; box-shadow: 0 5px 15px rgba(0,0,0,0.05); transition: transform 0.3s; }
        .stat-card:hover { transform: translateY(-5px); }
        .wallet-display { background: white; border-radius: 50px; padding: 8px 25px; box-shadow: 0 4px 10px rgba(0,0,0,0.05); border: 1px solid #eee; }
    </style>
</head>
<body>

<?php include __DIR__ . '/../includes/college_sidebar.php'; ?>

<!-- Mobile menu button -->
<button class="btn btn-sm mobile-menu-btn d-md-none"
        style="display:none;position:fixed;top:15px;left:15px;z-index:1050;background:var(--primary);color:white;border-radius:8px;padding:8px 12px;"
        onclick="toggleSidebar()" id="mobileMenuBtn">
    <i class="fas fa-bars"></i>
</button>
<div class="overlay d-none" id="sidebarOverlay" onclick="toggleSidebar()"
     style="position:fixed;inset:0;background:rgba(0,0,0,.4);z-index:1039;"></div>

<div class="college-main">

    <?php if (!empty($_SESSION['impersonated_by_admin'])): ?>
    <div class="alert d-flex align-items-center justify-content-between mb-4 py-2 px-3"
         style="background:#fff3cd;border:2px solid #ffc107;border-radius:10px;">
        <div>
            <i class="fas fa-user-secret me-2 text-warning"></i>
            <strong>Admin Mode:</strong> You are viewing this college as an admin.
        </div>
        <a href="<?= BASE_URL ?>admin/return-to-admin.php"
           class="btn btn-sm btn-warning fw-bold">
            <i class="fas fa-arrow-left me-1"></i>Return to Admin Panel
        </a>
    </div>
    <?php endif; ?>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h3 class="fw-bold mb-1">Welcome back!</h3>
            <p class="text-muted small mb-0">Here is what's happening today.</p>
        </div>
        <div class="d-flex gap-3 align-items-center">
            <a href="authorization-certificate.php" class="btn btn-outline-primary rounded-pill btn-sm px-3 fw-bold" target="_blank">
                <i class="fas fa-award me-1"></i> My Authorization Certificate
            </a>
            <div class="wallet-display">
                <i class="fas fa-wallet text-primary me-2"></i> Balance: <strong class="text-primary">₹<?= number_format($walletBalance, 2) ?></strong>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-5">
        <div class="col-md-4">
            <div class="card stat-card p-4 border-start border-4 border-primary">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-muted small fw-bold text-uppercase">Total Students</h6>
                        <h2 class="fw-bold mb-0"><?= $totalStudents ?></h2>
                    </div>
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle text-primary">
                        <i class="fas fa-user-graduate fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card stat-card p-4 border-start border-4 border-success">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-muted small fw-bold text-uppercase">Wallet Status</h6>
                        <h2 class="fw-bold mb-0 text-success">Active</h2>
                    </div>
                    <div class="bg-success bg-opacity-10 p-3 rounded-circle text-success">
                        <i class="fas fa-check-circle fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card stat-card p-4 border-start border-4 border-warning">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-muted small fw-bold text-uppercase">Pending Admissions</h6>
                        <h2 class="fw-bold mb-0"><?= $pendingResults ?></h2>
                    </div>
                    <div class="bg-warning bg-opacity-10 p-3 rounded-circle text-warning">
                        <i class="fas fa-hourglass-half fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm rounded-4 p-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h5 class="fw-bold">Recent Registrations</h5>
            <a href="students.php?action=add" class="btn btn-primary rounded-pill btn-sm px-4">
                <i class="fas fa-plus me-1"></i> New Admission
            </a>
        </div>
        <div class="table-responsive">
            <table class="table align-middle table-hover">
                <thead class="bg-light">
                    <tr>
                        <th>Name</th>
                        <th>Course</th>
                        <th>Reg. No</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($recentStudents)): ?>
                        <tr><td colspan="5" class="text-center py-4 text-muted">No recent students found</td></tr>
                    <?php else: ?>
                        <?php foreach ($recentStudents as $student): ?>
                        <tr>
                            <td>
                                <div class="fw-bold"><?= htmlspecialchars($student['full_name']) ?></div>
                                <div class="small text-muted"><?= htmlspecialchars($student['email']) ?></div>
                            </td>
                            <td><?= htmlspecialchars($student['course_name']) ?></td>
                            <td class="small fw-bold"><?= htmlspecialchars($student['registration_number'] ?: 'PENDING') ?></td>
                            <td>
                                <span class="badge rounded-pill bg-<?= $student['status'] == 'active' ? 'success' : 'warning' ?> bg-opacity-10 text-<?= $student['status'] == 'active' ? 'success' : 'warning' ?>">
                                    <?= ucfirst($student['status']) ?>
                                </span>
                            </td>
                            <td>
                                <a href="view-student.php?id=<?= $student['id'] ?>" class="btn btn-light btn-sm rounded-circle"><i class="fas fa-eye text-primary"></i></a>
                                <button class="btn btn-light btn-sm rounded-circle ms-1" title="Request Edit" onclick="requestEdit(<?= $student['id'] ?>)"><i class="fas fa-edit text-muted"></i></button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    function requestEdit(studentId) {
        alert('As per security policy, Colleges cannot edit student information directly. A request has been logged for Admin approval.');
        // Here you would typically send an AJAX request to log the edit request
    }
</script>
</body>
</html>
