<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();
session_start();

$date = date("Y-m-d H:i", strtotime("+2 Hours"));

//date
$timestamp = strtotime($date);
$current_date = date("Y-m-d");

// Make sure user is logged in and file is uploaded
if (!isset($_SESSION['user_id'])) {
    echo "User not logged in.";
    exit;
}

if (!isset($_FILES['file'])) {
    echo "No file uploaded.";
    exit;
}

try {

    if ($type == "received_by_signature") {

        $received_by = $_POST['received_by'];
        $target_file = "../consumables/signatures/visitor{$received_by}-training-{$current_date}.png";

        if (file_exists($target_file)) {
            // $operations_manager_no++;
            $target_file = "../consumables/signatures/received_by_{$received_by}-consumables_{$current_date}.png";
        } else {
            $target_file = "../consumables/signatures/received_by_{$received_by}-consumables_{$current_date}.png";
        }

        // Optional: Validate the file type
        if ($_FILES["file"]["type"] !== "image/png") {
            echo "Invalid file type. Only PNG allowed.";
            exit;
        }

        // Move the uploaded file
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "OK";
        } else {
            echo "Failed to move uploaded file.";
        }

        $targetDir = "../consumables/uploads/";
        $targetFile = $targetDir . basename($_FILES["image"]["name"]);

        if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
            //$sql = $db->query("","UPDATE consumables SET image_path = '$targetFile' WHERE ");

            if ($sql) {
                echo "Image uploaded and path saved to database.";
            } else {
                // echo "DB Error: " . $conn->error;
            }
        } else {
            echo "Error uploading image.";
        }
    }

    if ($type == "received_from_signature") {

        $received_from = $_POST['received_from'];
        $target_file = "../consumables/signatures/visitor{$received_from}-training-{$current_date}.png";

        if (file_exists($target_file)) {
            // $operations_manager_no++;
            $target_file = "../consumables/signatures/received_from{$received_from}-training_{$current_date}.png";
        } else {
            $target_file = "../consumables/signatures/received_from{$received_from}-training_{$current_date}.png";
        }

        // Optional: Validate the file type
        if ($_FILES["file"]["type"] !== "image/png") {
            echo "Invalid file type. Only PNG allowed.";
            exit;
        }

        // Move the uploaded file
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "OK";
        } else {
            echo "Failed to move uploaded file.";
        }

        $targetDir = "../consumables/uploads/";
        $targetFile = $targetDir . basename($_FILES["image"]["name"]);

        if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
            //$sql = $db->query("","UPDATE consumables SET image_path = '$targetFile' WHERE ");

            if ($sql) {
                echo "Image uploaded and path saved to database.";
            } else {
                // echo "DB Error: " . $conn->error;
            }
        } else {
            echo "Error uploading image.";
        }
    }

    return 1;

} catch (Exception $e) {
    echo $e->getMessage();
    exit;
}