<?php
require_once(__DIR__ . "/../header.php");
require_once(__DIR__ . "/../../public/connection.php");

if (!isset($_SESSION['staff'])) {
    header("Location: ../../login.php");
    exit;
}

$teacher_id = $_SESSION['staff'];

// Fetch assignments issued by this teacher with submission count
$assignments_q = $connection->prepare("
    SELECT 
        a.id,
        a.title,
        a.subject,
        a.class,
        a.due_date,

        /* Count submissions for THIS assignment */
        (
            SELECT COUNT(*) 
            FROM submissions s 
            WHERE s.assignment_id = a.id
        ) AS total_submissions,

        /* Count students ONLY in the assignment's class */
        (
            SELECT COUNT(*) 
            FROM students st 
            WHERE st.class = a.class
        ) AS total_students

    FROM assignments a
    WHERE a.teacher_id = ?
    ORDER BY a.due_date DESC
");
$assignments_q->bind_param("i", $teacher_id);
$assignments_q->execute();
$assignments_res = $assignments_q->get_result();

$assignments = [];
while ($row = $assignments_res->fetch_assoc()) {
    $assignments[] = $row;
}

?>

<link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
.table-modern {
    border-radius: 0.75rem;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    border: none;
}

.table-modern thead {
    background: linear-gradient(90deg, #28a745, #198754);
    color: #fff;
    font-weight: 600;
    font-size: 0.95rem;
}

.table-modern tbody tr:hover {
    background-color: #e6f4ea;
    transition: background-color 0.2s ease-in-out;
}

.badge-modern {
    font-size: 0.85rem;
    padding: 0.35em 0.6em;
    border-radius: 0.5rem;
}

.card-modern {
    border-radius: 0.75rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

h3 i {
    color: #28a745;
}
</style>

<div class="container mt-4">
    <h3 class="mb-4"><i class="fa fa-file-alt me-2"></i>Assignment Submission Status</h3>

    <?php if(empty($assignments)): ?>
        <div class="alert alert-info mt-3">You haven't issued any assignments yet.</div>
    <?php else: ?>
        <div class="table-responsive card-modern p-3">
            <table class="table table-modern table-striped table-hover align-middle">
                <thead>
                    <tr>
                        <th>Assignment</th>
                        <th>Subject</th>
                        <th>Class</th>
                        <th>Due Date</th>
                        <th>Status</th>
                        <th>Submitted</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($assignments as $a):
                        $status = $a['total_submissions'] >= $a['total_students'] ? 'All Submitted' : 'Pending';
                        $badge_class = $a['total_submissions'] >= $a['total_students'] ? 'bg-success text-white' : 'bg-light text-success border border-success';
                    ?>
                        <tr>
                            <td><i class="fa fa-file-alt me-1 text-success"></i><?= htmlspecialchars($a['title']) ?></td>
                            <td><?= htmlspecialchars($a['subject']) ?></td>
                            <td><?= htmlspecialchars($a['class']) ?></td>
                            <td><?= date("d M Y", strtotime($a['due_date'])) ?></td>
                            <td><span class="badge badge-modern <?= $badge_class ?>"><?= $status ?></span></td>
                            <td><?= $a['total_submissions'] ?> / <?= $a['total_students'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
