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




$searchQuery = " ";
if($searchBydelNote != ''){
    $searchQuery .= " and (del_no like '%".$searchBydelNote."%') ";
}

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


if($searchByClientName != ''){
    $searchQuery .= " and (client_id='".$searchByClientName."') ";
}



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

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



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

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

## Fetch records
//$empQuery = "select * from delnote WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empQuery = "select * from delnote WHERE 1 ".$searchQuery." order by del_id desc limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$categoriesId = $row['del_id'];
	$delNo = $row['del_no'];
	$clientId = $row['client_id'];
	
	$sql362a = "SELECT * FROM clients where client_id='$clientId'";
              $result362a = $conn->query($sql362a);
			  $num362a = mysqli_num_rows($result362a);
              while ($row362a = mysqli_fetch_array($result362a)){
						$clientName = $row362a['client_fname']." ".$row362a['client_lname'];
			                }
	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editDelNote.php?i='.$categoriesId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="issueInvoice.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center>Issue Invoice</center></a>
	</div>';
	
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_delNote.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	$button3 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="viewClientDelNote.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(
    		"del_id"=>$x,
			"delNo"=>$delNo,
			"clientName"=>$clientName,
			"edit"=>$button,
			//"issue"=>$button1,
    		"pdfFile"=>$button2,
			//"ClientDelNote"=>$button3,
			"delete"=>$button4
    	);
		$x++;
		
		
}

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

echo json_encode($response);
