<?php

include('phpClasses/Department.php');
$department = new Department();

if (isset($_POST['depcode_check'])) {
    $dep_code =  filter_var($_POST['depcode'], FILTER_SANITIZE_STRING);
   
    $avail = $department->checkDpCode($dep_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);
    $fal_code = filter_var($_POST['falcode'], FILTER_SANITIZE_STRING);
    $description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);
    $avail = $department->checkDpCode($dep_code);
    if ($avail == 'taken') {
        echo "exists";
        exit();
    } else {
        $department->insertDepartment($dep_code,trim($fal_code), $description);
        echo 'Saved!';
        exit();
    }
}
?>
