<?php
include("../inc/config.php");
$clientType=$_REQUEST['client_type'];
$requestData= $_REQUEST;
$del=$_REQUEST['del'];
$edit=$_REQUEST['edit'];

// $allPermissions = $_SESSION['permission'];
/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'hsncode',
            2 =>'igst',
            3 =>'cgst',
            4 =>'sgst'
        );
        
/*Fetch All Data Start*/    
    	$statement = $pdo->prepare("SELECT * FROM tbl_hsncode WHERE id!=0");   
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        $sql="SELECT * FROM tbl_hsncode WHERE id!=0";
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( hsncode LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR igst LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR cgst LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR sgst LIKE '%".$requestData['search']['value']."%' )";
        }
       
    	$statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalFiltered = $statement->rowCount();
        
/*Fetch Filter Data End*/       
        

 $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
//  $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
$statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=$requestData['start']+1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
 	$nestedData[] = $i;
//  	if($clientType=="Admin")
//  	{ 	
//      	$nestedData[] ="<a href='hsnCode-edit.php?id=".$row['id']."' class='btn btn-success btn-xs'>Edit</a> &nbsp;<a href='#' class='btn btn-danger btn-xs' data-href='hsnCode-delete.php?id=".$row['id']."' data-toggle='modal' data-target='#confirm-delete'>Delete</a>";
//  	} 
 	
 	
 	if($edit=='1')
 	{ 	
     	$action="<a href='hsnCode-edit.php?id=".$row['id']."' class='btn btn-success btn-xs'>Edit</a> &nbsp;";
 	} 
 	if($edit=='1'&&$del=='1')
 	{ 	
     	$action .=" <a href='#' class='btn btn-danger btn-xs' data-href='hsnCode-delete.php?id=".$row['id']."' data-toggle='modal' data-target='#confirm-delete'>Delete</a>";
     	$nestedData[]=$action;
 	}
 	elseif($del=='1'){
 	    $action =" <a href='#' class='btn btn-danger btn-xs' data-href='hsnCode-delete.php?id=".$row['id']."' data-toggle='modal' data-target='#confirm-delete'>Delete</a>";
 	    $nestedData[]=$action;
 	}
 	elseif($edit=='1')
 	{ 	
     	$nestedData[]=$action;
 	} 

 	
 	
 	$nestedData[] = $row["hsncode"];
	$nestedData[] = $row["igst"];
	$nestedData[] = $row["cgst"];
	$nestedData[] = $row["sgst"];
	
	$data[] = $nestedData;
	$i++;
}
        $json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format

?>