<?php
include_once('database_connection.php');
if(isset($_GET['action']))
{
	//get grid data
	if($_GET['action']=="GetUnitOfMeasure")
 	{
 		if(isset($_GET['Abbreviation'])  || isset($_GET['UnitName']))
		{
		if(isset($_GET['Abbreviation']))
		$Abbreviation="Abbreviation like '".$_GET['Abbreviation']."%'";
		if(isset($_GET['UnitName']))
			$UnitName="UnitName like '".$_GET['UnitName']."%'";

		if($UnitName=="" && $Abbreviation=="")
		{
			$sql="select  ROW_NUMBER() OVER (
     
      ORDER BY [UnitName]
   ) row_num,Abbreviation,OtherInformation,UnitName,'' as col_Action,ID from 
	UnitOfMeasure where active=1  ";
		  /* $sql="select   ROW_NUMBER() OVER (
     
      ORDER BY b.[Under]
   ) row_num,a.Name as  [Abbreviation],b.Name as UnitName,b.SortOrder,'' as col_Action,a.Under,a.ID, FROM [UnitOfMeasure] as a,[UnitOfMeasure] as b

  where a.degree=2 and a.ProductBaseType='".$ProductBaseType."' and b.ID=a.under";*/
	
		}
		 else
		 {
		 	if($UnitName!="" && $Abbreviation!="")
		 	{
		 		$searchbox="(".$Abbreviation." OR ".$UnitName.")";
		 	}
		 	else if($UnitName!="" && $Abbreviation=="")
		 	{
		 		$searchbox=$UnitName;
		 	}
		 	else if($UnitName=="" && $Abbreviation!="")
		 	{
		 		$searchbox=$Abbreviation;
		 	}
		 	else
		 	{
		 		$searchbox="";
		 	}
    $sql="select  ROW_NUMBER() OVER (
     
      ORDER BY [UnitName]
   ) row_num,Abbreviation,UnitName,OtherInformation,'' as col_Action,ID from 
	UnitOfMeasure as a where active=1  and ".$searchbox."";

		 }
		}//end if search top isset($_GET['[Abbreviation]']
		else
		{
			  $sql="select  ROW_NUMBER() OVER (
     
      ORDER BY [UnitName]
   ) row_num,Abbreviation,UnitName,OtherInformation,'' as col_Action,ID from 
	UnitOfMeasure as a where active=1   ";

		}
$data=array();
//echo $sql;
	$result=odbc_exec($connection, $sql);
	//'.$row["CategoryID"].'
	//$limit=425;//
	//echo  odbc_num_rows($result);
  while ($row = odbc_fetch_array($result)) {
        $row['col_Action']='<i class="fa fa-pencil fa-lg" style="text-align: left;color: blue;cursor:pointer;" onclick="javascript:editRow(this,'.$row["ID"].');"></i>&nbsp;&nbsp;&nbsp;  <i class="fa fa-trash-o fa-lg" style="text-align: right;color: red;cursor:pointer;" onclick="javascript:deleteRow(this,'.$row["ID"].');"></i>';
        $row['Abbreviation']=htmlspecialchars($row['Abbreviation']);
        $row['UnitName']=htmlspecialchars($row['UnitName']);

    $data []=$row;
  		}//end while
  		echo json_encode($data); 
 	}//end if 

if($_GET['action']=="InsertUnitOfMeasure")
	{

		/*$data=json_encode($_POST);
		$_POST=$data;
*/		$data=array_keys($_POST);
		$d=json_decode($data[0]);
		//print_r($d);
		$_POST=$d;
		if(isset($_POST->ID) && $_POST->ID!="")
		{

		$ID=$_POST->ID;
		}
			else
		{		
			$ID=0;
		}
		$UnitName=str_replace('_',' ',$_POST->UnitName);
		$Abbreviation=str_replace('_',' ',$_POST->Abbreviation);
		$OtherInformation=str_replace('_',' ',$_POST->OtherInformation);
		$CategoryName='';
		//get max id for table 
		$id=(getMaxId("UnitOfMeasure","ID",$connection)+1);
		// check if the record already exits 
		 $sql="select * from UnitOfMeasure where ID=".$ID;
		$result=odbc_exec($connection, $sql);
		if(odbc_num_rows($result)>0)
		{
			// update query
		   $sql="update UnitOfMeasure set Abbreviation='".$Abbreviation."',OtherInformation='".$OtherInformation."',UnitName='".$UnitName."' where ID=".$ID;
		}
		else
		{
			// insert query 

  			   $sql="insert into UnitOfMeasure (ID,Abbreviation,[UnitName],[OtherInformation],Active,isDefault) values($id,'".$Abbreviation."','".$UnitName."','".$OtherInformation."',1,1)";

		}
		odbc_exec($connection, $sql) or odbc_error();
		$res=array('status'=>"ok",'description'=>'Data Saved');
		echo json_encode($res);//implode(",",$res);
	}// end action insertAbbreviation 
	if($_GET['action']=="DeleteUnitOfMeasure")
	{
	//	print_r($_GET);
		//	$data=array_keys($_POST);
		//$d=json_decode($data[0]);
		//print_r($d);
		//$_POST=$d;
		//$TextBoxName=$_GET['TextBoxName'];
		$ID=$_GET['ID'];
		$sqldelete="update UnitOfMeasure set active=0 where ID=".$ID;
			odbc_exec($connection, $sqldelete) or odbc_error();
		$res=array('status'=>"ok",'description'=>'Data Deleted Successfully');
		echo json_encode($res);//implode(",",$res);
	}//end delete MAster Value
}// end action if 

?>