<?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');

$stmtAbsenType = mysqli_query($conn, "SELECT id FROM app_global_lists_choices 
WHERE (name = '".$_POST['attendancecode']."' OR id = '".$_POST['attendancecode']."') AND lists_id = 24;");
$rowAbsenType = mysqli_fetch_assoc($stmtAbsenType);

function checkPostValuesTime($postid) {
    if (strlen($postid) == 0) {
        $postid = NULL;
    } else {
        $postid = timeToSeconds($postid);
    }
    return $postid;
}

function checkPostValues($postid) {
    if (strlen($postid) == 0) {
        $postid = NULL;
    }
    return $postid;
}

if ($_GET['absenupload'] == 'checkin') { // IF ABSEN MASUK

    $stmtExistIN = mysqli_query($conn, "SELECT COUNT(*) AS existin FROM app_entity_27 WHERE 
    parent_item_id = ".$_POST['empid']." AND FROM_UNIXTIME(field_243, '%Y-%m-%d') = '".$_POST['date']."';");
    $rowExistIN = mysqli_fetch_assoc($stmtExistIN);
    
    if ($rowExistIN['existin'] == '0') {
        $imgPost = NULL;
        $checkindate = strtotime($_POST['date']);
        $checkin = checkPostValuesTime($_POST['checkin']);
        $pointMapCheckIN = "[map]".$_POST['checkinlatlong']."(".$_POST['location'].")[/map]";
        $filename_db = time()."_"."CHECKIN".$_POST['addedby'].$_POST['empid'].date("YmdHis",time()).".png";
        
        if ($checkin == NULL || strlen($checkin) == 0) {
            $imgPost = NULL;
            $filename_db = NULL;
        } else {
            $imgPost = $_POST['apelimg'];
            $filename_db = time()."_"."CHECKIN".$_POST['addedby'].$_POST['empid'].date("YmdHis",time()).".png";
        }
        
        $attendanceCode = 0;
        if ($rowAbsenType['id'] == 224) {
            $attendanceCode = 223;
        } else {
            $attendanceCode = $rowAbsenType['id'];
        }
    
        $queryUploadCheckIN = "INSERT INTO app_entity_27 (parent_item_id, 
        date_added, created_by, field_819, field_243, 
        field_253, field_653, field_658, field_659, 
        field_654, field_656, field_753, field_755, 
        field_1946, field_2104, field_2105) VALUES (?, 
        UNIX_TIMESTAMP(), ?, ?, ?, 
        ?, ?, ?, ?, 
        ?, ?, ?, ?, 
        ?, ?, ?);";
        
        $stmtUploadCheckIN = mysqli_prepare($conn, $queryUploadCheckIN);
        mysqli_stmt_bind_param($stmtUploadCheckIN, 'iissiissssssiii', $_POST['empid'], 
        $_POST['uploadby'], $_POST['addedby'], $checkindate, 
        $_POST['empid'], $attendanceCode, $_POST['location'], $checkin, 
        $_POST['attendancenotes'], $_POST['checkinlatlong'], $pointMapCheckIN, $filename_db, 
        $_POST['inputmethod'], $_POST['attendanceshift'], $_POST['attendancetype']);
            
        if (mysqli_stmt_execute($stmtUploadCheckIN)) {
            echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
            
            $stmtImgAttIN = mysqli_query($conn, "SELECT field_755 FROM app_entity_27 WHERE id = ".mysqli_insert_id($conn).";");
            $rowImgAttIN = mysqli_fetch_assoc($stmtImgAttIN);
    		
    		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")."/".$rowImgAttIN['field_755'], "w+");
    	            fwrite($fp, base64_decode($imgPost));
                } 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")."/".$rowImgAttIN['field_755'], "w+");
    	            fwrite($fp, base64_decode($imgPost));
                }
            } 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")."/".$rowImgAttIN['field_755'], "w+");
    	            fwrite($fp, base64_decode($imgPost));
                } 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")."/".$rowImgAttIN['field_755'], "w+");
    	            fwrite($fp, base64_decode($imgPost));
                }
            }
    		
    		mysqli_close($conn);
        }
    } else {
        if ($rowAbsenType['id'] != 228) {
            $stmtUpdateCheckIN = mysqli_prepare($conn, "UPDATE app_entity_27 SET field_653 = ? 
            WHERE parent_item_id = ? AND FROM_UNIXTIME(field_243, '%Y-%m-%d') = ?");
            
            mysqli_stmt_bind_param($stmtUpdateCheckIN, 'iis', 
            $rowAbsenType['id'], $_POST['empid'], $_POST['date']);
            
            if (mysqli_stmt_execute($stmtUpdateCheckIN)) {
                echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
            }
        } else if ($rowAbsenType['id'] == 228) {
            echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
        }
        
    }
    
}

else if ($_GET['absenupload'] == 'checkout') { // IF ABSEN PULANG
    
    $stmtExistOUT = mysqli_query($conn, "SELECT COUNT(*) AS existout FROM app_entity_60 WHERE 
    parent_item_id = ".$_POST['empid']." AND FROM_UNIXTIME(field_828, '%Y-%m-%d') = '".$_POST['date']."';");
    $rowExistOUT = mysqli_fetch_assoc($stmtExistOUT);
    
    if ($rowExistOUT['existout'] == '0') {
        $imgPost = NULL;
        $checkoutdate = strtotime($_POST['date']);
        $checkout = checkPostValuesTime($_POST['checkout']);
        $pointMapCheckOUT = "[map]".$_POST['checkoutlatlong']."(".$_POST['location'].")[/map]";
        $filename_db = time()."_"."CHECKOUT".$_POST['addedby'].$_POST['empid'].date("YmdHis",time()).".png";
        
        if ($checkout == NULL || strlen($checkout) == 0) {
            $imgPost = NULL;
            $filename_db = NULL;
        } else {
            $imgPost = $_POST['apelimg'];
            $filename_db = time()."_"."CHECKOUT".$_POST['addedby'].$_POST['empid'].date("YmdHis",time()).".png";
        }
    
        $queryUploadCheckOUT = "INSERT INTO app_entity_60 (
        parent_item_id, date_added, created_by, 
        field_848, field_828, field_837, 
        field_838, field_840, field_842, 
        field_841, field_845, field_847, 
        field_2106, field_2107) VALUES 
        (?, UNIX_TIMESTAMP(), ?, 
        ?, ?, ?, 
        ?, ?, ?, 
        ?, ?, ?, 
        ?, ?);";
        
        $stmtUploadCheckOUT = mysqli_prepare($conn, $queryUploadCheckOUT);
        mysqli_stmt_bind_param($stmtUploadCheckOUT, 'iiisiisssssii', 
        $_POST['empid'], $_POST['uploadby'], 
        $_POST['addedby'], $checkoutdate, $_POST['empid'], 
        $rowAbsenType['id'], $_POST['location'], $checkout, 
        $_POST['checkoutlatlong'], $pointMapCheckOUT, $filename_db, 
        $_POST['attendanceshift'], $_POST['attendancetype']);
            
        if (mysqli_stmt_execute($stmtUploadCheckOUT)) {
            echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
        
        	$stmtImgAttOUT = mysqli_query($conn, "SELECT field_847 FROM app_entity_60 WHERE id = ".mysqli_insert_id($conn).";");
            $rowImgAttOUT = mysqli_fetch_assoc($stmtImgAttOUT);
            
            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")."/".$rowImgAttOUT['field_847'], "w+");
    		        fwrite($fp, base64_decode($imgPost));
                } 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")."/".$rowImgAttOUT['field_847'], "w+");
    		        fwrite($fp, base64_decode($imgPost));
                }
            } 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")."/".$rowImgAttOUT['field_847'], "w+");
    		        fwrite($fp, base64_decode($imgPost));
                } 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")."/".$rowImgAttOUT['field_847'], "w+");
                fwrite($fp, base64_decode($imgPost));
                }
            }
        }
    } else {
        if ($rowAbsenType['id'] != 228) {
            $stmtUpdateCheckOUT = mysqli_prepare($conn, "UPDATE app_entity_60 SET field_838 = ? 
            WHERE parent_item_id = ? AND FROM_UNIXTIME(field_828, '%Y-%m-%d') = ?");
            
            mysqli_stmt_bind_param($stmtUpdateCheckOUT, 'iis', 
            $rowAbsenType['id'], $_POST['empid'], $_POST['date']);
            
            if (mysqli_stmt_execute($stmtUpdateCheckOUT)) {
                echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
            }
        } else if ($rowAbsenType['id'] == 228) {
            echo json_encode(array("UPLOADBRIEFING"=>"SUCCESS"));
        }
    }
    
    
}

?>