<?php
require_once '../../Server/connection.php';

## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Custom Field value
$searchByAssetName = $_POST['searchByAssetName'];
$searchByItemType = $_POST['searchByItemType'];
//$searchByStaffCurrent = 1;
## Search 




//if($searchByClientFileStatus=='Pending Files'){
	//$searchByClientFStatus = 0;
//}else{
	//$searchByClientFStatus = 1;
//}

$searchQuery = " ";
if($searchByAssetName != ''){
    $searchQuery .= " and (assets.asset_name like '%".$searchByAssetName."%') ";
}




if($searchByItemType != ''){
    $searchQuery .= " and (assets.itemType_id='".$searchByItemType."') ";
}




if($searchValue != ''){ 
    $searchQuery .= " and (assets.asset_name like '%".$searchValue."%')";
}

//if($searchByStaffCurrent != ''){
    //$searchQuery .= " and (status_id='".$searchByStaffCurrent."') ";
//}



## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from assets,itemtypes where assets.itemType_id=itemtypes.itemType_id and assets.itemType_id!=5");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($conn,"select count(*) as allcount from assets,itemtypes WHERE assets.itemType_id=itemtypes.itemType_id and assets.itemType_id!=5 and 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from assets,itemtypes WHERE assets.itemType_id=itemtypes.itemType_id and assets.itemType_id!=5 and 1 ".$searchQuery." order by assets.asset_name limit ".$row.",".$rowperpage;
//$empQuery = "select * from collected_payments,invoices WHERE collected_payments.inv_id=invoices.inv_id and 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$categoriesId = $row['asset_id'];
	$assetName = $row['asset_name'];
	$serialNo = $row['serial_no'];
	$Value = number_format($row['value'],2);
	$remarks = $row['remarks'];
	$itemTypeName = $row['name'];
	
	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editAsset.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_receipt.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
	//$button1 = '<!-- Single button -->
	//<div class="btn-group">
	    //<a type="button" class="btn btn-primary" href="uploaded_Docs.php?i='.$categoriesId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	//</div>';
	
	
	$button4 = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeCategoriesModal" id="removeCategoriesModalBtn" onclick="removeCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
	
    $data[] = array(
    		"asset_id"=>$x,
			"itemTypeName"=>$itemTypeName,
			"assetName"=>$assetName,
			"serialNo"=>$serialNo,
			"Value"=>$Value,
			"remarks"=>$pmodeName,
			"edit"=>$button,
			"delete"=>$button4
    	);
		$x++;
		
	
		
		
		
}




## Response
$response = array(
    "draw" => intval($draw),
	//"iTotalRecords" => $totalAmountwithFilter7,
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
	//"iTotalAmount" => $totalAmountwithFilter7,
    "aaData" => $data
);

echo json_encode($response);
