<?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
$searchByCurrencyName = $_POST['searchByCurrencyName'];
$searchByYear = $_POST['searchByYear'];
$searchByMonth1 = $_POST['searchByMonth'];
$searchByDate = $_POST['searchByDate'];
$searchByDate11 = date('Y-m-d', strtotime($searchByDate));
$searchByStaffCurrent = 1;
## Search 



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

if($searchByYear != ''){
    $searchQuery .= " and (year='".$searchByYear."') ";
}

if($searchByMonth1 != ''){
	
	
	if($searchByMonth1==1){
	 $searchByMonth = 'JAN';
	 $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";
	  }else if($searchByMonth1==2){
	 $searchByMonth = 'FEB';	
     $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	 
	  }else if($searchByMonth1==3){
	$searchByMonth = 'MARCH';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==4){
	$searchByMonth = 'APR';	
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==5){
	$searchByMonth = 'MAY';	
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==6){
	$searchByMonth = 'JUNE';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==7){
	$searchByMonth = 'JULY';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==8){
	$searchByMonth = 'AUG';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==9){
	$searchByMonth = 'SEPT';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==10){
	$searchByMonth = 'OCT';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else if($searchByMonth1==11){
	$searchByMonth = 'NOV';
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }else{
	$searchByMonth = 'DEC';	
    $searchQuery .= " and (bank_withdrawals.month='".$searchByMonth."') ";	
	  }
	
	
}


if($searchByDate != ''){
    $searchQuery .= " and (bank_withdrawals.date='".$searchByDate11."') ";
}





## 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)){
	
	$productId = $row['bankwithdrawal_id'];
	$currencyId = $row['currency_id'];
	$amount_withdrawn = number_format($row['amount'],2);
	$withDate = $row['date'];
	
	
	$withDrawnDate = date('d-m-Y', strtotime($withDate));
	
	
	$empQuery15 = "select * from currency WHERE currency_id='$currencyId'";
    $empRecords15 = mysqli_query($conn, $empQuery15);
    while ($row15 = mysqli_fetch_assoc($empRecords15)){
		$currencyName = $row15['currencyName'];
	         }		
		 
	
	
		
		$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal"  data-target="#MyModal" data-id='.$productId.' id="getUser11"> <i class="glyphicon glyphicon-eye-open"></i></a>
	</div>';
	
	
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$productId.')"> <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('.$productId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
    $data[] = array(
    		"bankwithdrawal_id"=>$x,
			"Amount"=>$amount_withdrawn,
			"In"=>$currencyName,
			"WithDrawnDate"=>$withDrawnDate,
    		"edit"=>$button1,
			//"delete"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
