<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
ini_set ("display_errors", "0");
error_reporting(E_ALL);
require("../connect.php");
include './funcConvertToSecond.php';
date_default_timezone_set('Asia/Jakarta');

$sqlcheck  = " SELECT * FROM app_entity_63 WHERE ";
$sqlcheck .= " field_949 = '".$_POST['nodoc']."';";

$checkresult = $conn->query($sqlcheck);

if ($checkresult->num_rows == 0) {
    
    $pickupdate = strtotime($_POST['harvestdate']);
    $pickuptime = timeToSeconds($_POST['pickuptime']);
    $pointMapSPB = "[map]".$_POST['latspb'].",".$_POST['longspb']."(".$_POST['nodoc'].")[/map]";
    $filename_db = time()."_"."SPB".$_POST['addedby'].$_POST['vehicleid'].$_POST['driverid'].rand(1,99).date("YmdHis",time()).".png";

    $queryUploadSPB = "INSERT INTO app_entity_63 
    (parent_item_id, date_added, created_by, field_939, field_938, field_937, 
    field_934, field_935, field_936, field_929, field_931, field_1139, 
    field_949, field_933, field_1135, field_940, field_950, field_951, 
    field_945, field_932, field_942, field_943, field_946, field_947, 
    field_948, field_1137, field_2008) VALUES (?, UNIX_TIMESTAMP(), ?, ?, 
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    
    $stmtUploadSPB = mysqli_prepare($conn, $queryUploadSPB);
    mysqli_stmt_bind_param($stmtUploadSPB, 'iiissssssissssiiisssssssss', $_POST['vehicleid'], 
    $_POST['uploadby'], $_POST['addedby'], $_POST['holding'], $_POST['region'], 
    $_POST['company'], $_POST['site'], $_POST['estate'], $_POST['divisioncode'], 
    $_POST['divisionid'], $_POST['kuponpanen'], $_POST['nodoc'], $pickupdate, $pickuptime, 
    $_POST['harvester'], $_POST['driverid'], $_POST['vehicleid'], $_POST['loaders'], 
    $_POST['blockid'], $_POST['tphid'], $_POST['qrspb'], $_POST['latspb'], 
    $_POST['longspb'], $pointMapSPB, $filename_db, $_POST['jjgangkut']);
    
    if (mysqli_stmt_execute($stmtUploadSPB)) {
        
        echo json_encode(array("UPLOADSPB"=>"SUCCESS"));
        
        $lastInsertID = mysqli_insert_id($conn); // Getting last ID
        
        // Inserting to values app_entity_63_values
        mysqli_query($conn, "INSERT INTO app_entity_63_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 931, ".$_POST['divisionid'].");");
        mysqli_query($conn, "INSERT INTO app_entity_63_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 939, ".$_POST['addedby'].");");
        mysqli_query($conn, "INSERT INTO app_entity_63_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 950, ".$_POST['driverid'].");");
        mysqli_query($conn, "INSERT INTO app_entity_63_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 951, ".$_POST['vehicleid'].");");
        
        $stmtImgSPB = mysqli_query($conn, "SELECT field_1137 FROM app_entity_63 WHERE id = ".$lastInsertID.";");
    	$rowImgSPB = mysqli_fetch_assoc($stmtImgSPB);
    	
    	if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m"))) {
            mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m"));
            if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) {
                mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"));
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".$rowImgSPB['field_1137'], "w+");
    	        fwrite($fp, base64_decode($_POST['spbimg']));
            } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) { 
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".$rowImgSPB['field_1137'], "w+");
    	        fwrite($fp, base64_decode($_POST['spbimg']));
            }
        } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m"))) {
            if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) {
                mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"));
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".$rowImgSPB['field_1137'], "w+");
    	        fwrite($fp, base64_decode($_POST['spbimg']));
            } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) { 
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".$rowImgSPB['field_1137'], "w+");
    	        fwrite($fp, base64_decode($_POST['spbimg']));
            }
        }
    	
        mysqli_close($conn);
        	
    }

} else if ($checkresult->num_rows > 0) {
	echo json_encode(array("UPLOADSPB"=>"SUCCESS"));
	mysqli_close($conn);
}
?>