<?php
include_once('database_connection.php');

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

	if($_GET['action']=="GetVisa")
	{
		$data=array();
			//	$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Employee=isset($_GET['Employee'])?$_GET['Employee']:'';
		$PassportNo=isset($_GET['PassportNo'])?$_GET['PassportNo']:'';
		$Status=isset($_GET['Status'])?$_GET['Status']:'';

		if($Employee!="")
		{
						$Employee="SalesManName like '%".$Employee."%' and ";
		}
		else
		{
			$Employee="";
		}
		if($PassportNo!="")
		{
			$PassportNo="PassportNo like '%".$PassportNo."%' and ";
		}
		else
		{
			$PassportNo="";
		}
		if($Status!="")
		{
			$Status="Status like '%".$Status."%' and ";
		}
		else
		{
			$Status="";
		}
		$searchbox=$Employee."".$PassportNo."".$Status;


		$sql="Select ROW_NUMBER() OVER (
						PARTITION BY ev.Keyfield
						ORDER BY ev.Keyfield
			 )  row_num, EmployeeVisaID, ev.EmployeeID, SalesManName as Employee, PassportNo,StartDate,EndDate, Status, ev.Picture,
				 ev.LastModificationTimeStamp, ev.Description,ev.Keyfield,'' as col_Action
	   				FROM EmployeeVisa as ev
	   				inner join SalesManInformation as smi on ev.EmployeeID=smi.SalesManID
					where ".$searchbox." ev.active=1";   
		   
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			$row['Employee']=htmlspecialchars($row['Employee']);
			$row["col_Action"]='<i class="fa fa-pencil fa-lg" style="text-align: left;color: blue;cursor:pointer;" onclick="javascript:editRow(this);"></i> &nbsp;&nbsp;&nbsp; <i class="fa fa-trash-o fa-lg" style="text-align: right;color: red;cursor:pointer;" onclick="javascript:deleteRow(this);"></i>';
		$data[]=$row;	
		}
		echo json_encode($data);
	}

	if($_GET['action']=="searchGetAllowance^^^")
	{
		// $DesignationName=$_GET['DesignationName'];
		// $Description=isset($_GET['Description'])?$_GET['Description']:'';
		// $DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Employee=isset($_GET['Employee'])?$_GET['Employee']:'';
		$Allowance=isset($_GET['Allowance'])?$_GET['Allowance']:'';
		$AllowanceType=isset($_GET['AllowanceType'])?$_GET['AllowanceType']:'';

		if($Employee!="")
		{
			$Employee="SalesManName like '%".$Employee."%' and ";
		}
		else
		{
			$Employee="";
		}
		if($Allowance!="")
		{
						$Allowance="MasterValue like '%".$Allowance."%' and ";

		}
		else
		{
			$Allowance="";
		}
		$searchbox=$Employee."".$Allowance;
		$data=array();
		echo $sql="SELECT EmployeeAllowanceID as row_num, ea.SalesManID , SalesManName as Employee, 
		ea.MasterValueID , MasterValue as Allowance, AllowanceType,'' as col_Action
			FROM EmployeeAllowance as ea 
			inner join SalesManInformation as smi on ea.SalesManID=smi.SalesManID
			inner join master_values as mv on ea.MasterValueID=mv.MasterValueID where ".$searchbox." active=1";
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			//$row['SalesManName']=htmlspecialchars($row['SalesManName']);
			$row['Employee']=htmlspecialchars($row['Employee']);
			$row["col_Action"]='<i class="fa fa-pencil fa-lg" style="text-align: left;color: blue;cursor:pointer;" onclick="javascript:editRow(this);"></i> &nbsp;&nbsp;&nbsp; <i class="fa fa-trash-o fa-lg" style="text-align: right;color: red;cursor:pointer;" onclick="javascript:deleteRow(this);"></i>';
			$data[]=$row;	
		}

		echo json_encode($data);
	}

	if($_GET['action']=="InsertVisa")
	{
		//print_r($_POST);
		//print_r($_FILES);
		//return;

		//$data=array_keys($_POST);
		//$data=json_decode($data[0]);  // Not required for file upload
		
		$EmployeeVisaID=isset($_POST['EmployeeVisaID'])?$_POST['EmployeeVisaID']:0;
		$EmployeeID=isset($_POST['EmployeeID'])?$_POST['EmployeeID']:0;
		$PassportNo=isset($_POST['PassportNo'])?$_POST['PassportNo']:Date("Y-m-d");
		$StartDate=isset($_POST['StartDate'])?$_POST['StartDate']:Date("Y-m-d");
		$EndDate=isset($_POST['EndDate'])?$_POST['EndDate']:'';
		$Status=isset($_POST['Status'])?$_POST['Status']:'';
		$Description=str_replace("_"," ",isset($_POST['Description'])?$_POST['Description']:'');

		$ppic="";
		if (isset($_FILES['Picture'])){
			$ppic=$_FILES['Picture']['name'];
		}

		if($ppic!="")
		{
			$newfile=rand()*1200;
			$ext=strrchr($ppic,".");
			$newpic=$newfile.$ext;
			save($_FILES['Picture'],'',"images/");	
			rename("images/".$ppic,"images/".$newpic);
			//$pic="PictureLocation='$newpic'";		
		}
		else
		{
			//$pic="";
			$newpic=" ";			
		}

		//$sql="update InvoiceMaster set $pic where InvoiceID='$InvoiceID'";

		//odbc_exec($connection,$sql);
		//---
		
		if($EmployeeVisaID>0 && $EmployeeVisaID!="")
		{
				$updatesql="update EmployeeVisa set EmployeeID='".$EmployeeID."', PassportNo='".$PassportNo."', StartDate='".$StartDate."', 
				EndDate='".$EndDate."', Status='".$Status."',Picture='".$newpic."',Description='".$Description."' where EmployeeVisaID=".$EmployeeVisaID;	
		}
		else  {
			# code...
			if($ppic!="")
				$updatesql="insert into EmployeeVisa (EmployeeID, PassportNo, StartDate, EndDate, Status,Picture, Description, KeyField, Active) 
				values(".$EmployeeID.",'".$PassportNo."','".$StartDate."','".$EndDate."','".$Status."','".$newpic."','".$Description."','',1)";
			else
				$updatesql="insert into EmployeeVisa (EmployeeID, PassportNo, StartDate, EndDate, Status, Description, KeyField, Active) 
				values(".$EmployeeID.",'".$PassportNo."','".$StartDate."','".$EndDate."','".$Status."','".$Description."','',1)";		
		}	
		odbc_exec($connection,$updatesql);
		$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);			
	}

	if($_GET['action']=="DeleteVisa")
	{
		$EmployeeVisaID=$_GET['EmployeeVisaID'];
		$sql="update EmployeeVisa set Active=0 where EmployeeVisaID=".$EmployeeVisaID;
		odbc_exec($connection,$sql);
		$res=array('status'=>"ok",'description'=>'Record Deleted Sucessfully');
		echo json_encode($res);
	}
}

?>