<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	
	$des_no = base64_decode(base64_decode(base64_decode(base64_decode($_POST['des_no']))));
	$emp_ad_id = base64_decode(base64_decode(base64_decode(base64_decode($_POST['emp_id']))));
	$app_dt = str_replace("/", "-",$_POST['app_dt']);
	$st_tim = date("H:i:s",strtotime($_POST['st_tim']));
	$ed_tim = date("H:i:s",strtotime($_POST['ed_tim']));
	
	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));
	}
	$sts = $_POST['question1'];
	if($sts!='session_out')
	{
		$no_pce = $_POST['no_pce'];
		$volum = $_POST['volum'];
		$g1 = $_POST['gold_wt'];
		$g2 = $_POST['gold_to_vol'];
		$des_hgt = $_POST['des_hgt'];
		$des_wth = $_POST['des_wth'];
	}
	else if($sts=='session_out')
	{
		$no_pce = '';
		$volum 	= '';
		$g1 	= '';
		$g2 	= '';
		$des_hgt= '';
		$des_wth= '';
	}
	/*$no_pce = $_POST['no_pce'];
	$volum = $_POST['volum'];
	$g1 = $_POST['gold_wt'];
	$g2 = $_POST['gold_to_vol'];*/
	
	if($_POST['des_caty']=="Stone")
	{
		$final_wght = $_POST['final_wght'];
	}
	else
	{
		$final_wght = '0.00';
	}
	
	/*$des_hgt = $_POST['des_hgt'];
	$des_wth = $_POST['des_wth'];*/
	
	if($sts=='waiting')
	{
		$wait_dt = str_replace("/", "-",$_POST['wait_dt']);
		$adm_id = $_POST['adm_nm'];
		$adm_nm = 'Admin';
		$cad_sts = 'approved';
	}
	else
	{
		$wait_dt = '';
		$adm_id = '';
		$adm_nm = '';
		$cad_sts = $sts;
	}
	
	//$PATH_photo	= "../cad_fin_images/";
	$PATH_photo2	= "../../cad_fin_images/";
				
	$SLCT = "SELECT * FROM design_det WHERE design_det_id='$des_no'";
	//echo $SLCT;
	$QURY = mysqli_query($connect,$SLCT);
	$FTCH = mysqli_fetch_object($QURY);				
	
	$photo = substr($_FILES["imgInp"]["name"], strrpos($_FILES["imgInp"]["name"], '.') + 1);
	$photo = strtolower($photo);
	$pho_ext="CF.".$des_no.".".$photo;
	
	if(is_uploaded_file($_FILES['imgInp']['tmp_name']))
	{
		$ext = strtolower($_FILES['imgInp']['type']);
		switch(strtolower($_FILES['imgInp']['type']))
		{
			case 'image/jpeg':
				$image = imagecreatefromjpeg($_FILES['imgInp']['tmp_name']);
				break;
			case 'image/png':
				$image = imagecreatefrompng($_FILES['imgInp']['tmp_name']);
				break;
			case 'image/gif':
				$image = imagecreatefromgif($_FILES['imgInp']['tmp_name']);
				break;
			case 'image/bmp':
				$image = imagecreatefromgif($_FILES['imgInp']['tmp_name']);
			default:
				//exit('Unsupported type: '.$_FILES[imgInp]['type'][$j]);
		}
		$old_width  = imagesx($image);
		$old_height = imagesy($image);
		// Target dimensions
		$size_width=array(60,200,600);
		$size_hieght=array(60,200,600);
		$prod_size=array('S','M','L');
		
		for($l=0;$l<3;$l++)
		{
			$max_width = $size_width[$l];
			$max_height = $size_hieght[$l];
		
			// Get current dimensions
			// Calculate the scaling we need to do to fit the image inside our frame
			$scale = min($max_width/$old_width, $max_height/$old_height);
			
			// Get the new dimensions
			$new_width  = ceil($scale*$old_width);
			$new_height = ceil($scale*$old_height);
			// Create new empty image
			$new = imagecreatetruecolor($new_width, $new_height);
			// Resize old image into new
			imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
			imagealphablending($new, false);
			imagesavealpha($new, true);
			imagecopyresampled($new, $image,0, 0, 0, 0,$new_width, $new_height, $old_width, $old_height);
		
			// Catch the imagedata
			ob_start();
			
			switch ($ext)
			{
				case 'image/gif':
					//imagegif($im, $fileName);
					//imagegif($new,"$PATH_photo/$prod_size[$l].CF.$des_no.gif", 90);
					imagegif($new,"$PATH_photo2/$prod_size[$l].CF.$des_no.gif", 90);
					break;
				case 'image/jpg':
				case 'image/jpeg':
					//imagejpeg($im, $fileName, $quality);
					//imagejpeg($new,"$PATH_photo/$prod_size[$l].CF.$des_no.jpg", 90);
					//imagejpeg($new,"$PATH_photo/$prod_size[$l].CF.$des_no.jpeg", 90);
					imagejpeg($new,"$PATH_photo2/$prod_size[$l].CF.$des_no.jpg", 90);
					imagejpeg($new,"$PATH_photo2/$prod_size[$l].CF.$des_no.jpeg", 90);
					break;
				case 'image/png':
					//imagepng($im, $fileName);
					//imagepng($new,"$PATH_photo/$prod_size[$l].CF.$des_no.png", 8);
					imagepng($new,"$PATH_photo2/$prod_size[$l].CF.$des_no.png", 8);
					break;
				case 'image/bmp':
					//imagewbmp($im, $fileName);
					//imagewbmp($new,"$PATH_photo/$prod_size[$l].CF.$des_no.bmp", 90);
					imagewbmp($new,"$PATH_photo2/$prod_size[$l].CF.$des_no.bmp", 90);
					break;
				default:
					break;
			}
		}
	}
	
	$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 `no_of_pice`='$no_pce', curr_status='$cad_sts', waiting_app_dt='$app_dt', `approved_dt`='$wait_dt', `img_ext`='$pho_ext', `cad_vol`='$volum', `gold_wgt`='$g1', `vol_to_gold`='$g2', `design_height`='$des_hgt', `design_width`='$des_wth', total_stone_weight='$final_wght', statu_s='1'  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 `no_of_pice`='$no_pce', waiting_app_dt='$app_dt', approved_dt`='$wait_dt', `admin_id`='$adm_id', `appr_name`='$adm_nm', end_time='$ed_tim', time_taken='$tak_tim', `cad_vol`='$volum', `gold_wgt`='$g1', `vol_to_gold`='$g2', `design_height`='$des_hgt', `design_width`='$des_wth', 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`, `waiting_app_dt`, `approved_dt`, `admin_id`, `appr_name`, `cad_vol`, `gold_wgt`, `vol_to_gold`, `no_of_pice`, `design_height`, `design_width`, `start_time`, `end_time`, `time_taken`, `tot_time`, `post_dt`) VALUES ('$max_id','$app_dt','$app_dt','$wait_dt','$adm_id','$adm_nm','$volum','$g1','$g2','$no_pce','$des_hgt','$des_wth','$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`, `waiting_app_dt`, `approved_dt`, `img_ext`, `cad_vol`, `gold_wgt`, `vol_to_gold`, `no_of_pice`, `design_height`, `design_width`, `total_stone_weight`,`curr_status`, `post_dt`) VALUES ('$emp_ad_id','$des_no','$app_dt','$app_dt','$wait_dt','$pho_ext','$volum','$g1','$g2','$no_pce','$des_hgt','$des_wth','$final_wght','$cad_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`, `waiting_app_dt`, `approved_dt`, `admin_id`, `appr_name`,  `cad_vol`, `gold_wgt`, `vol_to_gold`, `no_of_pice`, `design_height`, `design_width`, `start_time`, `end_time`, `time_taken`, `tot_time`, `post_dt`) VALUES ('$max_id','$app_dt','$app_dt','$wait_dt','$adm_id','$adm_nm','$volum','$g1','$g2','$no_pce','$des_hgt','$des_wth','$st_tim','$ed_tim','$tak_tim','$tot_tim','$cur_time')";
		//echo $INSRT_CURR_DET2."<br>";
		$EXECT_CURR_DET2 = mysqli_query($connect,$INSRT_CURR_DET2);
	}
	
	$SLCT_STN = "SELECT * FROM emp_current_design_stone WHERE design_det_id = '$des_no' AND emp_det_id='$emp_ad_id'";
	$QURY_STN = mysqli_query($connect,$SLCT_STN);
	if(mysqli_num_rows($QURY_STN)>0)
	{
		$DELT_STN = "DELETE FROM emp_current_design_stone WHERE design_det_id = '$des_no' AND emp_det_id='$emp_ad_id'";
		$WXWC_STN = mysqli_query($connect,$DELT_STN);
	}	
	if($_POST['des_caty']=="Stone")
	{
		$cnt1 = $_POST['cur_count'];
		for($x=1;$x<=$cnt1;$x++)
		{
			if(isset($_POST['cat_name'][$x]))
			{		
				$cat_name=base64_decode(base64_decode(base64_decode(base64_decode($_POST['cat_name'][$x]))));						
			}
			if(isset($_POST['stn_size'][$x]))
			{		
				$stn_size=base64_decode(base64_decode(base64_decode(base64_decode($_POST['stn_size'][$x]))));							
			}
			if(isset($_POST['stn_wght'][$x]))
			{		
				$stn_wght=$_POST['stn_wght'][$x];							
			}
			if(isset($_POST['quanty'][$x]))
			{		
				$quanty=$_POST['quanty'][$x];							
			}
			if(isset($_POST['tot_wght'][$x]))
			{		
				$tot_wght=$_POST['tot_wght'][$x];							
			}
			
			$INSRT_CURR_DET2 = "INSERT INTO `emp_current_design_stone`(`emp_current_design_id`, `emp_det_id`, `design_det_id`, `mas_stone_cat_id`, `mas_stone_type_id`, `stone_weight`, `quantity`, `total_weight`, `post_dt`) VALUES ('$max_id', '$emp_ad_id', '$des_no', '$cat_name', '$stn_size', '$stn_wght', '$quanty', '$tot_wght','$cur_time')";
			$EXECT_CURR_DET2 = mysqli_query($connect,$INSRT_CURR_DET2);
		}
	}
	header('Location:../cad_finishing.php');
?>