<?php
require_once 'db_connection.php';

/* =========================
   FETCH DATA
========================= */

// Fetch students
$students = [];
$result = $conn->query("SELECT student_id, name FROM students ORDER BY name ASC");
if ($result && $result->num_rows > 0) {
    $students = $result->fetch_all(MYSQLI_ASSOC);
}

// Fetch subjects
$subjects = [];
$result = $conn->query("SELECT subject_id, subject_name FROM subject ORDER BY subject_name ASC");
if ($result && $result->num_rows > 0) {
    $subjects = $result->fetch_all(MYSQLI_ASSOC);
}

// Fetch classes
$classes = [];
$result = $conn->query("SELECT class_id, class_name FROM classes ORDER BY class_name ASC");

if ($result && $result->num_rows > 0) {
    $classes = $result->fetch_all(MYSQLI_ASSOC);
}
// DELETE ENROLLMENT
if (isset($_GET['delete_enrollment'])) {
    $id = $_GET['delete_enrollment'];

    $stmt = $conn->prepare("DELETE FROM enrollments WHERE id = ?");
    $stmt->bind_param("i", $id);

    if ($stmt->execute()) {
        header("Location: enroll.php?success=Enrollment removed");
        exit();
    } else {
        header("Location: enroll.php?error=Delete failed");
        exit();
    }
}
?>

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

<style>

/* =========================
   GLOBAL STYLES
========================= */
body {
    font-family: Arial, sans-serif;
    background: #f4f6f9;
    margin: 0;
}

/* =========================
   HEADER
========================= */
.header {
    background: #1e293b;
    color: white;
    padding: 15px 20px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

.header-container {
    max-width: 1100px;
    margin: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo h2 {
    margin: 0;
    font-size: 20px;
}

.nav {
    display: flex;
    gap: 15px;
}

.nav a {
    color: white;
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 14px;
}

.nav a:hover {
    background: rgba(255,255,255,0.1);
}

.nav a.active {
    background: #2563eb;
}

.nav a.logout {
    background: #ef4444;
}

/* =========================
   MAIN CONTAINER
========================= */
.container {
    max-width: 600px;
    margin: 40px auto;
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.08);
}

/* =========================
   BACK BUTTON
========================= */
.back-btn {
    display: inline-block;
    margin-bottom: 15px;
    background: #111827;
    color: white;
    padding: 10px 15px;
    border-radius: 8px;
    text-decoration: none;
}

.back-btn:hover {
    background: #000;
}

/* =========================
   FORM STYLES
========================= */
h2 {
    margin-bottom: 20px;
    color: #1e293b;
}

label {
    display: block;
    margin-top: 15px;
    font-weight: bold;
    color: #374151;
}

select {
    width: 100%;
    padding: 10px;
    margin-top: 6px;
    border-radius: 8px;
    border: 1px solid #d1d5db;
    outline: none;
}

select:focus {
    border-color: #2563eb;
}

button {
    width: 100%;
    padding: 12px;
    margin-top: 20px;
    border-radius: 8px;
    border: none;
    background: #2563eb;
    color: white;
    font-weight: bold;
    cursor: pointer;
}

button:hover {
    background: #1d4ed8;
}

/* =========================
   ALERT MESSAGES
========================= */
.message {
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 8px;
}

.success {
    background: #ecfdf5;
    color: #065f46;
    border: 1px solid #10b981;
}

.error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #ef4444;
}

.enroll-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
    font-size: 14px;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
}

.enroll-table th {
    background: #1e293b;
    color: white;
    padding: 12px;
    text-align: left;
}

.enroll-table td {
    padding: 12px;
    border-bottom: 1px solid #e5e7eb;
}

.enroll-table tr:hover {
    background: #f8fafc;
}

.delete-btn {
    background: #ef4444;
    color: white;
    padding: 6px 10px;
    border-radius: 6px;
    text-decoration: none;
    font-size: 12px;
}

.delete-btn:hover {
    background: #dc2626;
}
</style>
</head>

<body>

<!-- ================= HEADER ================= -->
<div class="header">
    <div class="header-container">
        <div class="logo">
            <h2>AGID EDU TECH</h2>
        </div>

        <div class="nav">
            <a href="admin_dashboard.php">Dashboard</a>
            <a href="#" class="active">Enroll</a>
            <a href="manage_students.php">Students</a>
            <a href="add_subject.php">Subjects</a>
            <a href="logout.php" class="logout">Logout</a>
        </div>
    </div>
</div>

<!-- ================= MAIN CONTENT ================= -->
<div class="container">

<a href="admin_dashboard.php" class="back-btn">← Back to Dashboard</a>

<h2>Enroll Student in Subject</h2>

<?php if (isset($_GET['success'])): ?>
    <div class="message success">Enrollment added successfully.</div>
<?php elseif (isset($_GET['error'])): ?>
    <div class="message error"><?php echo htmlspecialchars($_GET['error']); ?></div>
<?php endif; ?>

<form method="POST" action="process_enrollment.php">

    <label for="student_id">Select Student</label>
    <select name="student_id" required>
        <option value="">-- Select Student --</option>
        <?php foreach ($students as $student): ?>
            <option value="<?php echo $student['student_id']; ?>">
                <?php echo htmlspecialchars($student['name']); ?>
            </option>
        <?php endforeach; ?>
    </select>

   <label for="subject_id">Select Subjects</label>
<select name="subject_id[]" multiple required>
    <?php foreach ($subjects as $subject): ?>
        <option value="<?php echo $subject['subject_id']; ?>">
            <?php echo htmlspecialchars($subject['subject_name']); ?>
        </option>
    <?php endforeach; ?>
</select>

<small>Hold CTRL (Windows) or CMD (Mac) to select multiple</small>

    <label for="class_id">Class</label>
<select name="class_id" required>
    <option value="">-- Select Class --</option>

    <?php if (!empty($classes)): ?>
        <?php foreach ($classes as $class): ?>
            <option value="<?php echo $class['class_id']; ?>">
                <?php echo htmlspecialchars($class['class_name']); ?>
            </option>
        <?php endforeach; ?>
    <?php else: ?>
        <option value="">No classes found</option>
    <?php endif; ?>

</select>

<label>Session</label>
<input type="text" name="session" placeholder="e.g. 2024/2025" required>

<label>Term</label>
<select name="term" required>
    <option value="">-- Select Term --</option>
    <option value="First Term">First Term</option>
    <option value="Second Term">Second Term</option>
    <option value="Third Term">Third Term</option>
</select>

    <button type="submit">Add Enrollment</button>

</form>
<h2 style="margin-top:40px;">Enrolled Students</h2>

<?php
$sql = "
SELECT 
    e.id,
    s.name AS student_name,
    sub.subject_name,
    c.class_name,
    e.session,
    e.term
FROM enrollments e
JOIN students s ON e.student_id = s.student_id
JOIN subject sub ON e.subject_id = sub.subject_id
JOIN classes c ON e.class_id = c.class_id
ORDER BY s.name ASC
";

$result = $conn->query($sql);
?>

<?php if ($result && $result->num_rows > 0): ?>

<div style="overflow-x:auto;">
<table class="enroll-table">

    <thead>
        <tr>
            <th>Student</th>
            <th>Subject</th>
            <th>Class</th>
            <th>Session</th>
            <th>Term</th>
            <th>Action</th>
        </tr>
    </thead>

    <tbody>
        <?php while ($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo htmlspecialchars($row['student_name']); ?></td>
            <td><?php echo htmlspecialchars($row['subject_name']); ?></td>
            <td><?php echo htmlspecialchars($row['class_name']); ?></td>
            <td><?php echo htmlspecialchars($row['session']); ?></td>
            <td><?php echo htmlspecialchars($row['term']); ?></td>
            <td>
                <a class="delete-btn"
                   href="?delete_enrollment=<?php echo $row['id']; ?>"
                   onclick="return confirm('Are you sure you want to remove this enrollment?')">
                   Remove
                </a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>

</table>
</div>

<?php else: ?>
    <p style="text-align:center; color:#6b7280;">
        No enrollments found.
    </p>
<?php endif; ?>
</div>

</body>
</html>