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

$requestData= $_REQUEST;


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

$category = $_POST['category'];
$created_user = $_POST['created_user'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];



if($category!=""){
    $conditions.=" and s.category='".$category."'";
}
if($created_user!=""){
    $conditions.=" and s.user_id=".$created_user;
}

if($start_date!="" && $end_date!=""){
    $conditions.=" and s.date BETWEEN '$start_date' AND '$end_date'";
}


    
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT s.*,cu.name as created_user 
    	FROM `tbl_notifications` s
    	LEFT JOIN tbl_users cu ON cu.id=s.user_id
    	WHERE s.status!=0 ".$conditions." ORDER BY s.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 s.*,cu.name as created_user 
    	FROM `tbl_notifications` s
    	LEFT JOIN tbl_users cu ON cu.id=s.user_id
    	WHERE s.status!=0  ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( s.message LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR s.category 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/sales-view-modal.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='pos-edit.php?id=".$row["id"]."' title='Edit Sales' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                
		                
		if($row["status"]==1){                
		                 $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/payment-add-modal.php?id=".$row["id"]."' data-toggle='ajax-modal' title='Payment' class='tip btn btn-success btn-xs'>
		                    <i class='fa fa-inr' style='width: 14px;font-size: 12px;'></i>
		                </a> ";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/sales-list-modal.php?id=".$row["id"]."' id='".$row["id"]."' data-toggle='ajax-modal' title='Payment List' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-list' style=''></i>
		                </a> ";
		}
                        $action.="<a style='padding: 6px; margin-right: 3px;' href='sales-view.php?id=".$row["id"]."' title='Print Sales' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                $action.=" <button style='padding: 6px; margin-right: 3px;' title='Delete Sales' 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"><input type="checkbox" class="check_box" id="check_'.$row["id"].'"  name="assign_ids" value="'.$row["id"].'"></div>';
	
	$nestedData[] = "<div class='text-center'>".$row["message"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row["date"]))."</div>";
	
    $nestedData[] = "<div class='text-center'>".$row["category"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["created_user"]."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["created_at"]))."</div>";
	
	
	if($row["status"]==1){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-success btn-xs'><i class='fa fa-eye'></i> New Notifications</span></div>";
	}else if($row["status"]==2){
	    $nestedData[] = "<div class='text-center'><span class='btn btn-warning btn-xs'><i class='fa fa-eye-slash'></i> Readed</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); 




?>