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

if (!isset($_SESSION['user_id'])) {
    echo "User not logged in.";
    exit;
}

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

$user_id = $_SESSION['user_id'];
$record_id = $_POST['record_id'];
$type = $_POST['type'];
$target_dir = __DIR__ . "/../../jobcards/pump/signatures/";

if ($_FILES["file"]["type"] !== "image/png") {
    echo "Invalid file type. Only PNG allowed.";
    exit;
}

$target_file = $target_dir . "$type-jc_$record_id-$user_id-signature.png";

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "OK";
} else {
    echo "Failed to move uploaded file.";
}

return 1;