<?php
session_start();
include('db_connection.php');

/* ================= AUTH ================= */
if (
    !isset($_SESSION['student_id']) ||
    $_SESSION['role'] !== 'student'
) {
    header("Location: student_login.php?error=Unauthorized");
    exit();
}

$student_id = $_SESSION['student_id'];
$student_name = $_SESSION['name'] ?? 'Student';

/* ================= STUDENT DATA ================= */
$stmt = $conn->prepare("SELECT class_id, fees_paid, fees_outstanding FROM students WHERE student_id=?");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

$class_id = $student['class_id'] ?? 0;
$fees_paid = $student['fees_paid'] ?? 0;
$fees_outstanding = $student['fees_outstanding'] ?? 0;

/* ================= CLASS COUNT ================= */
$stmt = $conn->prepare("SELECT COUNT(*) total FROM students WHERE class_id=?");
$stmt->bind_param("i", $class_id);
$stmt->execute();
$classCount = $stmt->get_result()->fetch_assoc()['total'];

/* ================= ATTENDANCE ================= */
$stmt = $conn->prepare("
    SELECT 
        COUNT(*) total_days,
        SUM(status='present') present_days
    FROM student_attendance
    WHERE student_id=?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$att = $stmt->get_result()->fetch_assoc();

$total_days = $att['total_days'] ?? 0;
$present_days = $att['present_days'] ?? 0;
$attendance_percent = $total_days > 0 ? round(($present_days/$total_days)*100) : 0;

/* ================= SETTINGS ================= */
$settings = $conn->query("SELECT current_term, current_session FROM settings LIMIT 1")->fetch_assoc();

/* ================= NOTIFICATIONS ================= */
$notifications = $conn->query("
    SELECT n.message_head, n.message_body
    FROM notifications n
    JOIN notification_recipients r ON n.id = r.notification_id
    WHERE r.role = 'student'
    ORDER BY n.created_at DESC
    LIMIT 5
");

/* ================= ACTIVITIES ================= */
$activities = $conn->query("
    SELECT activity_name, description
    FROM upcoming_activities
    ORDER BY activity_date DESC
    LIMIT 5
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Dashboard</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body { background:#f4f6f9; }

.sidebar {
    position:fixed;
    width:250px;
    height:100%;
    background:#1f2d3d;
    color:white;
}

.sidebar a {
    display:block;
    padding:12px;
    color:white;
    text-decoration:none;
}

.sidebar a:hover { background:#34495e; }

.main {
    margin-left:250px;
    padding:20px;
}

.card { border:none; border-radius:12px; }

.quick-link i {
    font-size:20px;
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">
    <h4 class="text-center py-3"><i class="fas fa-user-graduate"></i></h4>
    <p class="text-center"><?php echo htmlspecialchars($student_name); ?></p>

    <a href="student_dashboard.php"><i class="fas fa-home me-2"></i> Dashboard</a>
    <a href="view_homework.php"><i class="fas fa-book me-2"></i> Homework</a>
    <a href="view_fees.php"><i class="fas fa-money-bill me-2"></i> Fees</a>
    <a href="view_timetable.php"><i class="fas fa-calendar me-2"></i> Timetable</a>
    <a href="student_profile.php"><i class="fas fa-user me-2"></i> Profile</a>
    <a href="view_results.php"><i class="fas fa-chart-line me-2"></i> Results</a>
    <a href="attendance.php"><i class="fas fa-check-circle me-2"></i> Attendance</a>
    <a href="logout.php"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
</div>

<!-- MAIN -->
<div class="main">

<!-- HEADER -->
<div class="d-flex justify-content-between mb-4">
    <h3>Dashboard</h3>
    <div>
        <strong>Term:</strong> <?php echo $settings['current_term'] ?? 'N/A'; ?> |
        <strong>Session:</strong> <?php echo $settings['current_session'] ?? 'N/A'; ?>
    </div>
</div>

<!-- STATS -->
<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="card shadow p-3 text-center">
            <h6>Classmates</h6>
            <h3><?php echo $classCount; ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow p-3 text-center">
            <h6>Fees Paid</h6>
            <h3>₦<?php echo number_format($fees_paid); ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow p-3 text-center">
            <h6>Outstanding</h6>
            <h3>₦<?php echo number_format($fees_outstanding); ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow p-3 text-center">
            <h6>Attendance</h6>
            <h3><?php echo $attendance_percent; ?>%</h3>
        </div>
    </div>
</div>

<!-- QUICK LINKS -->
<div class="row g-3 mb-4">
    <div class="col-md-3 quick-link text-center">
        <a href="view_homework.php" class="card p-3 shadow">
            <i class="fas fa-book"></i>
            <p>Homework</p>
        </a>
    </div>

    <div class="col-md-3 quick-link text-center">
        <a href="view_results.php" class="card p-3 shadow">
            <i class="fas fa-chart-bar"></i>
            <p>Results</p>
        </a>
    </div>

    <div class="col-md-3 quick-link text-center">
        <a href="attendance.php" class="card p-3 shadow">
            <i class="fas fa-check"></i>
            <p>Attendance</p>
        </a>
    </div>

    <div class="col-md-3 quick-link text-center">
        <a href="view_fees.php" class="card p-3 shadow">
            <i class="fas fa-wallet"></i>
            <p>Fees</p>
        </a>
    </div>
</div>

<!-- CONTENT -->
<div class="row g-4">

    <!-- NOTIFICATIONS -->
    <div class="col-md-6">
        <div class="card shadow p-3">
            <h5><i class="fas fa-bell"></i> Notifications</h5>
            <hr>
            <?php if ($notifications->num_rows): ?>
                <?php while ($n = $notifications->fetch_assoc()): ?>
                    <p><strong><?php echo htmlspecialchars($n['message_head']); ?></strong></p>
                    <p><?php echo htmlspecialchars($n['message_body']); ?></p>
                    <hr>
                <?php endwhile; ?>
            <?php else: ?>
                <p>No notifications available</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- ACTIVITIES -->
    <div class="col-md-6">
        <div class="card shadow p-3">
            <h5><i class="fas fa-calendar-alt"></i> Activities</h5>
            <hr>
            <?php if ($activities->num_rows): ?>
                <?php while ($a = $activities->fetch_assoc()): ?>
                    <p><strong><?php echo htmlspecialchars($a['activity_name']); ?></strong></p>
                    <p><?php echo htmlspecialchars($a['description']); ?></p>
                    <hr>
                <?php endwhile; ?>
            <?php else: ?>
                <p>No activities available</p>
            <?php endif; ?>
        </div>
    </div>

</div>

</div>

</body>
</html>