<?php
include("../db.php");
$requestData= $_REQUEST;
$table = $_POST['type'];
if($table == 0)
{
	$columns = array( 
		0 =>  'id_job',
		1 =>  'name_job'
	);
	$sql = "select * from `jobs` where `active_job` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_job LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_job LIKE '".$requestData['search']['value']."%' )";    
	}
}
if($table == 1)
{
	$columns = array( 
		0 =>  'id_branch',
		1 =>  'name_branch'
	);	
	$sql = "select * from `branch` where `active_branch` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_branch LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_branch LIKE '".$requestData['search']['value']."%' )";    
	}
}
if($table == 2)
{
	$columns = array( 
		0 =>  'id_cata',
		1 =>  'name_cata'
	);	
	$sql = "select * from `cata` where `active_cata` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_cata LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_cata LIKE '".$requestData['search']['value']."%' )";    
	}
}
if($table == 3)
{
	$columns = array( 
		0 =>  'id_cat',
		1 =>  'name_cat'
	);	
	$sql = "select * from `cat` where `active_cat` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_cat LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_cat LIKE '".$requestData['search']['value']."%' )";    
	}
}
if($table == 4)
{
	$columns = array( 
		0 =>  'id_expense',
		1 =>  'name_expense'
	);	
	$sql = "select * from `expense` where `active_expense` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_expense LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_expense LIKE '".$requestData['search']['value']."%' )";    
	}
}
if($table == 5)
{
	$columns = array( 
		0 =>  'id_inp',
		1 =>  'name_inp'
	);
	$sql = "select * from `inp` where `active_inp` = '0'";
	$query=mysqli_query($db, $sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	if( !empty($requestData['search']['value']) ) {  
		$sql.=" AND ( id_inp LIKE '".$requestData['search']['value']."%'  ";
		$sql.=" OR name_inp LIKE '".$requestData['search']['value']."%' )";    
	}
}
$query=mysqli_query($db, $sql);
$totalFiltered = mysqli_num_rows($query); 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$query=mysqli_query($db, $sql);
$data = array();
while( $row=mysqli_fetch_array($query)) {
	if($type == 4 or $type == 5)
	{
		if($type == 4)
		{
			$name = "صرف";
		}else{
			$name = "ايراد";
		}
		$txt = "<button type='button' class='btn btn-danger' onclick=del_branch('".$row['id_type']."')>حذف</button>
		<button type='button' class='btn btn-success'  onclick='get_type_1(".$row['id_type'].")'>تعديل</button>
		<button type='button' class='btn btn-warning'  onclick='open_mony_1(".$row['id_type'].")'>".$name."</button>
		";
	}else{
		$txt = "<button type='button' class='btn btn-danger' onclick=del_branch('".$row['id_type']."')>حذف</button>
		<button type='button' class='btn btn-success'  onclick='get_type_1(".$row['id_type'].")'>تعديل</button>";
	}
	$nestedData=array(); 
	$nestedData[] = $row["id_type"];
	$nestedData[] = $row["name_type"];
	$nestedData[] = $txt;
	
	$data[] = $nestedData;
}



$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);

?>



