<?php 
ob_start();
session_start();
include "../config/config.php";
$allPermissions = $_SESSION['permission'];

$requestData= $_REQUEST;


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'name', 
            2 =>'email',
            3 =>'password',
            4 =>'phone',
            5 =>'address',
            6 =>'role',
            7 =>'role_id',
            8 =>'id',
            9 =>'id'
           
        );
        
$conditions="";

$status = $_POST['status'];

if($status!=""){
    $conditions.=" and e.status=".$status;
}


        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT e.*, tbl_ids_products.name as product_name,
    	COALESCE(p.total_purchase_amount, 0) AS total_purchase_amount, COALESCE(p.total_purchase_count, 0) AS total_purchase_count
    	FROM tbl_suppliers e 
    	LEFT JOIN tbl_ids_products ON tbl_ids_products.id=e.product_ids
    	LEFT JOIN (SELECT supplier_id, SUM(grand_total) AS total_purchase_amount, count(id) as total_purchase_count FROM tbl_purchases GROUP BY supplier_id) p ON e.id = p.supplier_id
    	where e.id!=0 ".$conditions." ORDER BY e.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql = "SELECT e.*, tbl_ids_products.name as product_name,
         COALESCE(p.total_purchase_amount, 0) AS total_purchase_amount, COALESCE(p.total_purchase_count, 0) AS total_purchase_count
         FROM tbl_suppliers e 
         LEFT JOIN tbl_ids_products ON tbl_ids_products.id=e.product_ids
         LEFT JOIN (SELECT supplier_id, SUM(grand_total) AS total_purchase_amount, count(id) as total_purchase_count FROM tbl_purchases GROUP BY supplier_id) p ON e.id = p.supplier_id
    	WHERE e.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( e.name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR e.mobile_no LIKE '%".$_REQUEST['search']."%' ) ";
        }
        
/*Fetch Filter Data End*/       
        


         $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
       	
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
	$action= "<div class='text-center'><div class='btn-group'>";
	 
	 
	                    $action.=" <a style='padding: 6px;margin-right: 3px;' href='modal/users-view.php?id=".$row["id"]."' data-toggle='ajax-modal' title='View Details' class='tip btn btn-primary btn-xs'>
		                    <i class='fa fa-eye'></i>
		                </a>";
		                
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='suppliers-edit.php?id=".$row["id"]."' title='Edit Suppliers' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		    if($row["status"]==1){            
		                $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Suppliers' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
		    }

		                
	$action.="</div></div>";
	
	
	//$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["mobile_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row['unique_id']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['product_name']."</div>";
	
	$nestedData[] = "<div class='text-center'><span class='btn btn-danger btn-xs amount_span'><i class='fa fa-inr'></i> ".number_format($row["total_purchase_amount"],2)."</span></div>";
	

	if($row["status"]==1){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</span></div>";
	}else if($row["status"]==0){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-danger btn-xs' onclick='DynamicActive(".$row["id"].")'><i class='fa fa-eye-slash'></i> In-Active</span></div>";
	}
        	
        	
	$data[] = $nestedData;
	$i++;
	

}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data); 




?>