<?php

include_once("../class/class.MySQL.php");
$db	=new MySQL();

$dt = new DateTime();
//$dt->setTimestamp(0);
//$dt->format('H:i:s');
$e_date=date('Y-m-d');

//$time=date('h:i:s');

$currentDateTime=date('m/d/Y H:i:s');
$cur_time = date('h:i:s', strtotime($currentDateTime));
$am_pm = date('A', strtotime($currentDateTime));

if($am_pm=='AM')
{
$time=$cur_time;
$times=date('h:i', strtotime($currentDateTime));
}elseif($am_pm=='PM')
{
//$time=$cur_time+'12:00:00';
$secs = strtotime("12:00:00")-strtotime("00:00:00");
$time = date("H:i:s",strtotime($cur_time)+$secs);
$times=date('h:i', strtotime($currentDateTime));
}

if(isset($_GET['loadtime'])){

$today =date('Y-m-d');
$timeOut	="00:00:00";

	//print_r($timeDetails);
	
	echo '{"timeDetails":'.json_encode($timeDetails).'}';
}elseif(isset($_POST['ClockIn']))
{
$today =date('Y-m-d');

$data['e_date']=$e_date;
$data['e_in']=$time;
$data['time_in']=$times;
$data['time_inloc']=$am_pm;
$data['e_id']=$_POST['e_id'];

//echo "SELECT * FROM `time` where e_date = '$today' and e_id = '".$_POST['e_id']."' and is_late_status != 1 and is_late_status != 0  order by id asc limit 1";

$latestatusDetails	=	$db->fetchQuery("SELECT * FROM `time` where e_date = '$today' and e_id = '".$_POST['e_id']."' order by id asc limit 1");

if(count($latestatusDetails) == 0)
{
	$reportimeDetails	=	$db->fetchQuery("SELECT * FROM `report_time` ");
	echo $reportimeDetails[0]['report_time'];
	if($time < $reportimeDetails[0]['report_time'])
	{
		$data['is_late_status']=0;
	}
	else
	{
		$data['is_late_status']=1;
	}

}


//$timeDetails	=	$db->fetchQuery("SELECT * FROM `time` WHERE e_date='$today' AND e_out='$timeOut'");



$employee	=	$db->Insert($data,'time');


$set1['clock_status']=1;
$where['e_id']=$_POST['e_id'];
$db->Update('employee',$set1,$where);
//print_r($set1);

$where['e_id']=$_POST['e_id'];
$clock_time =$db->fetchAll('time',$where,'', '', '', '',$cols='MAX(id) as c_id');
$arr=array();
   $i=1;
 foreach($clock_time as $clock_time)
   {
  
$c_ids=$clock_time['c_id'];
$wheres['id']=$c_ids;
$qry=$db->fetchAll('time',$wheres,'', '', '', '',$cols='e_in');
 $e_in=$qry[0]['e_in'];
   $arr[$i]['e_in']= $e_in;
    $i++;

}
echo '{"clock_time":'.json_encode($arr).'}';
}elseif(isset($_POST['ClockOut']))
{
$employeeid=$_POST['e_id'];
$today =date('Y-m-d');
$timeOut	="00:00:00";

$workdetails=	$db->fetchQuery("SELECT * FROM `time` WHERE e_date='$today' AND e_out='$timeOut' AND e_id='$employeeid' ");
$e_in=$workdetails[0]['e_in'];

//exit;
$table='time';
$set['e_date']=$e_date;
$set['e_work']=$timediff;
$set['e_out']=$time;
$set['time_out']=$times;
$set['time_outloc']=$am_pm;
$where['e_out']='00:00:00';
$where['e_id']=$_POST['e_id'];
$where['e_date']=$e_date;
	$db->Update($table,$set,$where);
	
	$set1['clock_status']=0;
	$where1['e_id']=$_POST['e_id'];
	
	$db->Update('employee',$set1,$where1);
	//print_r($set1);

$where1['e_id']=$_POST['e_id'];
$clock_time =$db->fetchAll('time',$where1,'', '', '', '',$cols='MAX(id) as c_id');

$arr=array();
   $i=1;

 foreach($clock_time as $clock_time)
   {
$c_ids=$clock_time['c_id'];
$wheres['id']=$c_ids;


$qry=$db->fetchAll('time',$wheres,'', '', '', '',$cols='e_in,e_out');

$e_in=$qry[0]['e_in'];
$e_out=$qry[0]['e_out'];

$time1 = strtotime($e_in);
$time2 = strtotime($e_out);
$diff = $time2 - $time1;

$datedifference = date('H:i:s',strtotime("-6 hours 30 minutes",$diff));
$update_time=	$db->fetchQuery("update `time` set e_work ='$datedifference'  WHERE id='$c_ids' ");

   $arr[$i]['e_out']= $e_out;
    $i++;

}	
echo '{"clock_times":'.json_encode($arr).'}';	
}elseif(isset($_GET['empID'])){
		
		$where['e_id'] = $_GET['empID'];
		$where['e_date'] = date('Y-m-d');
		$timePeriod	=	$db->fetchAll('time',$where);
		echo '{"timePeriod":'.json_encode($timePeriod).'}';
		
		//echo "<pre>";
		//print_r($emp);

}else{

	/*$emp	=	$db->fetchAll('employee','','','',false,'','e_fname,e_id,clock_status','');
	echo '{"employee":'.json_encode($emp).'}';*/
}
//$emp1	=	$db->fetchAll('time','','','',false,'','e_in,e_out','');
/*foreach($emp1 as $val)
{
$emp_in=$val['e_in'];
 $emp_out=$val['e_out'];
// echo $result = floor((($emp_out- $emp_in)/60)/60);
}*/
?>                                                                                                                                                                                                                                  