<?php include("../database/auth_check.php"); ?>

<?php
$con = mysqli_connect("localhost","root","","WSAS");
if(!$con){
    die("Connection failed");
}

if (!isset($_GET['subject_id']) || !isset($_GET['course_id'])) {
    die("Missing subject or course ID");
}

$subject_id = $_GET['subject_id'];
$course_id = $_GET['course_id'];

if(isset($_POST['save_prereq'])){

    $post_subject_id = $_POST['subject_id'];
    $post_course_id = $_POST['course_id'];

    mysqli_query($con, "DELETE FROM subject_prerequisite WHERE subject_id='$post_subject_id'");

    if(isset($_POST['prereq'])){
        foreach($_POST['prereq'] as $prerequisite_id){
            mysqli_query($con, "
                INSERT INTO subject_prerequisite (subject_id, prerequisite_id)
                VALUES ('$post_subject_id','$prerequisite_id')
            ");
        }
    }
header("Location: ../view/subject_view.php?subject_id=$post_subject_id");
exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Subject Prerequisite - Admin</title>
<link rel="shortcut icon" type="x-icon" href="../images/logo.png">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
<link rel="stylesheet" href="../Style/navstyle.css">
<style>
/* Dashboard layout */
.dashboard-container {
    display: flex;
    min-height: 100vh;
    font-family: "Segoe UI", sans-serif;
    background: #f4f6f8;
}

.mainbar {
    margin-left: 260px;
    width: calc(100% - 260px);
    flex: 1;
    display: flex;
    flex-direction: column;
}

.dashboard-btn {
    margin: 0; /* Remove top margin */
    font-size: 14px;
    padding: 8px 16px;
}

.open-btn {
    display: inline-block;
    background-color: #4a9c3f;
    color: #fff;
    padding: 10px 18px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    margin-top: 10px;
    text-decoration: none;
    transition: background 0.3s;
}
.open-btn:hover { background-color: #3e8c36; }

form {
    margin-top: 20px;
    background: #f9f9f9;
    padding: 20px;
    border-radius: 10px;
}

form label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
    color: #555;
}

form input[type="number"] {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border: 1px solid #ccc;
    border-radius: 6px;
}

form button {
    width: 100%;
    padding: 12px;
    background: #4a9c3f;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 15px;
    cursor: pointer;
}
form button:hover { background: #43a047; }

/* Table styling */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

table th, table td {
    padding: 12px;
    border: 1px solid #ccc;
    text-align: center;
}

table th {
    background-color: #2b5820;
    color: white;
    font-weight: 600;
}

table tr:nth-child(even) {
    background: #f8f8f8;
}

table tr:hover {
    background: rgba(74,156,63,0.1);
    transition: 0.3s;
}

/* Responsive */
@media (max-width:1024px){
    .mainbar { margin-left:0; margin-top:70px; }
}

.modal {
    display: none;
    position: fixed;
    z-index: 999;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    justify-content: center;
    align-items: center;
}

.modal-content {
    background: #fff;
    padding: 20px 25px;
    border-radius: 10px;
    text-align: center;
    width: 300px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.modal-actions {
    margin-top: 15px;
    display: flex;
    justify-content: space-between;
    gap: 10px;
}

.modal-actions button {
    flex: 1;
    padding: 10px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

#confirmYes {
    background-color: #4a9c3f;
    color: white;
}

#confirmNo {
    background-color: #ccc;
}
</style>
</head>
<body>
<div class="dashboard-container">
    <?php include('adminnav.php'); ?>
    <aside class="mainbar">

        <form method="POST">

        <input type="hidden" name="subject_id" value="<?php echo $subject_id; ?>">
        <input type="hidden" name="course_id" value="<?php echo $course_id; ?>">

        <label>Select Prerequisites:</label><br>

        <?php
        // GET CURRENT SUBJECT YEAR + SEM
        $current = mysqli_fetch_assoc(mysqli_query($con, "
            SELECT year_level, semester
            FROM course_subject
            WHERE subject_id = '$subject_id'
            AND course_id = '$course_id'
        "));

        // GET VALID PREREQUISITES
        $prereq_q = mysqli_query($con, "
            SELECT cs.subject_id, a.subject_code, a.subject_name, cs.year_level, cs.semester
            FROM course_subject cs
            JOIN adminsubject a ON cs.subject_id = a.subject_id
            WHERE cs.course_id = '$course_id'
            AND a.is_major = 1
            AND (
                cs.year_level < '{$current['year_level']}' 
                OR
                (cs.year_level = '{$current['year_level']}' AND cs.semester < '{$current['semester']}')
            )
            ORDER BY cs.year_level ASC, cs.semester ASC
        ");

        // OPTIONAL: get already selected prereq
        $existing = [];
        $existing_q = mysqli_query($con, "
            SELECT prerequisite_id FROM subject_prerequisite WHERE subject_id='$subject_id'
        ");
        while($e = mysqli_fetch_assoc($existing_q)){
            $existing[] = $e['prerequisite_id'];
        }

        while($row = mysqli_fetch_assoc($prereq_q)){
            $checked = in_array($row['subject_id'], $existing) ? "checked" : "";
            echo "
                <label>
                    <input type='checkbox' name='prereq[]' value='{$row['subject_id']}' $checked>
                    {$row['subject_code']} - {$row['subject_name']}
                </label><br>
            ";
        }
        ?>

        <button type="button" id="openModalBtn">Save</button>

        </form>
<div id="confirmModal" class="modal">
  <div class="modal-content">
    <h3>Confirm Action</h3>
    <p>Are you sure you want to save these prerequisites?</p>
    <div class="modal-actions">
      <button id="confirmYes" name="save_prereq">Yes, Save</button>
      <button id="confirmNo">Cancel</button>
    </div>
  </div>
</div>
       
    </aside>
</div>

<script>
const modal = document.getElementById("confirmModal");
const openBtn = document.getElementById("openModalBtn");
const yesBtn = document.getElementById("confirmYes");
const noBtn = document.getElementById("confirmNo");
const form = document.querySelector("form");

openBtn.addEventListener("click", () => {
    modal.style.display = "flex";
});

noBtn.addEventListener("click", () => {
    modal.style.display = "none";
});

yesBtn.addEventListener("click", () => {
    let input = document.createElement("input");
    input.type = "hidden";
    input.name = "save_prereq";
    input.value = "1";
    form.appendChild(input);

    form.submit();
});
</script>

</body>
</html>