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

$stmtCheckSPL = mysqli_query($conn, "SELECT COUNT(*) AS cekspl 
FROM app_entity_96 WHERE field_2206 = '".$_POST['nodoc']."';");
$rowCheckSPL = mysqli_fetch_assoc($stmtCheckSPL);

if ($rowCheckSPL['cekspl'] == '0') {
    
    $workDate = strtotime($_POST['workdate']);
    $timeFrom = timeToSeconds($_POST['timefrom']);
    $timeUntil = timeToSeconds($_POST['timeuntil']);
    
    $queryUploadSPL = "INSERT INTO app_entity_96 
    (parent_item_id, date_added, created_by, 
    field_2206, field_2207, field_2208, 
    field_2215, field_2209, field_2210, 
    field_2211, field_2212) VALUES 
    (?, UNIX_TIMESTAMP(), ?, 
    ?, ?, ?, 
    ?, ?, ?, 
    ?, ?);";
    
    $stmtUploadSPL = mysqli_prepare($conn, $queryUploadSPL);
    mysqli_stmt_bind_param($stmtUploadSPL, 'iissiissss', 
    $_POST['addedby'], $_POST['uploadby'], 
    $_POST['nodoc'], $workDate, $_POST['addedby'], 
    $_POST['workshift'], $_POST['workemp'], $_POST['activity'], 
    $timeFrom, $timeUntil);
    
    if (mysqli_stmt_execute($stmtUploadSPL)) {
        echo json_encode(array("UPLOADSPL"=>"SUCCESS"));
        
        mysqli_query($conn, "INSERT INTO app_entity_96_values (items_id, fields_id, value) VALUES (".mysqli_insert_id($conn).", 2208, ".$_POST['addedby'].");");
        mysqli_query($conn, "INSERT INTO app_entity_96_values (items_id, fields_id, value) VALUES (".mysqli_insert_id($conn).", 2215, ".$_POST['workshift'].");");
        mysqli_close($conn);
    }   
} else if ($rowCheckSPL['cekspl'] != '0') {
    echo json_encode(array("UPLOADSPL"=>"SUCCESS"));
}
        
?>