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

if ($_GET['rkhtype'] == 'division') {
	
	$sqlcheck  = " SELECT * FROM app_entity_85 WHERE ";
    $sqlcheck .= " field_1776 = '".$_POST['divisionid']."'";
    $sqlcheck .= " AND field_1789 = '".$_POST['nodoc']."';";
  
    $checkresult = mysqli_query($conn, $sqlcheck);
    $rowcount = mysqli_num_rows($checkresult);
	
	$rowfetch = mysqli_fetch_assoc($checkresult);

	if ($rowcount == 0) {
	    
	    $upkeepDate = strtotime($_POST['upkeepdate']);
	    
		$queryUploadDivUpkeepRKH = "INSERT INTO app_entity_85 
		(parent_item_id, date_added, created_by, 
		field_1788, field_1779, field_1780, field_1781, 
		field_1776, field_1789, field_1777) VALUES (?, 
		UNIX_TIMESTAMP(), ?, ?, ?, ?, ?, ?, ?, ?);";
        
        $stmtUploadDivUpkeepRKH = mysqli_prepare($conn, $queryUploadDivUpkeepRKH);
        mysqli_stmt_bind_param($stmtUploadDivUpkeepRKH, 'iiisssiss', 
        $_POST['divisionid'], $_POST['uploadby'], $_POST['addedby'], 
        $_POST['company'], $_POST['site'], $_POST['estate'], 
        $_POST['divisionid'], $_POST['nodoc'], $upkeepDate);
        
        if (mysqli_stmt_execute($stmtUploadDivUpkeepRKH)) {
            echo json_encode(array_map('utf8_encode', array(
                "DIVISIUPKEEPRKH" => "SUCCESS",
                "IDRKH" =>  mysqli_insert_id($conn),
                "NODOCRKH" => $_POST['nodoc']
            )));
            
            mysqli_query($conn, "INSERT INTO app_entity_85_values (items_id, fields_id, value) VALUES (".mysqli_insert_id($conn).", 1776, ".$_POST['divisionid'].");");
            mysqli_close($conn);
        }
	} else {
	    echo json_encode(array_map('utf8_encode', array(
	        "DIVISIUPKEEPRKH" => "SUCCESS",
            "IDRKH" => $rowfetch['id'],
           	"NODOCRKH" => $_POST['nodoc']
        )));
        mysqli_close($conn);
    }
}

if ($_GET['rkhtype'] == 'block') {
    
    $upkeepDate = strtotime($_POST['upkeepdate']);
    $pointMapUpkeepRKH = "[map]".$_POST['latrkh'].",".$_POST['longrkh']."(".$_POST['nodoc'].")[/map]";
    
    // Getting activity UOM
    $stmtChoiceActivityUOM = mysqli_query($conn, "SELECT id FROM app_global_lists_choices WHERE name = '".$_POST['uomact']."' AND lists_id = 8;");
    $rowChoiceActivityUOM = mysqli_fetch_assoc($stmtChoiceActivityUOM);
    
    // Getting material UOM
    $stmtChoiceMaterialUOM = mysqli_query($conn, "SELECT id FROM app_global_lists_choices WHERE name = '".$_POST['materialuom']."' AND lists_id = 8;");
    $rowChoiceMaterialUOM = mysqli_fetch_assoc($stmtChoiceMaterialUOM);
    
    $queryUploadBlokUpkeepRKH = "INSERT INTO app_entity_87 (parent_item_id, 
    date_added, created_by, field_1886, field_1874, field_1887, field_1875, 
    field_1891, field_1884, field_1883, field_1897, field_1876, field_1899, 
    field_1885, field_1898, field_1892, field_1896, field_1882, field_1888, 
    field_1889, field_1890) VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?, 
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    
    $stmtUploadBlokUpkeepRKH = mysqli_prepare($conn, $queryUploadBlokUpkeepRKH);
    mysqli_stmt_bind_param($stmtUploadBlokUpkeepRKH, 'iiiiissiiisisissssss', 
    $_POST['blockid'], $_POST['uploadby'], $_POST['addedby'], $_POST['divisionid'], 
    $_POST['nodocid'], $upkeepDate, $_POST['empid'], $_POST['totaltrip'], 
    $_POST['totalunit'], $_POST['blockid'],  $_POST['targethektar'], $_POST['activityid'], 
    $_POST['activityresult'], $rowChoiceActivityUOM['id'], $_POST['materialid'], $_POST['materialqty'], 
    $rowChoiceMaterialUOM['id'], $_POST['latrkh'], $_POST['longrkh'], $pointMapUpkeepRKH);
    
    if (mysqli_stmt_execute($stmtUploadBlokUpkeepRKH)) {
        echo json_encode(array("UPLOADUPKEEPRKH"=>"SUCCESS"));
        mysqli_query($conn, "INSERT INTO app_entity_87_values (items_id, fields_id, value) VALUES (".mysqli_insert_id($conn).", 1874, ".$_POST['divisionid'].");");
        mysqli_close($conn);
    }
}

?>