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


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

	if($_GET['action']=="GetDesignation")
	{
		$data=array();
			//	$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Description=isset($_GET['Description'])?$_GET['Description']:'';
		$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		if($DesignationName!="")
		{
			$DesignationName="DesignationName like '%".$DesignationName."%' and ";
		}
		else
		{
			$DesignationName="";
		}
		if($Description!="")
		{
						$Description="Description like '%".$Description."%' and ";

		}
		else
		{
			$Description="";
		}
		$searchbox=$DesignationName."".$Description;

		$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";
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			$row['DesignationName']=htmlspecialchars($row['DesignationName']);
			$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']=="searchGetDesignation")
	{
		$DesignationName=$_GET['DesignationName'];
		$Description=isset($_GET['Description'])?$_GET['Description']:'';
		$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		if($DesignationName!="")
		{
			$DesignationName="DesignationName like '%".$DesignationName."%' and ";
		}
		else
		{
			$DesignationName="";
		}
		if($Description!="")
		{
						$Description="Description like '%".$Description."%' and ";

		}
		else
		{
			$Description="";
		}
		$searchbox=$DesignationName."".$Description;
		$data=array();
		echo $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";
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			$row['DesignationName']=htmlspecialchars($row['DesignationName']);
			$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']=="InsertDesignation")
	{
		$data=array_keys($_POST);
		$data=json_decode($data[0]);
	//	print_r($data);
		$DesignationID=isset($data->DesignationID)?$data->DesignationID:0;
		$DesignationName=isset($data->DesignationName)?$data->DesignationName:0;
		$Description=isset($data->Description)?$data->Description:0;
		if($DesignationID>0 && $DesignationID!="")
		{
		$updatesql="update pr_DesignationMaster set DesignationName='".$DesignationName."',Description='".$Description."' where DesignationID=".$DesignationID;	
		}

		else  {
				# code...
			$DesignationID=getMaxId("pr_DesignationMaster","DesignationID",$connection)+1;
			$updatesql="insert into pr_DesignationMaster (DesignationID,DesignationName,Description,Keyfield,Active) values(".$DesignationID.",'".$DesignationName."','".$Description."','',1)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);


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


?>