<?php
require_once 'classes/ConDB.php';

require_once 'classes/getErrorMsg.php';

header('Content-type: application/json');

if (isset($_REQUEST['timelog_submit'])) {
    $db = new ConDB();
    
    $request = $db-> _cleanInputs($_REQUEST);
       
    
    extract($request);
   
    if ($device_id == '' || $emp_no == '' || $log_type == '' || $latidegress == '' || $latimins == '' || $latisecs == '' || $longdegress == '' || $longmins == '' || $longsecs == '' || $filename == '') {
        $msg = new getErrorMsg('7');
        $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
        echo json_encode($json);
        return false;
    } 
    $inloop = false;

    if ($inloop) {
       
        //header('Content-type: application/json');
        $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
        echo json_encode($json);
        return false;
    } else {
    	$time_log_type = "IN";
	if($log_type == "0")
		$time_log_type = "TIMEIN";
	else if ($log_type == "1")
		$time_log_type = "TIMEOUT";
	else if ($log_type == "2")
		$time_log_type = "OVERTIME";
			
	$date = date_create('now', timezone_open('Asia/Manila'));
	$current_time = date_format($date, 'Y-m-d H:i:sP') . "\n";
      $reg_log_qry = "insert into time_log_table(device_id,emp_no,log_type,latidegress, latimins, latisecs, longdegress, longmins, longsecs,log_date_time,filename) values('" . $device_id . "', '" . $emp_no . "','" . $time_log_type . "','" . $latidegress . "','" . $latimins . "','" . $latisecs . "','" . $longdegress . "','" . $longmins . "','" . $longsecs . "','" . $current_time . "','" . $filename . "')";
  
  
        $resutl= mysql_query($reg_log_qry, $db->conn);
        $inserted_id = mysql_insert_id();
        if (mysql_affected_rows() > 0) {
            $msg = new getErrorMsg('0');
            $json = array('Emp_ID' => $inserted_id, 'statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
            echo json_encode($json);
            return false;
        } else {
            $msg = new getErrorMsg('9');
            $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
            echo json_encode($json);
            return false;
        }
    }
}
?>