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

/* =========================
   FETCH DATA
========================= */
$classes = $conn->query("SELECT class_id, class_name FROM classes ORDER BY class_name ASC");
$subjects = $conn->query("SELECT subject_id, subject_name FROM subject ORDER BY subject_name ASC");

/* =========================
   HANDLE UPDATE
========================= */
if (isset($_POST['update_frequency'])) {

    $id = $_POST['id'];
    $frequency = $_POST['frequency'];

    $stmt = $conn->prepare("
        UPDATE class_subject
        SET frequency = ?
        WHERE id = ?
    ");

    $stmt->bind_param("ii", $frequency, $id);
    $stmt->execute();

    echo "<script>alert('Updated successfully');</script>";
}

/* =========================
   HANDLE DELETE
========================= */
if (isset($_GET['delete'])) {

    $id = $_GET['delete'];

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

    echo "<script>alert('Deleted successfully');</script>";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Assign Subjects to Class | ERP</title>

<style>
body {
    font-family: Arial;
    background: #eef2f7;
    margin: 0;
}

/* HEADER */
.header {
    background: #1e293b;
    color: white;
    text-align: center;
    padding: 20px;
}

/* CONTAINER */
.container {
    width: 50%;
    margin: 30px auto;
}

/* CARD */
.card {
    background: white;
    padding: 25px;
    border-radius: 10px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.1);
}

/* FORM */
label {
    display: block;
    margin-top: 12px;
    font-weight: bold;
}

select, input {
    width: 100%;
    padding: 10px;
    margin-top: 6px;
    border: 1px solid #ccc;
    border-radius: 6px;
}

/* BUTTON */
button {
    margin-top: 15px;
    width: 100%;
    padding: 12px;
    background: #16a34a;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

button:hover {
    background: #15803d;
}

/* TABLE */
.table-box {
    margin-top: 30px;
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.1);
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    background: #1e293b;
    color: white;
    padding: 10px;
}

td {
    padding: 10px;
    border-bottom: 1px solid #ddd;
    text-align: center;
}

/* ACTION BUTTONS */
.edit-btn {
    background: #2563eb;
    color: white;
    padding: 5px 10px;
    border-radius: 5px;
    text-decoration: none;
}

.delete-btn {
    background: #dc2626;
    color: white;
    padding: 5px 10px;
    border-radius: 5px;
    text-decoration: none;
}

.edit-btn:hover { background: #1d4ed8; }
.delete-btn:hover { background: #b91c1c; }

/* INLINE FORM */
.inline-form {
    display: flex;
    gap: 5px;
    justify-content: center;
}
</style>
</head>

<body>

<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>
    <p>Assign Subjects to Class (ERP Module)</p>
</div>

<div class="container">

<!-- ASSIGN FORM -->
<div class="card">

<h3>Assign Subject to Class</h3>

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

<label>Class</label>
<select name="class_id" required>
    <option value="">Select Class</option>
    <?php while($c = $classes->fetch_assoc()): ?>
        <option value="<?= $c['class_id'] ?>">
            <?= htmlspecialchars($c['class_name']) ?>
        </option>
    <?php endwhile; ?>
</select>

<label>Subject</label>
<select name="subject_id" required>
    <option value="">Select Subject</option>
    <?php while($s = $subjects->fetch_assoc()): ?>
        <option value="<?= $s['subject_id'] ?>">
            <?= htmlspecialchars($s['subject_name']) ?>
        </option>
    <?php endwhile; ?>
</select>

<label>Frequency (Times per Week)</label>
<input type="number" name="frequency" min="1" max="10" value="1" required>

<button type="submit">Assign Subject</button>

</form>

</div>

<!-- TABLE -->
<div class="table-box">

<h3>Existing Assignments</h3>

<table>
<tr>
    <th>Class</th>
    <th>Subject</th>
    <th>Frequency</th>
    <th>Actions</th>
</tr>

<?php
$sql = "
SELECT cs.id, c.class_name, s.subject_name, cs.frequency
FROM class_subject cs
JOIN classes c ON cs.class_id = c.class_id
JOIN subject s ON cs.subject_id = s.subject_id
ORDER BY c.class_name
";

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

while ($row = $result->fetch_assoc()):
?>

<tr>
    <td><?= htmlspecialchars($row['class_name']) ?></td>
    <td><?= htmlspecialchars($row['subject_name']) ?></td>

    <!-- INLINE EDIT FORM -->
    <td>
        <form method="POST" class="inline-form">
            <input type="hidden" name="id" value="<?= $row['id'] ?>">
            <input type="number" name="frequency" value="<?= $row['frequency'] ?>" min="1" max="10" style="width:70px;">
            <button type="submit" name="update_frequency">Save</button>
        </form>
    </td>

    <td>
        <a class="delete-btn" href="?delete=<?= $row['id'] ?>"
           onclick="return confirm('Delete this assignment?')">Delete</a>
    </td>
</tr>

<?php endwhile; ?>

</table>

</div>

</div>

</body>
</html>