<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";
	
	$des_no = base64_decode(base64_decode(base64_decode(base64_decode($_GET['des_no']))));
	$emp_ad_id = base64_decode(base64_decode(base64_decode(base64_decode($_GET['emp_id']))));
	$app_dt = str_replace("/", "-",$_GET['app_dt']);
	$adm_nm = $_GET['adm_nm'];
	$wait_dt = str_replace("/", "-",$_GET['wait_dt']);
	$st_tim = date("H:i:s",strtotime($_GET['st_tim']));
	$ed_tim = date("H:i:s",strtotime($_GET['ed_tim']));
	$sts = $_GET['sts'];
	
	function getTimeDiff($dtime,$atime)
	{	 
		 $dep=explode(':',$dtime);
		 $arr=explode(':',$atime);
		 $diff=abs(mktime($dep[0],$dep[1],$dep[2])-mktime($arr[0],$arr[1],$arr[2]));
		 $hours=floor($diff/(60*60));
		 $mins=floor(($diff-($hours*60*60))/(60));
		 $secs=floor(($diff-(($hours*60*60)+($mins*60))));
		 if(strlen($hours)<2){$hours="0".$hours;}
		 if(strlen($mins)<2){$mins="0".$mins;}
		 if(strlen($secs)<2){$secs="0".$secs;}
		 return $hours.':'.$mins.':'.$secs;
	}
	
	$tak_tim = getTimeDiff($ed_tim,$st_tim);
	
	$SLCT_CAD = "SELECT * FROM emp_current_design,design_det WHERE design_det.design_det_id = emp_current_design.design_det_id AND emp_current_design.design_det_id = '$des_no' AND emp_current_design.emp_det_id='$emp_ad_id'";
	$QURY_CAD = mysqli_query($connect,$SLCT_CAD);
	$FTCH_CAD = mysqli_fetch_object($QURY_CAD);	
	
	$SLCT_CAD_DET = "SELECT * FROM emp_current_design WHERE design_det_id = '$des_no' AND emp_current_design.emp_det_id='$emp_ad_id'";
	$QURY_CAD_DET = mysqli_query($connect,$SLCT_CAD_DET);
	if(mysqli_num_rows($QURY_CAD_DET)>0)
	{
		$FTCH_CAD_DET = mysqli_fetch_object($QURY_CAD_DET);
	
		$SLCT_CURR_TIM = "SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `time_taken` ) ) ) AS tot_time FROM emp_current_design_det WHERE emp_current_design_id = '$FTCH_CAD_DET->emp_current_design_id'";
		//echo $SLCT_CURR_TIM;
		$QURY_CURR_TIM = mysqli_query($connect,$SLCT_CURR_TIM);
		$FTCH_CURR_TIM = mysqli_fetch_object($QURY_CURR_TIM);
	
		$time2 = $FTCH_CURR_TIM->tot_time;	
		$secs = strtotime($tak_tim)-strtotime("00:00:00");
		$tot_tim = date("H:i:s",strtotime($time2)+$secs);
	}
	else
	{
		$tot_tim = date("H:i:s",strtotime($tak_tim));
	}

	$SLCT_CAD_DET = "SELECT * FROM emp_current_design WHERE design_det_id = '$des_no' AND emp_current_design.emp_det_id='$emp_ad_id'";
	$QURY_CAD_DET = mysqli_query($connect,$SLCT_CAD_DET);
	if(mysqli_num_rows($QURY_CAD_DET)>0)
	{
		$INSRT_CURR_DET = "UPDATE `emp_current_design` SET curr_status='$sts', WHERE emp_det_id='$emp_ad_id' AND design_det_id='$des_no'";
		//echo $INSRT_CURR_DET."<br>";
		$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
		
		$SLCT_CURR  = "SELECT emp_current_design_id FROM emp_current_design WHERE emp_det_id='$emp_ad_id' AND design_det_id='$des_no' ORDER BY emp_current_design_id DESC";
		//echo $SLCT_CURR."<br />";
		$EXEC_CURR 	= mysqli_query($connect,$SLCT_CURR);
		$FTCH_CURR	= mysqli_fetch_object($EXEC_CURR);			
		$max_id = $FTCH_CURR->emp_current_design_id;
		
		$SLCT_CURR_DET  = "SELECT * FROM emp_current_design_det WHERE emp_current_design_id='$max_id' ORDER BY emp_current_design_id DESC";
		//echo $SLCT_CURR_DET;
		$EXEC_CURR_DET 	= mysqli_query($connect,$SLCT_CURR_DET);
		$FTCH_CURR_DET	= mysqli_fetch_object($EXEC_CURR_DET);
		
		if($FTCH_CURR_DET->end_time=="00:00:00" && $FTCH_CURR_DET->time_taken=='00:00:00' && $FTCH_CURR_DET->tot_time=='00:00:00')
		{
			$INSRT_CURR_DET = "UPDATE `emp_current_design_det` SET end_time='$ed_tim', time_taken='$tak_tim', tot_time='$tot_tim' WHERE `emp_current_design_id`='$max_id' AND emp_current_design_det_id='$FTCH_CURR_DET->emp_current_design_det_id'";
			//echo $INSRT_CURR_DET."<br>";
			$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
		}
		else
		{
			$INSRT_CURR_DET2 = "INSERT INTO `emp_current_design_det`(`emp_current_design_id`, `work_dt`, `start_time`, `end_time`, `time_taken`, `tot_time`, `post_dt`) VALUES
			 ('$max_id','$app_dt','$st_tim','$ed_tim','$tak_tim','$tot_tim','$cur_time')";
			//echo $INSRT_CURR_DET2."<br>";
			$EXECT_CURR_DET2 = mysqli_query($connect,$INSRT_CURR_DET2);
		}
	}
	else
	{
		$INSRT_CURR_DET = "INSERT INTO `emp_current_design`(`emp_det_id`, `design_det_id`, `start_dt`, `curr_status`, `post_dt`) VALUES
		 ('$emp_ad_id','$des_no','$app_dt','$sts','$cur_time')";
		//echo $INSRT_CURR_DET."<br>";
		$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
		
		$SLCT_CURR  = "SELECT emp_current_design_id FROM emp_current_design WHERE emp_det_id='$emp_ad_id' AND design_det_id='$des_no' ORDER BY emp_current_design_id DESC";
		//echo $SLCT_CURR."<br />";
		$EXEC_CURR 	= mysqli_query($connect,$SLCT_CURR);
		$FTCH_CURR	= mysqli_fetch_object($EXEC_CURR);			
		$max_id = $FTCH_CURR->emp_current_design_id;
		
		$INSRT_CURR_DET2 = "INSERT INTO `emp_current_design_det`(`emp_current_design_id`, `work_dt`, `start_time`, `end_time`, `time_taken`, `tot_time`, `post_dt`) VALUES
		 ('$max_id','$app_dt','$st_tim','$ed_tim','$tak_tim','$tot_tim','$cur_time')";
		//echo $INSRT_CURR_DET2."<br>";
		$EXECT_CURR_DET2 = mysqli_query($connect,$INSRT_CURR_DET2);
	}
?>
