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

// Check existing leave nodoc
$stmtCheckLeave = mysqli_query($conn, "SELECT COUNT(*) AS ceklv 
FROM app_entity_97 WHERE field_2248 = '".$_POST['nodoc']."';");
$rowCheckLeave = mysqli_fetch_assoc($stmtCheckLeave);

if ($rowCheckLeave['ceklv'] == '0') {
    
    $originalImgName= time()."_".$_FILES['filename']['name'];
    $tempName= $_FILES['filename']['tmp_name'];
    $folder = "../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/";
    $url = "../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".$originalImgName;
    
    if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) {
        mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"));
    }
    
    if (move_uploaded_file($tempName, $folder.$originalImgName)) {
        $unixLeaveFrom = strtotime($_POST['datetimefrom']);
        $unixLeaveTo = strtotime($_POST['datetimeto']);
        $unixStartWork = strtotime($_POST['datestartwork']);
        $unixDateAdded = strtotime($_POST['dateadded']);
        
        $queryLeaveReq = "INSERT INTO app_entity_97 
        (parent_item_id, date_added, created_by, field_2230, 
        field_2231, field_2232, field_2233, field_2234, 
        field_2235, field_2236, field_2237, field_2247, 
        field_2248, field_2241, field_2242, field_2243, 
        field_2244, field_2245, field_2246, field_2240) VALUES 
        (?, ?, ?, ?, 
        ?, ?, ?, ?, 
        ?, ?, ?, ?, 
        ?, ?, ?, ?, 
        ?, ?, ?, 117)";
        
        $stmtLeaveReq = mysqli_prepare($conn, $queryLeaveReq);
        mysqli_stmt_bind_param($stmtLeaveReq, 'isiiissssisississss', 
        $_POST['empid'], $unixDateAdded, $_POST['uploadby'], $_POST['attendancecode'], 
        $_POST['leavetype'], $_POST['leavedesc'], $unixLeaveFrom, $unixLeaveTo, 
        $unixStartWork, $_POST['empid'], $originalImgName, $_POST['addedby'], 
        $_POST['nodoc'], $_POST['pendingtask'], $_POST['actingto'], $_POST['famname'], 
        $_POST['famrelation'], $_POST['famcontact'], $_POST['address']);
        
        if (mysqli_stmt_execute($stmtLeaveReq)) {
            echo json_encode(array("UPLOADLEAVE"=>"SUCCESS"));
            
            $lastInsertID = mysqli_insert_id($conn);
            
            // Inserting to app_entity_97_values
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2230, ".$_POST['attendancecode'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2231, ".$_POST['leavetype'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2236, ".$_POST['empid'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2242, ".$_POST['actingto'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2247, ".$_POST['addedby'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2240, 117);");
            
            // Approver Satu
            $stmtApproverSatu = mysqli_query($conn, "SELECT id, field_207 FROM app_entity_1 
            WHERE field_207 = (SELECT field_3381 FROM app_entity_31 WHERE id = ".$_POST['empid'].");");
            $rowApproverSatu = mysqli_fetch_assoc($stmtApproverSatu);
            
            // Approver Dua
            $stmtApproverDua = mysqli_query($conn, "SELECT id, field_207 FROM app_entity_1 
            WHERE field_207 = (SELECT field_3381 FROM app_entity_31 WHERE id = ".$rowApproverSatu['field_207'].");");
            $rowApproverDua = mysqli_fetch_assoc($stmtApproverDua);
            
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2238, ".$rowApproverSatu['id'].");");
            mysqli_query($conn, "INSERT INTO app_entity_97_values (items_id, fields_id, value) 
            VALUES (".$lastInsertID.", 2238, ".$rowApproverDua['id'].");");
            
            mysqli_close($conn);
        }
    }
    
} else if ($rowCheckLeave['ceklv'] != '0') {
    echo json_encode(array("UPLOADLEAVE"=>"SUCCESS"));
}
        
?>