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

/* =========================
   AUTH CHECK
========================= */
if (
    !isset($_SESSION['teacher_id']) ||
    $_SESSION['role'] !== 'teacher'
) {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = intval($_SESSION['teacher_id']);

/* =========================
   FILTER
========================= */
$filter = $_GET['filter'] ?? 'all';

/* =========================
   FETCH STUDENTS (BASED ON TEACHER CLASSES)
========================= */

$sql = "SELECT 
            s.student_id,
            s.name,
            s.regNumber,
            s.fees_paid,
            s.fees_outstanding,
            c.class_name
        FROM students s
        JOIN classes c ON s.class_id = c.class_id
        WHERE c.teacher_id = ?";

$params = [$teacher_id];
$types = "i";

/* FILTER LOGIC */
if ($filter === 'owing') {
    $sql .= " AND s.fees_outstanding > 0";
} elseif ($filter === 'paid') {
    $sql .= " AND s.fees_outstanding = 0";
}

$sql .= " ORDER BY s.name ASC";

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$students = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();

$conn->close();
?>

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

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
:root{
    --bg:#f4f6f9;
    --card:#fff;
    --text:#111;
    --sidebar:#0f172a;
}
body.dark{
    --bg:#0b1220;
    --card:#111827;
    --text:#e5e7eb;
    --sidebar:#020617;
}
body{
    margin:0;
    font-family:Segoe UI;
    background:var(--bg);
    color:var(--text);
}

/* SIDEBAR */
.sidebar{
    position:fixed;
    width:260px;
    height:100%;
    background:var(--sidebar);
    padding-top:20px;
}
.sidebar a{
    display:block;
    padding:12px 20px;
    color:#cbd5e1;
    text-decoration:none;
}
.sidebar a:hover{
    background:#1e293b;
    color:#fff;
}

/* MAIN */
.main{
    margin-left:260px;
    padding:20px;
}

/* CARD */
.card-box{
    background:var(--card);
    padding:20px;
    border-radius:12px;
    box-shadow:0 5px 15px rgba(0,0,0,0.08);
}

/* MOBILE */
.menu-btn{display:none;}
@media(max-width:768px){
    .sidebar{transform:translateX(-100%);}
    .sidebar.active{transform:translateX(0);}
    .main{margin-left:0;}
    .menu-btn{display:block;font-size:24px;}
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar" id="sidebar">
    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="attendance.php">Attendance</a>
    <a href="add_homework.php">Assignments</a>
    <a href="feedback_grading.php">Grades</a>
    <a href="class_timetable.php">Timetable</a>
    <a href="profile.php">Profile</a>
    <a href="debtors.php">Debtors</a>
    <a href="logout.php">Logout</a>
</div>

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

<!-- TOP BAR -->
<div class="d-flex justify-content-between mb-3">
    <span class="menu-btn" onclick="toggleSidebar()">☰</span>
    <button class="btn btn-dark btn-sm" onclick="toggleDark()">🌙 Dark</button>
</div>

<div class="card-box">

<h4>Student Debtors</h4>

<!-- FILTER -->
<form method="GET" class="mb-3">
    <select name="filter" onchange="this.form.submit()" class="form-select w-25">
        <option value="all" <?= $filter=='all'?'selected':'' ?>>All Students</option>
        <option value="owing" <?= $filter=='owing'?'selected':'' ?>>Owing</option>
        <option value="paid" <?= $filter=='paid'?'selected':'' ?>>Fully Paid</option>
    </select>
</form>

<!-- TABLE -->
<table class="table table-bordered">
<thead>
<tr>
    <th>Name</th>
    <th>Reg No</th>
    <th>Class</th>
    <th>Paid (₦)</th>
    <th>Outstanding (₦)</th>
    <th>Status</th>
</tr>
</thead>

<tbody>
<?php if(empty($students)): ?>
<tr><td colspan="6" class="text-center">No records found</td></tr>
<?php else: ?>
<?php foreach($students as $s): ?>
<tr>
    <td><?= htmlspecialchars($s['name']) ?></td>
    <td><?= htmlspecialchars($s['regNumber']) ?></td>
    <td><?= htmlspecialchars($s['class_name']) ?></td>
    <td>₦<?= number_format($s['fees_paid']) ?></td>
    <td>₦<?= number_format($s['fees_outstanding']) ?></td>
    <td>
        <?php if($s['fees_outstanding'] > 0): ?>
            <span class="badge bg-danger">Owing</span>
        <?php else: ?>
            <span class="badge bg-success">Paid</span>
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>

</div>
</div>

<script>
function toggleSidebar(){
    document.getElementById("sidebar").classList.toggle("active");
}

function toggleDark(){
    document.body.classList.toggle("dark");
    localStorage.setItem("dark", document.body.classList.contains("dark"));
}

if(localStorage.getItem("dark")==="true"){
    document.body.classList.add("dark");
}
</script>

</body>
</html>