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

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

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

		// $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 EmployeeDocumentID as row_num, EmployeeDocumentID, ed.SalesManID , SalesManName as Employee, 
					 ed.MasterValueID , MasterValue as Document, DocumentType, DocumentURL, DocumentExpiry, ed.Description ,'' as col_Action
		FROM EmployeeDocument as ed 
		inner join SalesManInformation as smi on ed.SalesManID=smi.SalesManID
		inner join master_values as mv on ed.MasterValueID=mv.MasterValueID
		where ".$searchbox." ed.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']=="InsertDocument")
	{
		// print_r($_POST);
		// print_r($_FILES);
		// return;

		//$data=array_keys($_POST);
		//$data=json_decode($data[0]);  // Not required for file upload
		
		// $EmployeeDocumentID=isset($data->EmployeeDocumentID)?$data->EmployeeDocumentID:0;
		// $SalesManID=isset($data->SalesManID)?$data->SalesManID:0;
		// $MasterValueID=isset($data->MasterValueID)?$data->MasterValueID:0;
		// $DocumentType=isset($data->DocumentType)?$data->DocumentType:'';
		// $DocumentURL=isset($data->DocumentURL)?$data->DocumentURL:'';
		// $DocumentExpiry=isset($data->DocumentExpiry)?$data->DocumentExpiry:'';
		// $Description=str_replace("_"," ",isset($data->Description)?$data->Description:'');

		$EmployeeDocumentID=isset($_POST['EmployeeDocumentID'])?$_POST['EmployeeDocumentID']:0;
		$SalesManID=isset($_POST['SalesManID'])?$_POST['SalesManID']:0;
		$MasterValueID=isset($_POST['MasterValueID'])?$_POST['MasterValueID']:0;
		$DocumentType=isset($_POST['DocumentType'])?$_POST['DocumentType']:'';
		$DocumentURL=isset($_POST['DocumentURL'])?$_POST['DocumentURL']:'';
		$DocumentExpiry=isset($_POST['DocumentExpiry'])?$_POST['DocumentExpiry']:'';
		$Description=isset($_POST['Description'])?$_POST['Description']:'';


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

		if($ppic!="")
		{
			$newfile=rand()*1200;
			$ext=strrchr($ppic,".");
			$newpic=$newfile.$ext;
			save($_FILES['DocumentURL'],'',"images/");	
			rename("images/".$ppic,"images/".$newpic);
			//$pic="PictureLocation='$newpic'";		
		}
		else
		{
			//$pic="";
			$newpic=" ";			
		}
		
		
		if($EmployeeDocumentID>0 && $EmployeeDocumentID!="")
		{
		$updatesql="update EmployeeDocument set SalesManID='".$SalesManID."', MasterValueID='".$MasterValueID."', DocumentType='".$DocumentType."', 
					DocumentURL='".$DocumentURL."', DocumentExpiry='".$DocumentExpiry."',Description='".$Description."' where EmployeeDocumentID=".$EmployeeDocumentID;	
		}

		else  {
				# code...
				if($ppic!="")
						$updatesql="insert into EmployeeDocument (SalesManID,MasterValueID,DocumentType,DocumentURL,DocumentExpiry,Description,Active) 
						values(".$SalesManID.",".$MasterValueID.",'".$DocumentType."','".$newpic."','".$DocumentExpiry."','".$Description."',1)";
				else
						$updatesql="insert into EmployeeDocument (SalesManID,MasterValueID,DocumentType,DocumentExpiry,Description,Active) 
						values(".$SalesManID.",".$MasterValueID.",'".$DocumentType."','".$DocumentExpiry."','".$Description."',1)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
			echo json_encode($res);
	}

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

}
?>