<?php  
error_reporting(0);
require '../class/resizeImage.php';
 
session_start();
include_once("../class/class.MySQL.php");
$db	  	  = new MySQL(); 
$data 	  = array();
$action   = $_REQUEST['action'];
if(isset($_REQUEST['hid']))
{
	$id = $_REQUEST['hid'];
	if($action=='basic'){
	
		$data['e_fname']  	= $_POST['fname'];
		$data['e_mname'] 	= $_POST['mname'];
		$data['e_lname'] 	= $_POST['lname'];
		$data['e_add']	 	= $_POST['address'];
		$data['e_country']	= $_POST['country'];
		$data['e_state'] 	= $_POST['state'];
		$data['e_zip']		= $_POST['zip'];
		$data['e_dob']		= date("Y-m-d",strtotime($_POST['dob']));
		$data['e_phone']	= $_POST['phone'];
		$data['e_mob']		= $_POST['mobile'];
		$data['e_gender']	= $_POST['gender'];
		$data['e_mail']		= $_POST['email'];
		$where['e_id']		= $id;
		$employee			= $db->Update('employee',$data,$where);
		
		#upload resume
		$data2		 = array();
		$allowedExts = array("doc", "docx", "pdf","jpg","jpeg","png","txt");
		$temp 		 = explode(".", $_FILES["resume"]["name"]);
		$extension	 = end($temp);
		if (in_array($extension, $allowedExts))
		{
		 	$randnum		 = rand(120000,99999999);
		    $resumename 	 = $randnum.$_FILES["resume"]["name"];
			$data2['resume'] = $resumename;
		 	if ($_FILES["resume"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["resume"]["error"] . "<br>";
			}
		 	else
			{
				"Upload: " . $_FILES["resume"]["name"]. "<br>";
				"Type: " . $_FILES["resume"]["type"] . "<br>";
				"Size: " . ($_FILES["resume"]["size"]/ 1024) . " kB<br>";
				"Temp file: " . $_FILES["resume"]["tmp_name"] . "<br>";
				if(file_exists("../upload/resume/". $resumename))
			    {
			  		echo "<script>alert('$filename already exists.')</script> ";
			  	}
				else
			    {
			  		echo $_FILES["resume"]["name"];
			  		copy($_FILES["resume"]["tmp_name"],"../upload/resume/". $resumename);
			   		echo "<script>alert('Your Resume has Been Submitted!!')</script> ";
			  	}
			}
		}
		print_r($data2);
		#upload profile photo
		if(isset($_FILES['photo']['name']) && $_FILES['photo']['name']!=''){
			$randnum		= rand(1200156,9994564569);
			$image			= $randnum.$_FILES['photo']['name'];
			$data2['photo']	= $image;
			$paththumb		= "../upload/thumb/".$randnum.$_FILES['photo']['name'];
			$pathimg		= "../upload/image/".$randnum.$_FILES['photo']['name'];
			
			try {
		
				$image = new ImageResize($_FILES['photo']['tmp_name']);
				
				//// Save thumb
				$image->resize(230,110);
				$image->save($paththumb);
				
				/// Save Image
				$image1 = new ImageResize($_FILES['photo']['tmp_name']);
				$image1->resizeToWidth(500);
				$image1->save($pathimg);
	
			} catch (Exception $e) {
	
			}
		}
		$where['e_id']			= $id;
		$db->Update('file',$data2,$where);
		
		#idproof type
		$g=0;
		foreach ( $_FILES['passfile']['name']  as $values)
		{
			$data33					= array();
			$data33['filetype']		= $values;
			$data33['e_id']			= $id;
			$data33['proof_type']	= $_POST['passp'];
			if($values !=''){
				if(isset($_FILES['passfile']['name']) && $_FILES['passfile']['name']!=''){
					$randnum		= rand(1201210,9121212999);
					$image			= $randnum.$_FILES['passfile']['name'][$g];
					$data33['file']	= $image;
					$pathimg		= "../upload/idproof/".$randnum.$_FILES['passfile']['name'][$g];
					try {
						$image  = new ImageResize($_FILES['passfile']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['passfile']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
		
					} catch (Exception $e) {
					}
				}
				$where['e_id']			= $id;
				$db->Insert($data33,'idprooftype');	
				////upload sertificatees and save to the  idproof
			}	
			$g++;
		}
					
		//drive
		$g=0;		
		foreach ( $_FILES['drfile']['name']  as $values)
		{
		
			$data44				 = array();
			$data44['filetype']  = $values;
			$data44['e_id']		 = $id;
			$data44['proof_type']= $_POST['drive'];
			if($values !=''){
				if(isset($_FILES['drfile']['name']) && $_FILES['drfile']['name']!=''){
					$randnum		= rand(121100,99111199);
					$image			= $randnum.$_FILES['drfile']['name'][$g];
					$data44['file']	= $image;
					$pathimg= "../upload/idproof/".$randnum.$_FILES['drfile']['name'][$g];
					try {
			
						$image  = new ImageResize($_FILES['drfile']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['drfile']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
		
					} catch (Exception $e) {
		
					}
				}
				$where['e_id']			= $id;
				$db->Insert($data44,'idprooftype');	
				////upload sertificatees and save to the  idproof
			}
			$g++;
		}
		
		//govt
		$g=0;	
		foreach ( $_FILES['gvtfile']['name']  as $values)
		{
		
			$data55=array();
			$data55['filetype']=$values;
			$data55['proof_type']= $_POST['govtrs'];
			if($values !=''){
				if(isset($_FILES['gvtfile']['name']) && $_FILES['gvtfile']['name']!=''){
					$randnum		= rand(12000,990099);
					$image			= $randnum.$_FILES['gvtfile']['name'][$g];
					$data55['file']	= $image;
					$pathimg= "../upload/idproof/".$randnum.$_FILES['gvtfile']['name'][$g];
					try {
			
						$image  = new ImageResize($_FILES['gvtfile']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['gvtfile']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
					} catch (Exception $e) {
		
					}
				}
				$where['e_id']			= $id;
				$data55['e_id']=$id;
				$db->Insert($data55,'idprooftype');			
				////upload sertificatees and save to the  idproof
			}
			$g++;
		}	
					
		//other
		$g=0;		
		foreach ( $_FILES['othfile']['name']  as $values)
		{
		
			$data66=array();
			$data66['filetype']=$values;
			$data66['proof_type']= $_POST['othtxt'];
			if($values !=''){
				if(isset($_FILES['othfile']['name']) && $_FILES['othfile']['name']!=''){
					$randnum		= rand(12000,9999999);
					$image			= $randnum.$_FILES['othfile']['name'][$g];
					$data66['file']	= $image;
					$pathimg= "../upload/idproof/".$randnum.$_FILES['othfile']['name'][$g];
					
					try {
						$image  = new ImageResize($_FILES['othfile']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['othfile']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
					} catch (Exception $e) {
			
					}
				}
				$where['e_id']			= $id;
				$data66['e_id']=$id;
				$db->Insert($data66,'idprooftype');			
				////upload sertificatees and save to the  idproof
			}
			$g++;
		}
	}//basic end
	if ($action=='ref') {
	
		$data['e_rname']		= $_POST['rname'];
		$data['e_relation']		= $_POST['relation'];
		$data['e_contact']		= $_POST['contact'];
		$data['e_rlocation']	= $_POST['loc'];
		$data['e_dstart']		= date("Y-m-d",strtotime($_POST['dstart']));
		$where['e_id']			= $id;
		$employee				= $db->Update('employee',$data,$where);
	}//reference end
	
	if ($action=='exp') {
	
		#updating employee education
		$where['e_id']	 = $id;
		$edu	         =	$db->Delete('employee_education',$where);
		$n=0;
		foreach ( $_POST['course'] as $value)
		{
				if($value!=""){
					$data6=array();
					$data6['e_id'] 	   = $id;
					$data6['course']   = $_POST['course'][$n];
					$data6['spl']	   = $_POST['spl'][$n];
					$data6['unv']      = $_POST['unv'][$n];
					$data6['yr']       = $_POST['yr'][$n];
					$data6['pcntg']    = $_POST['pcntg'][$n];
					
					$db->Insert($data6,'employee_education');
				}
				$n++;
		}
		
		#updating work experience
	    $k=0;
	    $where['e_id']		 = $id;
	    $db->Delete('employee_expirence',$where);
	  
		foreach ( $_POST['refer'] as $value)
		{
			if($value!=""){
				$data4=array();
				$data4['refer']  	 = $value;
				$data4['emp'] 		 = $_POST['emp'][$k];
				$data4['phno'] 		 = $_POST['phno'][$k];
				$data4['email']		 = $_POST['email1'][$k];
				$data4['from']	 	 = $_POST['from'][$k];
				$data4['to'] 		 = $_POST['to'][$k];
				$data4['posi']		 = $_POST['posi'][$k];
				$data4['sal']		 = $_POST['sal'][$k];
				$data4['rfl']		 = $_POST['rfl'][$k];
				$data4['e_id']		 = $id;
				 
				$where['e_id']		 = $id;
				$db->Insert($data4,'employee_expirence');
			}	
			$k++;
		}
		
		#updating employee languages
		$where['e_id']	 = $id;
		$m=1; 
		foreach ( $_POST['lang'] as $value)
		{
			
			if($value!="")
			{
			
				if($_POST['emp_lang_id'.$m]){
					$data5=array();
					$data5['language_name'] = $value;
					$data5['lang_rd']	 	= $_POST['radio_eng_rd'.$m];
					$data5['lang_wt']    	= $_POST['radio_eng_wt'.$m];
					$data5['lang_sk']    	= $_POST['radio_eng_sk'.$m];
					$con['id']		 	 	= $_POST['emp_lang_id'.$m];
					$db->Update('emp_language',$data5,$con);
					
					
				}
				else {
					$data5=array();
					$data5['e_id'] 		 = $id;
					$data5['language_name']    = $value;
					$data5['lang_rd']	 = $_POST['radio_eng_rd'.$m];
					$data5['lang_wt']    = $_POST['radio_eng_wt'.$m];
					$data5['lang_sk']    = $_POST['radio_eng_sk'.$m];
					
					$db->Insert($data5,'emp_language');
				}
			
			}
			$m++;
		}		
	}//education & work experience
	
	if ($action=='details') {
		$data['employee_id']		= $_POST['emp_id'];
		$data['emp_status']	  		= $_POST['empstat'];
		$data['date_join']			= date("d-m-Y",strtotime($_POST['dt_join']));
		$data['blood_group']		= $_POST['blood_grp']; 
		$data['esi_details']		= $_POST['esi_details'];
		$data['emp_contract']	  	= $_POST['emp_contract'];
		$data['contract_end_date']  = $_POST['contractPeriod'];
		$data['pay_sal_type']  	    = $_POST['pay_salary_type'];
		$data['emp_prob_period']  	= $_POST['emp_prob_period'];
		$data['branchid']			= $_POST['sel_branch'];
		$data['departid']			= $_POST['sel_depart'];
		$data['e_post']				= $_POST['sel_desigs'];
		$data['imd_supervisor_id']	= $_POST['imdatesupervisor'];
		$data['pf_accnum']	        = $_POST['pf_acc_no'];
		$data['pf_date_join_fund']	= $_POST['date_join_fund'];
		$data['pf_service_year']	= $_POST['pf_year'];
		$data['pf_service_month']	= $_POST['pf_month'];
		$data['pf_service_day']	    = $_POST['pf_days'];
		$data['vpf_percent']	    = $_POST['vpf_percentage'];
		$data['insurance_alloted_corp']	    = $_POST['insur_alloted_corp'];
		$data['resigned_date']  = $_POST['resign_dt'];
		
		$where['e_id']				= $id;
		$employee					= $db->Update('employee',$data,$where);
		
		
		//echo "SELECT * FROM pf_esi_details where emp_id = '".$id."' and type_status = '1'";
		$pf_previous_comp_amt = $db->fetchQuery("SELECT * FROM pf_esi_details where emp_id = '".$id."' and type_status = '1'");
		if(count($pf_previous_comp_amt)==0)
		{
			$dataPF['emp_id'] = $id;
			$dataPF['emp_pf'] = $_POST['current_pf_acc_balance'];
			$dataPF['date'] = date('Y-m-d');
			$dataPF['type_status'] = 1;
			$db->Insert($dataPF,'pf_esi_details');
		}
		else
		{
			$dataPF['emp_id'] = $id;
			$dataPF['emp_pf'] = $_POST['current_pf_acc_balance'];
			$dataPF['date'] = date('Y-m-d');
			$dataPF['type_status'] = 1;
			$wherePF['emp_id'] = $id;
			$wherePF['type_status'] = '1';
			//$db->ExecuteSQL('Update pf_esi_details set '
			$employee = $db->Update('pf_esi_details',$dataPF,$wherePF);
		}
		//$db->Insert($data5,'pf_esi_details');
		
	}//details end
	if ($action=='bank') {
		$data['e_account']	= $_POST['accountname'];
		$data['e_bank']		= $_POST['bank'];
		$data['e_accountno']= $_POST['accountno'];
		$data['e_bankadd']	= $_POST['bankadd'];
		$data['e_code']		= $_POST['bankcode'];
		$where['e_id']		= $id;
		
		
	//echo "test";
	//	print_r($data['e_account']); exit();
		$employee			= $db->Update('employee',$data,$where);
	}//bank details end
	
	if ($action=='passport') {
		
		#passpor details
		$data['pass_no']	  	  = $_POST['passnum'];	
		$data['pass_country']	  = $_POST['isucountry'];
		$data['pass_issue_dat']	  = date("Y-m-d",strtotime($_POST['isudate']));	
		$data['pass_exp_dat']	  = date("Y-m-d",strtotime($_POST['pexpdate']));
		$data['nation']	  		  = $_POST['natn'];	
		
		#visa details
		$data['vsa_type']	  	  = $_POST['vstype'];
		$data['vsa_isue_place']	  = $_POST['plcisue'];	
		$data['uid_no']	  		  = $_POST['uidno'];
		$data['file_num']	  	  = $_POST['filen'];
		$data['vsa_pass_num']	  = $_POST['vpasno'];
		$data['profesion']	  	  = $_POST['profsn'];
		$data['sponsor']	  	  = $_POST['spons'];
		$data['vsa_isue_dat']	  = date("Y-m-d",strtotime($_POST['visuedate']));
		$data['vsa_exp_dat']	  = date("Y-m-d",strtotime($_POST['viexpdat']));	
		#emirates details
	
		$data['emirates_id_number']	  = $_POST['emirates_id_number'];	
		$data['emirates_card_number'] = $_POST['emirates_card_number'];	
		$data['emirates_expiry_date'] =	$_POST['emirates_expiry_date'];	
	
		#labour card details
		
		$data['labour_card_number']  			= $_POST['labour_card_number'];	
		$data['labour_card_issuing_authority']  = $_POST['labour_card_authority'];	
		$data['labour_card_zone']  				= $_POST['labour_card_zone'];	
		$data['labour_card_expiry_date']  		= $_POST['labour_expiry_date'];	
		
		$where['e_id']			  = $id;
		$employee				  = $db->Update('employee',$data,$where);
		
		//passports documents
		$g=0;
		foreach ( $_FILES['passport_doc']['name']  as $values)
		{
			$data77				= array();
			$data77['pass_type']= $values;
			$data77['e_id']		= $id;
			if($values !=''){
				if(isset($_FILES['passport_doc']['name']) && $_FILES['passport_doc']['name']!=''){
					$randnum		= rand(120000,999999);
					$image			= $randnum.$_FILES['passport_doc']['name'][$g];
					$data77['passport_doc']		=$image;
					$pathimg= "../upload/passport/".$randnum.$_FILES['passport_doc']['name'][$g];
					
					try {
				
						$image = new ImageResize($_FILES['passport_doc']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['passport_doc']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
			
					} catch (Exception $e) {
			
					}
				}
				$where['e_id']			= $id;
				$db->Insert($data77,'passport_documents');	
				////upload sertificatees and save to the  idproof
			}
			$g++;
		}
		//passports documents
		
		//visa documents
		$g=0;
		foreach ( $_FILES['visa_doc']['name']  as $values)
		{
				
			$data88=array();
			$data88['visa_typ']=$values;
			$data88['e_id']=$id;
			if($values !=''){
				if(isset($_FILES['visa_doc']['name']) && $_FILES['visa_doc']['name']!=''){
					$randnum		=rand(12000,999999);
					$image			=$randnum.$_FILES['visa_doc']['name'][$g];
					$data88['visa_docs']		=$image;
					$pathimg= "../upload/visa/".$randnum.$_FILES['visa_doc']['name'][$g];
						
					try {
						$image = new ImageResize($_FILES['visa_doc']['tmp_name'][$g]);
						/// Save Image
						$image1 = new ImageResize($_FILES['visa_doc']['tmp_name'][$g]);
						$image1->resizeToWidth(500);
						$image1->save($pathimg);
					 } catch (Exception $e) {
					 }
				}
				$where['e_id']			= $id;
				$db->Insert($data88,'visa_doc');	
				////upload sertificatees and save to the  idproof
			}
			$g++;
		}
		//visa documents
	

	}//passport end

	
	
}			
			

