<?php include "../../root.class.php";
$db = new db_safeguard();

// var_dump($_FILES);
try {

    if (!isset($_FILES['file'])) {
        echo "No file uploaded.";
        exit;
    }

    $target_dir = "../sorting/signatures/";
    $files = glob($target_dir . "*");
    $files = array_filter($files, 'is_file');

    $count = count($files) + 1;
    $type = $_POST['type'] ?? '';

    if ($type = 'production_supervisor') {
        $target_file = $target_dir . "{$count}-sorting-production-supervisor-{$_SESSION['user_id']}-" . basename($_FILES["file"]["name"]);
    } 

    if ($type = 'quality_assurance') {
        $target_file = $target_dir . "{$count}-sorting-quality-assurance-{$_SESSION['user_id']}-" . basename($_FILES["file"]["name"]);
    }

    // $target_file = $target_dir . "{$count}-sorting-jobcard-{$_SESSION['user_id']}-" . basename($_FILES["file"]["name"]);

    move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
    echo "SIGNATURE SAVED";
    echo $_FILES["file"]["tmp_name"];

} catch (Exception $e) {
    echo $e->getMessage();
}
