<?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
$searchByCurrency = $_POST['searchByCurrency'];
//$searchByClientName = $_POST['searchByClientName'];
//$searchByStaffCurrent = 1;
## Search 


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

//$searchQuery = " ";
//if($searchByClientInv != ''){
    //$searchQuery .= " and (inv_id like '%".$searchByClientInv."%') ";
//}




if($searchByCurrency != ''){
    $searchQuery .= " and (currency_id='".$searchByCurrency."') ";
}



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

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



## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from bank_withdrawals");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

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



## Fetch records
$empQuery = "select * from bank_withdrawals WHERE 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['bankwithdrawal_id'];
	$currencyId = $row['currency_id'];
	$withdrawnDate11 = $row['date'];
	$amount_withdrawn = number_format($row['amount'],2);
	$space = " ";
	
	$withdrawnDate = date('d-m-Y', strtotime($withdrawnDate11));
	
	
	$sql812 = "SELECT * FROM currency where currency_id='$currencyId'";
			$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$currencyName = $data812['currencyName']; 
							}
							
							
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-edit"></i></a>
	</div>';
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeProductModal" id="removeProductModalBtn" onclick="removeProduct('.$categoriesId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
	
	
    $data[] = array(
    		"withdrawId"=>$x,
			"AmountWithdrawn"=>$amount_withdrawn,
			"In"=>$currencyName,
			"withDrawnDate"=>$withdrawnDate
			//"edit"=>$button1,
			//"delete"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
