<?php
header('Access-Control-Allow-Origin: *');
session_start();

if (!$_SESSION["manu"]) {
    http_response_code(401);
    die();
}

$manu = $_SESSION["manu"];

//var_dump(json_decode(file_get_contents("php://input")));
//$postData = json_decode(file_get_contents("php://input"));

//$mysqli = new mysqli("localhost", "afxtech1_myori", "myori2017", "afxtech1_myori");
include_once("db_connect.php");
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 


//Fist, check if we have any file sent to the script:
$file = isset($_FILES['namefile']) ? $_FILES['namefile'] : NULL;
$nfc = isset($_POST['id']) ? $_POST['id'] : NULL;
$remark = isset($_POST['remark']) ? $_POST['remark'] : NULL;
    
//It seems we have a file?
$tmpfile = $file['tmp_name'];
//Find the file extension:
$fileparts = explode('.', $file['name']);

//Apply the extension, usually the last item in the parts array:
$fileExtension = $fileparts[count($fileparts) - 1];

//Make a filename:
$filename = md5($file['name']);

//add file extension to the filename:
$filename .= '.' . $fileExtension;

//Save to current directory:
$saveto = __DIR__ . DIRECTORY_SEPARATOR . "cocs/" . $filename;

//Move file:
move_uploaded_file($tmpfile, $saveto);

///If we ever get here, the file has been uploaded, saved and moved to the permanent location
//print 'File saved to: ' . $base64;
//print $base64;

$query = "INSERT cocs (remark, filename, nfc, manu) VALUES (
            '" .$remark. "', 
            '" .$filename. "', 
            '" .$nfc. "', 
            '" .$manu. "'
            ) ";

if ($mysqli->query($query) === true) {
    echo "Success: Record updated. [".$query."]";
	http_response_code(200);
} else {
	echo "Error: Process failed - " . $mysqli->error . "[".$query."]";
	http_response_code(401);
}


/*
echo "Success: Record updated. [".$query."]";
http_response_code(200);
*/
$mysqli->close();


?>