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

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

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

/* ================= FETCH FEES ================= */
$stmt = $conn->prepare("
    SELECT name, fees_paid, fees_outstanding,
           (fees_paid + fees_outstanding) AS total_fees
    FROM students
    WHERE student_id = ?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$fees = $stmt->get_result()->fetch_assoc();

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fees 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 */
.sidebar {
    position:fixed;
    width:240px;
    height:100%;
    background:#1f2d3d;
    color:white;
}
.sidebar a {
    display:block;
    padding:12px;
    color:white;
    text-decoration:none;
}
.sidebar a:hover { background:#34495e; }

/* Main */
.main {
    margin-left:240px;
    padding:20px;
}

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

/* Status Badge */
.badge-paid { background:#28a745; }
.badge-owing { background:#dc3545; }

/* Table */
.table {
    border-radius:10px;
    overflow:hidden;
}
</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"></i> Dashboard</a>
    <a href="view_homework.php"><i class="fas fa-book"></i> Homework</a>
    <a href="view_fees.php"><i class="fas fa-money-bill"></i> Fees</a>
    <a href="view_timetable.php"><i class="fas fa-calendar"></i> Timetable</a>
    <a href="student_profile.php"><i class="fas fa-user"></i> Profile</a>
    <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
</div>

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

<!-- HEADER -->
<div class="d-flex justify-content-between mb-4">
    <h3><i class="fas fa-wallet"></i> Fees Dashboard</h3>
</div>

<!-- SUMMARY CARDS -->
<div class="row g-3 mb-4">

    <div class="col-md-4">
        <div class="card shadow p-3 text-center">
            <h6>Total Fees</h6>
            <h3>₦<?php echo number_format($fees['total_fees'] ?? 0); ?></h3>
        </div>
    </div>

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

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

            <?php if (($fees['fees_outstanding'] ?? 0) > 0): ?>
                <span class="badge badge-owing mt-2">Owing</span>
            <?php else: ?>
                <span class="badge badge-paid mt-2">Paid</span>
            <?php endif; ?>
        </div>
    </div>

</div>

<!-- PAYMENT HISTORY -->
<div class="card shadow p-4">
    <h5><i class="fas fa-receipt"></i> Payment History</h5>
    <hr>

    <?php if ($payments->num_rows > 0): ?>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>Date</th>
                        <th>Amount</th>
                        <th>Purpose</th>
                        <th>Receipt</th>
                    </tr>
                </thead>
                <tbody>
                <?php while ($row = $payments->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['date']); ?></td>
                        <td>₦<?php echo number_format($row['amount']); ?></td>
                        <td><?php echo htmlspecialchars($row['purpose']); ?></td>
                        <td>
                            <span class="badge bg-primary">
                                <?php echo htmlspecialchars($row['receipt_no']); ?>
                            </span>
                        </td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    <?php else: ?>
        <p class="text-muted">No payment history available</p>
    <?php endif; ?>
</div>

</div>

</body>
</html>