<?php
$con = mysqli_connect("localhost","root","","WSAS");

if (!$con) {
    die("Connection failed");
}

$student_id = $_POST['student_id'];
$done_subjects = $_POST['done_subjects'] ?? [];

$course_query = mysqli_query($con, "
    SELECT course_id FROM adminstudent WHERE student_id = '$student_id'
");
$course = mysqli_fetch_assoc($course_query);
$course_id = $course['course_id'];

$subjects = mysqli_query($con, "
    SELECT subject_id FROM course_subject WHERE course_id = '$course_id'
");

while($row = mysqli_fetch_assoc($subjects)){
    $subject_id = $row['subject_id'];

    $status = in_array($subject_id, $done_subjects) ? 'completed' : 'not_taken';

    mysqli_query($con, "
        INSERT INTO student_curriculum (student_id, subject_id, sc_status)
        VALUES ('$student_id', '$subject_id', '$status')
    ");
}

header("Location: ../adminpanel/adminstudents.php?success=1");
exit;
?>