<?php
	//Connects to your Database
	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']))));
	$emp_cu_id	=	base64_decode(base64_decode(base64_decode(base64_decode($_GET['cur_ad_id']))));
	$des_sts	=	$_GET['des_sts'];
	$start_dt	=	date("d-m-Y",strtotime($cur_date));
	
	$app_type	=	$_POST['question1'];
	$remarks	=	$_POST['remarks'];
	$cad_pnts	=	$_POST['cad_pnts'];
	
	$ad_id		 = $_SESSION['admin_login_id'];
	if($ad_id!=1)
	{
		$SLCT_ad_emp = "SELECT * FROM login_details,emp_det WHERE admin_id = '$_SESSION[admin_login_id]' AND emp_det.emp_det_id = login_details.emp_det_id";
		$QURY_ad_emp = mysqli_query($connect,$SLCT_ad_emp);
		$FTCH_ad_emp = mysqli_fetch_object($QURY_ad_emp);
		$emp_ad_name = $FTCH_ad_emp->name;
	}
	else
	{
		$emp_ad_name = "Admin";
	}
	
	if($app_type=='closed')
	{
		$SLCT_CURR_DET1 = "SELECT * FROM emp_current_design WHERE emp_current_design_id='$emp_cu_id'";
		$EXEC_CURR_DET1	= mysqli_query($connect,$SLCT_CURR_DET1);
		$FTCH_CURR_DET1	= mysqli_fetch_object($EXEC_CURR_DET1);
		
		$INSRT_CURR_DET2 = "UPDATE `design_det` SET no_of_pieces='$FTCH_CURR_DET1->no_of_pice', design_status = 'In CAD Finishing',update_dt = '$cur_time', cad_fin_ext='$FTCH_CURR_DET1->img_ext' WHERE design_det_id='$des_no'";
		$EXECT_CURR_DET2 = mysqli_query($connect,$INSRT_CURR_DET2);
		
		$INSRT_CURR_DET = "UPDATE `emp_current_design` SET curr_status='$app_type', closed_dt='$start_dt', remarks='$remarks' WHERE emp_current_design_id='$emp_cu_id'";
		$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
		
		$SLCT_CURR_DET  = "SELECT * FROM emp_current_design_det WHERE emp_current_design_id='$emp_cu_id' ORDER BY emp_current_design_det_id DESC";
		$EXEC_CURR_DET 	= mysqli_query($connect,$SLCT_CURR_DET);
		$FTCH_CURR_DET	= mysqli_fetch_object($EXEC_CURR_DET);
		$cad_tm = $FTCH_CURR_DET->tot_time;
		
		$INSRT_CURR_DET1 = "UPDATE `emp_current_design_det` SET remarks='$remarks' WHERE emp_current_design_det_id='$FTCH_CURR_DET->emp_current_design_det_id'";
		$EXECT_CURR_DET1 = mysqli_query($connect,$INSRT_CURR_DET1);
		
		//THI IS FOR UPDATING THE CAD LOADING STATUS
		$UPDATE_cd = "UPDATE cad_loading SET cad_status = 'yes',update_dt = '$cur_time' WHERE design_det_id = '$des_no'";
		$QRY_cd    = mysqli_query($connect,$UPDATE_cd);
		
		$INSRT		= "INSERT INTO cad_finishing(`emp_det_id`, `finishing_date`, `post_dt`) VALUES('$emp_ad_id','$start_dt','$cur_time')";		
		$QURY		= mysqli_query($connect,$INSRT);
		
		$SLCT_sk = "SELECT  MAX(cad_finishing_id) as max_ids FROM cad_finishing ORDER BY cad_finishing_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;
		
		$INSRT_d	= "INSERT INTO cad_finishing_des(`cad_finishing_id`, `design_det_id`,`cad_time`, `post_dt`) VALUES('$max_id','$des_no','$cad_tm','$cur_time')";		
		//echo $INSRT_d."<br/>";
		$QURY_d		= mysqli_query($connect,$INSRT_d); 
	}
	else if($app_type=='approved')
	{
		$INSRT_CURR_DET = "UPDATE `emp_current_design` SET curr_status='$app_type', approved_dt='$start_dt', remarks='$remarks' WHERE emp_current_design_id='$emp_cu_id'";
		$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
		
		$SLCT_CURR_DET  = "SELECT * FROM emp_current_design_det WHERE emp_current_design_id='$emp_cu_id' ORDER BY emp_current_design_det_id DESC";
		$EXEC_CURR_DET 	= mysqli_query($connect,$SLCT_CURR_DET);
		$FTCH_CURR_DET	= mysqli_fetch_object($EXEC_CURR_DET);
		
		$INSRT_CURR_DET1 = "UPDATE `emp_current_design_det` SET approved_dt='$start_dt', admin_id='$ad_id', appr_name='$emp_ad_name', remarks='$remarks' WHERE emp_current_design_det_id='$FTCH_CURR_DET->emp_current_design_det_id'";
		$EXECT_CURR_DET1 = mysqli_query($connect,$INSRT_CURR_DET1);
	}
	else if($app_type=='rework')
	{
		$rerk = $_POST['rework1'];
		$rerk_rsn = $_POST['rework_reason'];
		if($rerk=='other')
		{
			$hdes_name = $_POST['hdes_name'];
			$rework_pnts = $_POST['rework_pnts'];
			$SLCT_CURR_DET1 = "SELECT * FROM emp_current_design WHERE emp_current_design_id='$emp_cu_id'";
			$EXEC_CURR_DET1	= mysqli_query($connect,$SLCT_CURR_DET1);
			$FTCH_CURR_DET1	= mysqli_fetch_object($EXEC_CURR_DET1);
			
			$INSRT_REWRK = "INSERT INTO `emp_current_design_rework`(`rework_for`,`emp_current_design_id`, `admin_id`, `admin_name`, `design_det_id`, `emp_det_id`, `re_emp_det_id`, `rework_pnts`, `total_pnts`, `rework_dt`, `master_cad_remarks_id`, `remark_s`, `post_dt`) VALUES('$rerk','$emp_cu_id','$ad_id','$emp_ad_name','$des_no','$hdes_name','$emp_ad_id','$rework_pnts','$cad_pnts','$start_dt','$rerk_rsn','$remarks','$cur_date')";
			//echo $INSRT_REWRK;
			$EXEC_REWRK  = mysqli_query($connect,$INSRT_REWRK);
			
			$INSRT_CURR_DET = "UPDATE `emp_current_design` SET curr_status='stopped', approved_dt='$start_dt', closed_dt='$start_dt', remarks='$remarks' WHERE emp_current_design_id='$emp_cu_id'";
			//echo $INSRT_CURR_DET;
			$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
			
			$SLCT_CURR_DET  = "SELECT * FROM emp_current_design_det WHERE emp_current_design_id='$emp_cu_id' ORDER BY emp_current_design_det_id DESC";
			$EXEC_CURR_DET 	= mysqli_query($connect,$SLCT_CURR_DET);
			$FTCH_CURR_DET	= mysqli_fetch_object($EXEC_CURR_DET);
			
			$INSRT_CURR_DET1 = "UPDATE `emp_current_design_det` SET approved_dt='$start_dt', admin_id='$ad_id', appr_name='$emp_ad_name', remarks='$remarks' WHERE emp_current_design_det_id='$FTCH_CURR_DET->emp_current_design_det_id'";
			//echo $INSRT_CURR_DET1;
			$EXECT_CURR_DET1 = mysqli_query($connect,$INSRT_CURR_DET1);
			
			$INSRT_CURR = "INSERT INTO `emp_current_design`(`emp_det_id`, `design_det_id`, `start_dt`, `curr_status`, `post_dt`) VALUES('$hdes_name','$des_no','$start_dt','process','$cur_date')";
			$EXECT_CURR = mysqli_query($connect,$INSRT_CURR);
			
			$SLCT_CURR  = "SELECT emp_current_design_id FROM emp_current_design WHERE emp_det_id='$hdes_name' AND design_det_id='$des_no' ORDER BY emp_current_design_id DESC";
			$EXEC_CURR 	= mysqli_query($connect,$SLCT_CURR);
			$FTCH_CURR	= mysqli_fetch_object($EXEC_CURR);
			
			$max_id = $FTCH_CURR->emp_current_design_id;
			
			$INSRT_CURR_DET = "INSERT INTO `emp_current_design_det`(`emp_current_design_id`, `work_dt`, `start_time`, `post_dt`) VALUES('$max_id','$start_dt','$cur_time','$cur_time')";
			$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
			
			$INSRT		= "UPDATE cad_loading SET `emp_det_id`='$hdes_name', cad_load='emp', cad_status='no', `update_dt`='$cur_time' WHERE `design_det_id`='$des_no'";	
			//echo $INSRT;
			$QURY		= mysqli_query($connect,$INSRT);
		}
		else
		{
			$INSRT_REWRK = "INSERT INTO `emp_current_design_rework`(`rework_for`,`emp_current_design_id`, `admin_id`, `admin_name`, `design_det_id`, `emp_det_id`, `re_emp_det_id`, `rework_pnts`, `total_pnts`, `rework_dt`, `master_cad_remarks_id`, `remark_s`, `post_dt`) VALUES('$rerk','$emp_cu_id','$ad_id','$emp_ad_name','$des_no','$hdes_name','$emp_ad_id','$rework_pnts','$cad_pnts','$start_dt','$rerk_rsn','$remarks','$cur_date')";
			//echo $INSRT_REWRK;
			$EXEC_REWRK  = mysqli_query($connect,$INSRT_REWRK);
			
			$INSRT_CURR_DET = "UPDATE `emp_current_design` SET curr_status='$app_type', approved_dt='$start_dt', remarks='$remarks' WHERE emp_current_design_id='$emp_cu_id'";
			//echo $INSRT_CURR_DET;
			$EXECT_CURR_DET = mysqli_query($connect,$INSRT_CURR_DET);
			
			$SLCT_CURR_DET  = "SELECT * FROM emp_current_design_det WHERE emp_current_design_id='$emp_cu_id' ORDER BY emp_current_design_det_id DESC";
			$EXEC_CURR_DET 	= mysqli_query($connect,$SLCT_CURR_DET);
			$FTCH_CURR_DET	= mysqli_fetch_object($EXEC_CURR_DET);
			
			$INSRT_CURR_DET1 = "UPDATE `emp_current_design_det` SET approved_dt='$start_dt', admin_id='$ad_id', appr_name='$emp_ad_name', remarks='$remarks' WHERE emp_current_design_det_id='$FTCH_CURR_DET->emp_current_design_det_id'";
			//echo $INSRT_CURR_DET1;
			$EXECT_CURR_DET1 = mysqli_query($connect,$INSRT_CURR_DET1);
		}
	}
			
	header('Location:../waiting_for_appr_list.php?des_sts='.$des_sts);
?>
	
