<?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 + USER DATA ================= */
$sql = "
SELECT 
    s.student_id,
    s.class_id,
    s.regNumber,
    s.fees_paid,
    s.fees_outstanding,
    u.name,
    u.email
FROM students s
JOIN users u ON s.user_id = u.user_id
WHERE s.student_id = ?
";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

$class_id = $student['class_id'];

/* ================= CLASS ================= */
$stmt = $conn->prepare("SELECT class_name FROM classes WHERE class_id = ?");
$stmt->bind_param("i", $class_id);
$stmt->execute();
$class = $stmt->get_result()->fetch_assoc();

/* ================= SUBJECTS ================= */
$stmt = $conn->prepare("
SELECT sub.subject_name
FROM enrollments e
JOIN subject sub ON e.subject_id = sub.subject_id
WHERE e.student_id = ?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$subjects = $stmt->get_result();

/* ================= PAYMENTS ================= */
$stmt = $conn->prepare("
SELECT amount, date, receipt_no
FROM payments
WHERE student_id = ?
ORDER BY date DESC
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$payments = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Profile</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;
    font-family: 'Segoe UI', sans-serif;
}

.profile-container {
    max-width: 1100px;
    margin: 40px auto;
}

.card {
    border: none;
    border-radius: 15px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.08);
}

.profile-header {
    background: linear-gradient(135deg,#4e73df,#224abe);
    color: white;
    padding: 25px;
    border-radius: 15px 15px 0 0;
}

.profile-img {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 4px solid white;
    object-fit: cover;
}

.badge-box {
    background: #eef2ff;
    padding: 10px;
    border-radius: 10px;
    margin-bottom: 10px;
}

.table th {
    background: #f1f3f6;
}
</style>
</head>

<body>

<div class="profile-container">

<!-- HEADER CARD -->
<div class="card mb-4">
    <div class="profile-header d-flex align-items-center gap-4">
        <img src="uploads/profile_pictures/default-profile.png" class="profile-img">

        <div>
            <h3 class="mb-1"><?php echo htmlspecialchars($student['name']); ?></h3>
            <p class="mb-0">
                <i class="fas fa-id-card"></i> <?php echo htmlspecialchars($student['regNumber']); ?>
            </p>
            <p class="mb-0">
                <i class="fas fa-school"></i> <?php echo $class['class_name'] ?? 'N/A'; ?>
            </p>
        </div>
    </div>
</div>

<div class="row g-4">

<!-- PERSONAL INFO -->
<div class="col-md-6">
    <div class="card p-4">
        <h5><i class="fas fa-user"></i> Personal Info</h5>
        <hr>

        <div class="badge-box">
            <strong>Name:</strong> <?php echo htmlspecialchars($student['name']); ?>
        </div>

        <div class="badge-box">
            <strong>Email:</strong> <?php echo htmlspecialchars($student['email']); ?>
        </div>

        <div class="badge-box">
            <strong>Reg Number:</strong> <?php echo htmlspecialchars($student['regNumber']); ?>
        </div>
    </div>
</div>

<!-- ACADEMIC INFO -->
<div class="col-md-6">
    <div class="card p-4">
        <h5><i class="fas fa-book"></i> Academic Info</h5>
        <hr>

        <p><strong>Class:</strong> <?php echo $class['class_name'] ?? 'N/A'; ?></p>

        <p><strong>Subjects:</strong></p>
        <ul>
            <?php while ($s = $subjects->fetch_assoc()): ?>
                <li><?php echo htmlspecialchars($s['subject_name']); ?></li>
            <?php endwhile; ?>
        </ul>
    </div>
</div>

<!-- FEES -->
<div class="col-md-6">
    <div class="card p-4">
        <h5><i class="fas fa-wallet"></i> Fees Status</h5>
        <hr>

        <p><strong>Paid:</strong> ₦<?php echo number_format($student['fees_paid']); ?></p>
        <p><strong>Outstanding:</strong> ₦<?php echo number_format($student['fees_outstanding']); ?></p>
    </div>
</div>

<!-- PAYMENTS -->
<div class="col-md-6">
    <div class="card p-4">
        <h5><i class="fas fa-receipt"></i> Payment History</h5>
        <hr>

        <div class="table-responsive">
            <table class="table table-sm">
                <thead>
                    <tr>
                        <th>Amount</th>
                        <th>Date</th>
                        <th>Receipt</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($payments->num_rows > 0): ?>
                    <?php while ($p = $payments->fetch_assoc()): ?>
                        <tr>
                            <td>₦<?php echo number_format($p['amount']); ?></td>
                            <td><?php echo $p['date']; ?></td>
                            <td><?php echo htmlspecialchars($p['receipt_no']); ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="3">No payments found</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</div>

</div>

</body>
</html>