<?php
session_start();
require_once '../../Server/connection.php';
//$staff_id = $_SESSION['staff_id'];
//$query153 = "select * from staff where staff_id='$staff_id'";
//$result153 = $conn->query($query153);
//$row153 = $result153->fetch_array();
//$des_id = $row153['des_id'];
## 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
//$searchByPermName = $_POST['searchByPermName'];
//$searchByroleName = $_POST['searchByroleName'];
$clientId = $_POST['clientId'];
## Search 

## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from client_docs where client_id='$clientId' and role!='ACCOUNTANT'");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Fetch records
$empQuery = "select * from client_docs where client_id='$clientId' and role!='ACCOUNTANT'";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$productId = $row['doc_id'];
	$fileName = $row['fileName'];
	$roleName = $row['role'];
	
	$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
	
	//$query155 = "select * from designation where des_id='$desId'";
    //$result155 = $conn->query($query155);
    //$row155 = $result155->fetch_array();
    //$dShortName = $row155['des_short_name'];
	
	//if($fileExtension=='pdf' or $fileExtension=='doc' or $fileExtension=='docx' or $fileExtension=='xlsx' or $fileExtension=='txt' or $fileExtension=='csv'){
   $button = '<!-- Single button -->
	<div class="btn-group">
	    <a class="btn btn-primary" href="files/'.$fileName.'"  id="editOrderModalBtn"> <i class="glyphicon glyphicon-download"></i></a>
	</div>';
       //}
	   
	   //else{
	
    // $imageUrl = substr($row['fileName'], 3);	
	 //$button = "<img class='img-round' src='".$imageUrl."' style='height:100px; width:100px;'  />";
		   
     //$button ='No File Uploaded';
       //}
	   
	 if($roleName=='DCO'){
	
	$button1 = '<!-- 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>';
	
	 }else{
	
     $button1 = 'Denied To Delete';	
		 
	 }
	
	   
	
	
    $data[] = array(
    		"doc_id"=>$x,
			"FileExt"=>$fileExtension,
			"Files"=>$button,
			"UploadedBy"=>$roleName,
			"delete"=>$button1
    	);
		$x++;
		
		
}

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

echo json_encode($response);
