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

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

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

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

		$sql=" Select ROW_NUMBER() OVER (
			PARTITION BY rm.Keyfield
			ORDER BY rm.Keyfield
			)  row_num, RequestMasterID, rm.EmployeeID, SalesManName as Employee, RequestTypeID, 
			CAST(
				CASE
					 WHEN RequestTypeID = 1 
						THEN 'LEAVE'
					 WHEN RequestTypeID = 2
						THEN 'LOAN'	
					 ELSE ''
				END AS text ) as RequestTypeName,
			 RequestDate, RequestTitle, rm.Value, Unit, rm.Picture, StartDate, EndDate, Status,
				rm.LastModificationTimeStamp, ActionDetail, ActionBy, ActionDate, rm.Description, 
				rm.Keyfield,'' as col_Action
					FROM RequestMaster as rm
					inner join SalesManInformation as smi on rm.EmployeeID=smi.SalesManID
					where ".$searchbox." rm.active=1 "; 
					
		// print_r($sql);
		// return;
		   
		$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: green;cursor:pointer;" onclick="javascript:editRowApprove(this);"></i> &nbsp;&nbsp;&nbsp; 
								<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']=="InsertRequestMaster")
	{
		// print_r($_POST);
		// print_r($_FILES);
		// return;

		//$data=array_keys($_POST);
		//$data=json_decode($data[0]);  // Not required for file upload
		
		$RequestMasterID=isset($_POST['RequestMasterID'])?$_POST['RequestMasterID']:0;
		$EmployeeID=isset($_POST['EmployeeID'])?$_POST['EmployeeID']:0;
		$RequestTypeID=isset($_POST['RequestTypeID'])?$_POST['RequestTypeID']:0;
		$RequestDate=isset($_POST['RequestDate'])?$_POST['RequestDate']:Date("Y-m-d");
		$RequestTitle=isset($_POST['RequestTitle'])?$_POST['RequestTitle']:'';
		$Value=isset($_POST['Value'])?$_POST['Value']:'';
		$Unit=isset($_POST['Unit'])?$_POST['Unit']:'';
		$StartDate=isset($_POST['StartDate'])?$_POST['StartDate']:Date("Y-m-d");
		$EndDate=isset($_POST['EndDate'])?$_POST['EndDate']:Date("Y-m-d");

		$Status=isset($_POST['Status'])?$_POST['Status']:'';
		$Description=isset($_POST['Description'])?$_POST['Description']:'';

		//$StartDate=isset($_POST['StartDate'])?$_POST['StartDate']:Date("Y-m-d");
		//$EndDate=isset($_POST['EndDate'])?$_POST['EndDate']:'';

		$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($RequestMasterID>0 && $RequestMasterID!="")
		{
			if($ppic!=""){	
				$updatesql="update RequestMaster set EmployeeID='".$EmployeeID."', RequestTypeID='".$RequestTypeID."', RequestDate='".$RequestDate."', 
				RequestTitle='".$RequestTitle."', Value='".$Value."', Unit='".$Unit."', Picture='".$newpic."', StartDate='".$StartDate."', EndDate='".$EndDate."', Description='".$Description."'
				where RequestMasterID=".$RequestMasterID;	
			}else{
				$updatesql="update RequestMaster set EmployeeID='".$EmployeeID."', RequestTypeID='".$RequestTypeID."', RequestDate='".$RequestDate."', 
				RequestTitle='".$RequestTitle."', Value='".$Value."', Unit='".$Unit."', StartDate='".$StartDate."', EndDate='".$EndDate."', Description='".$Description."'
				where RequestMasterID=".$RequestMasterID;
			}
		}
		else  {
			# code...
			if($ppic!="")
				$updatesql=" insert into RequestMaster ( 
							  EmployeeID,     RequestTypeID,      RequestDate,      RequestTitle,       Value,        Unit,      Picture,      StartDate,       EndDate,       Status,       Description, KeyField, Active) 
					values(".$EmployeeID.",".$RequestTypeID.",'".$RequestDate."','".$RequestTitle."','".$Value."','".$Unit."','".$newpic."','".$StartDate."','".$EndDate."','PENDING','".$Description."','',1) ";
			else
				$updatesql=" insert into RequestMaster ( 
						      EmployeeID,     RequestTypeID,      RequestDate,       RequestTitle,       Value,       Unit,       StartDate,       EndDate,     Status,    Description, KeyField, Active) 
	  				values(".$EmployeeID.",".$RequestTypeID.",'".$RequestDate."','".$RequestTitle."','".$Value."','".$Unit."','".$StartDate."','".$EndDate."','PENDING','".$Description."','',1) ";
		}	
		
		// print_r($updatesql);
		// return;

		odbc_exec($connection,$updatesql);
		$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);			
	}

	if($_GET['action']=="ApproveRequestMaster")
	{
		// print_r($_POST);
		// print_r($_FILES);
		// return;
		
		$RequestMasterID=isset($_POST['RequestMasterID'])?$_POST['RequestMasterID']:0;	
		$Status=isset($_POST['Status'])?$_POST['Status']:'';
		$ActionDetail=isset($_POST['ActionDetail'])?$_POST['ActionDetail']:'';
						
		if($RequestMasterID>0 && $RequestMasterID!="")
		{			
				$updatesql="update RequestMaster set Status='".$Status."', ActionDetail='".$ActionDetail."'
				where RequestMasterID=".$RequestMasterID;			
		}		
		
		// print_r($updatesql);
		// return;

		odbc_exec($connection,$updatesql);
		$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);			
	}

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

?>