<?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_156 WHERE ";
$sqlcheck .= " field_4652 = '".$_POST['nodoc']."';";

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

if ($checkresult->num_rows == 0) {
    
    $pickupdate = strtotime($_POST['pickupdate'].' '.$_POST['pickuptime']);
    $pointMapSPB = "[map]".$_POST['latspb'].",".$_POST['longspb']."(".$_POST['nodoc'].")[/map]";
    $filename_db = time()."_"."SPBT".$_POST['addedby'].$_POST['vehicleid'].$_POST['driverid'].rand(1,99).date("YmdHis",time()).".png";
    
    // Get region company info
    $queryCompInfo = "SELECT GROUP_CONCAT(CONCAT(\"'\", field_271, \"'\")) AS estate, GROUP_CONCAT(CONCAT(\"'\", field_270, \"'\")) AS site,
    GROUP_CONCAT(CONCAT(\"'\", field_269, \"'\")) AS comp, GROUP_CONCAT(CONCAT(\"'\", field_525, \"'\")) AS region, 
    (SELECT id FROM app_entity_30 WHERE field_263 = field_268) AS holding FROM app_entity_30 WHERE id IN (".$_POST['divisionid'].");";
    $stmtCompInfo = mysqli_query($conn, $queryCompInfo);
	$rowCompInfo = mysqli_fetch_assoc($stmtCompInfo);
	
	// Get estate 
	$queryEstate = "SELECT GROUP_CONCAT(id) AS id FROM app_entity_30 WHERE field_263 IN (".$rowCompInfo['estate'].");";
    $stmtEstate = mysqli_query($conn, $queryEstate);
	$rowEstate = mysqli_fetch_assoc($stmtEstate);
	
	// Get site
	$querySite = "SELECT GROUP_CONCAT(id) AS id FROM app_entity_30 WHERE field_263 IN (".$rowCompInfo['site'].");";
    $stmtSite = mysqli_query($conn, $querySite);
	$rowSite = mysqli_fetch_assoc($stmtSite);
	
	// Get comp
	$queryCompany = "SELECT GROUP_CONCAT(id) AS id FROM app_entity_30 WHERE field_263 IN (".$rowCompInfo['comp'].");";
    $stmtCompany = mysqli_query($conn, $queryCompany);
	$rowCompany = mysqli_fetch_assoc($stmtCompany);
	
	// Get region
	$queryRegion = "SELECT GROUP_CONCAT(id) AS id FROM app_entity_30 WHERE field_263 IN (".$rowCompInfo['region'].");";
    $stmtRegion = mysqli_query($conn, $queryRegion);
	$rowRegion = mysqli_fetch_assoc($stmtRegion);

    $queryUploadSPB = "INSERT INTO app_entity_156 
    (date_added, created_by, field_4646, field_4647,
    field_4648, field_4649, field_4650, field_4651, 
    field_4652, field_4653, field_4667, field_4668, 
    field_4669, field_4670, field_4671, field_4672, 
    field_4673, field_4674, field_4675, field_4676, field_4688)
    VALUES 
    (UNIX_TIMESTAMP(), ?, ?, ?,
    ?, ?, ?, ?, 
    ?, ?, ?, ?, 
    ?, ?, ?, ?, 
    ?, ?, ?, ?, ?);";
    
    $stmtUploadSPB = mysqli_prepare($conn, $queryUploadSPB);
    mysqli_stmt_bind_param($stmtUploadSPB, 'issssssssiiissssssss', 
    $_POST['uploadby'], $rowCompInfo['holding'], $rowRegion['id'], $rowCompany['id'], 
    $rowSite['id'], $rowEstate['id'], $_POST['divisionid'], $_POST['nodoc'], 
    $pickupdate, $_POST['vehicleid'], $_POST['addedby'], $_POST['driverid'], 
    $_POST['oldspb'], $_POST['totaljjg'], $_POST['oldvh'], $_POST['loaders'], 
    $_POST['latspb'], $_POST['longspb'], $pointMapSPB, $filename_db);
    
    if (mysqli_stmt_execute($stmtUploadSPB)) {
        
        echo json_encode(array("UPLOADSPB"=>"SUCCESS"));
        
        $lastInsertID = mysqli_insert_id($conn); // Getting last ID
        
        // Inserting to values app_entity_156_values
        mysqli_query($conn, "INSERT INTO app_entity_156_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 4667, ".$_POST['vehicleid'].");");
        mysqli_query($conn, "INSERT INTO app_entity_156_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 4668, ".$_POST['addedby'].");");
        mysqli_query($conn, "INSERT INTO app_entity_156_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 4669, ".$_POST['driverid'].");");
    	
    	// Check directory first, then upload img
    	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")."/".$filename_db, "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")."/".$filename_db, "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")."/".$filename_db, "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")."/".$filename_db, "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);
}



?>