<?php

include('phpClasses/Course.php');
$course = new Course();

if (isset($_POST['coscode_check'])) {
    $cos_code =  filter_var($_POST['coscode'], FILTER_SANITIZE_STRING);
   
    $avail = $course->checkCsCode($cos_code);
    if ($avail == 'taken') {
        echo "taken";
        exit();
    } else {
        echo 'not_taken';
        exit();
    }
    exit();
}

if (isset($_POST['save'])) {
    $dep_code =  filter_var($_POST['depcode'], FILTER_SANITIZE_STRING);
    $cos_code = filter_var($_POST['coscode'], FILTER_SANITIZE_STRING);
    $cos_unit = filter_var($_POST['cosunit'], FILTER_SANITIZE_STRING);
    $description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);
    $avail = $course->checkCsCode($cos_code);
    if ($avail == 'taken') {
        echo "exists";
        exit();
    } else {
        $course->insertCourse(trim($dep_code),trim($cos_code), $description, $cos_unit);
        echo 'Saved!';
        exit();
    }
}
?>
