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

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

	if($_GET['action']=="GetLeave")
	{
		$data=array();
			//	$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Employee=isset($_GET['Employee'])?$_GET['Employee']:'';
		$LeaveType=isset($_GET['LeaveType'])?$_GET['LeaveType']:'';
		// $DocumentType=isset($_GET['DocumentType'])?$_GET['DocumentType']:'';
		if($Employee!="")
		{
						$Employee="SalesManName like '%".$Employee."%' and ";
		}
		else
		{
			$Employee="";
		}
		if($LeaveType!="")
		{
			$LeaveType="MasterValue like '%".$LeaveType."%' and ";
		}
		else
		{
			$LeaveType="";
		}
		// if($DocumentType!="")
		// {
		// 	$DocumentType="DocumentType like '%".$DocumentType."%' and ";
		// }
		// else
		// {
		// 	$DocumentType="";
		// }
		$searchbox=$Employee."".$LeaveType;

		// $sql="Select ROW_NUMBER() OVER (
      	// PARTITION BY [Keyfield]
      	// ORDER BY [Keyfield]
		//    ) row_num,DesignationName,DesignationID,LastModificationTimeStamp,Description,Keyfield,'' as col_Action 
		//    from pr_DesignationMaster where ".$searchbox." active=1";

		$sql="SELECT EmployeeLeaveID as row_num, EmployeeLeaveID, el.EmployeeID , SalesManName as Employee, 
					 el.LeaveTypeID , MasterValue as LeaveType, el.Value, el.Days, Avail, Balance, el.Description ,'' as col_Action
		FROM EmployeeLeave as el
		inner join SalesManInformation as smi on el.EmployeeID=smi.SalesManID
		inner join master_values as mv on el.LeaveTypeID=mv.MasterValueID
		where ".$searchbox." el.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']=="InsertLeave")
	{
		$data=array_keys($_POST);
		$data=json_decode($data[0]);
		
		$EmployeeLeaveID=isset($data->EmployeeLeaveID)?$data->EmployeeLeaveID:0;
		$EmployeeID=isset($data->EmployeeID)?$data->EmployeeID:0;
		$LeaveTypeID=isset($data->LeaveTypeID)?$data->LeaveTypeID:0;
		$Value=isset($data->Value)?$data->Value:0;
		$Days=isset($data->Days)?$data->Days:0;
		$Avail=isset($data->Avail)?$data->Avail:0;
		$Balance=isset($data->Balance)?$data->Balance:0;
		$Description=isset($data->Description)?$data->Description:'';
		
		
		if($EmployeeLeaveID>0 && $EmployeeLeaveID!="")
		{
		$updatesql="update EmployeeLeave set EmployeeID=".$EmployeeID.", LeaveTypeID=".$LeaveTypeID.", Value=".$Value.", 
					Days=".$Days.", Avail=".$Avail.",Balance=".$Balance.",Description='".$Description."' where EmployeeLeaveID=".$EmployeeLeaveID;	
		}

		else  {
				# code...
			
			$updatesql="insert into EmployeeLeave (EmployeeID,LeaveTypeID,Value,Days,Avail,Balance,Description,KeyField,Active) 
						values(".$EmployeeID.",".$LeaveTypeID.",".$Value.",".$Days.",".$Avail.",".$Balance.",'".$Description."','',1)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
			echo json_encode($res);

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

?>