<?php 
require("../connect.php");
include './funcConvertToSecond.php';
date_default_timezone_set('Asia/Jakarta');

$sqlcheck  = " SELECT * FROM app_entity_71 WHERE ";
$sqlcheck .= " parent_item_id = '".$_POST['spbid']."';";

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

if ($checkresult->num_rows == 0) {
    
    $dateWeightIN = strtotime($_POST['indate']);
    $dateWeightOUT = strtotime($_POST['outdate']);
    $timeWeightIN = timeToSeconds($_POST['intime']);
    $timeWeightOUT = timeToSeconds($_POST['outtime']);
    $datetimeWeightIN = strtotime($_POST['indate'].' '.$_POST['intime']);
    $datetimeWeightOUT = strtotime($_POST['outdate'].' '.$_POST['outtime']);
    $filename_db = time().'_'.str_replace('/', '', $_POST['nodocwb']).date("YmdHis",time()).".png";
    $pointMapWB = "[map]".$_POST['latwb'].",".$_POST['longwb']."(".$_POST['nodocwb'].")[/map]";
    
	$queryUploadWB = "INSERT INTO app_entity_71 
	(parent_item_id, date_added, created_by, field_1202, 
	field_2926, field_1215, field_1152, field_1140, 
	field_1141, field_1142, field_1143, field_1144, 
	field_1145, field_1146, field_1147, field_1148, 
	field_1149, field_1150, field_1151, field_2921, 
	field_1206, field_1207, field_1261, field_1208) VALUES 
	(?, UNIX_TIMESTAMP(), ?, ?, 
	?, ?, ?, ?, 
	?, ?, ?, ?, 
	?, ?, ?, ?, 
	?, ?, ?, ?, 
	?, ?, ?, ?)";
    
    $stmtUploadWB = mysqli_prepare($conn, $queryUploadWB);
    mysqli_stmt_bind_param($stmtUploadWB, 'iiiiissssssssssssssssss', 
    $_POST['spbid'], $_POST['uploadby'], $_POST['addedby'], 
    $_POST['timbangloc'], $_POST['vehicleid'], $_POST['nodocwb'], $dateWeightIN, 
    $timeWeightIN, $datetimeWeightIN, $dateWeightOUT, $timeWeightOUT, 
    $datetimeWeightOUT, $_POST['weightin'], $_POST['weightout'], $_POST['penaltytetap'], 
    $_POST['buahbusuk'], $_POST['buahmentah'], $_POST['tangkaipanjang'], $_POST['penaltysortasi'], 
    $_POST['latwb'], $_POST['longwb'], $filename_db, $pointMapWB);
        
    if (mysqli_stmt_execute($stmtUploadWB)) {
        
        echo json_encode(array("UPLOADWB"=>"SUCCESS"));
        
        $lastInsertID = mysqli_insert_id($conn); // Getting last ID
        
        // Inserting to values app_entity_71_values
        mysqli_query($conn, "INSERT INTO app_entity_71_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 1202, ".$_POST['addedby'].");");
        mysqli_query($conn, "INSERT INTO app_entity_71_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 1215, ".$_POST['vehicleid'].");");
        mysqli_query($conn, "INSERT INTO app_entity_71_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 2926, ".$_POST['timbangloc'].");");
        
        $stmtImgWB = mysqli_query($conn, "SELECT field_1261 FROM app_entity_71 WHERE id = ".$lastInsertID."");
		$rowImgWB = mysqli_fetch_assoc($stmtImgWB);
		
		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")."/".$rowImgWB['field_1261'], "w+");
		        fwrite($fp, base64_decode($_POST['wbimg']));
            } 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")."/".$rowImgWB['field_1261'], "w+");
		        fwrite($fp, base64_decode($_POST['wbimg']));
            }
        } 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")."/".$rowImgWB['field_1261'], "w+");
		        fwrite($fp, base64_decode($_POST['wbimg']));
            } 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")."/".$rowImgWB['field_1261'], "w+");
		        fwrite($fp, base64_decode($_POST['wbimg']));
            }
        }
		
  	} 
      
} else if ($checkresult->num_rows > 0) {
	echo json_encode(array("UPLOADWB"=>"SUCCESS"));
}
?>