<?php
ob_start();
session_start();
include("../inc/config.php");
include "../inc/session.php";
$allPermissions = $_SESSION['permission'];
function xss_clean($data){
        $data = trim($data);
        // Fix &entity\n;
        $data = str_replace(array('&amp;', '&lt;', '&gt;'), array('&amp;amp;', '&amp;lt;', '&amp;gt;'), $data);
        $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
        $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
        $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');

        // Remove any attribute starting with "on" or xmlns
        $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);

        // Remove javascript: and vbscript: protocols
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);

        // Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);

        // Remove namespaced elements (we do not need them)
        $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);

        do {
            // Remove really unwanted tags
            $old_data = $data;
            $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
        } while ($old_data !== $data);

        // we are done...
        return $data;
    }

$requestData= $_REQUEST;



/*Fetch Database column Details*/
        $columns = array( 
            0 =>'cust_name', 
            1 =>'cust_code', 
            2 =>'cust_city',
            3 =>'cust_zip',
            4 =>'cust_address',
            5 =>'client_name',
            6 =>'destination_city'
        );
 ini_set('memory_limit', '1024M');       
/*Fetch All Data Start*/    
$condition='where 1=1 ';

if($session['id']!=1 && $session_branch_ids!='0'){
        $condi.= " and tbl_user.branch_id IN ($session_branch_ids)";
    }
      
		$statement = $pdo->prepare("SELECT * FROM tbl_customer tbc  $condition" );

		$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_customer tbc  $condition";
		error_log($sql);				
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( tbc.cust_name LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR tbc.cust_code LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR tbc.cust_city LIKE '%".$requestData['search']['value']."%' ";
        	//$sql.=" OR tbc.cust_zip LIKE '%".$requestData['search']['value']."%' ";
        // 	$sql.=" OR tbu.full_name LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR tbc.destination_city 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);
		error_log($sql);

$data = array();
$i=$requestData['start']+1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
$action='';	
if($session_id==1 || $allPermissions['consignee']['update']=="1"){
	   $action.= "<div class='text-center'><div class='btn-group'>";
	 
	                    $action.=" <a style='padding: 6px;margin-right: 3px;' href='customer-edit.php?id=".$row["id"]."'  title='Update Details' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a>";
    		                
    	$action.="</div></div>";
}	
 	$nestedData[] = $i;
 	$nestedData[] = $action;
 	if($row["cust_status"]==0){
	    $nestedData[] ="<div class='text-center'><button class='btn btn-success btn-xs' onClick='delete_Details(".$row["id"].")'><i class='fa fa-eye'></i> Active</button></div>";
	}else if($row["cust_status"]==1){
	    $nestedData[] ="<div class='text-center'><button class='btn btn-danger btn-xs' onclick='DynamicActive(".$row["id"].")'><i class='fa fa-eye-slash'></i> In-Active</button></div>";
	}
 /*	
 	$nestedData[] = "<a href='customer-edit.php?id=".$row['id']."' class='btn btn-warning btn-xs'><i class='fa fa-edit'></i> Edit</a>";*/
	$nestedData[] = xss_clean($row["cust_name"]);
	$nestedData[] = xss_clean($row["cust_code"]);
	$nestedData[] = xss_clean($row["cust_city"]);
	$nestedData[] = xss_clean($row["cust_zip"]);
	$nestedData[] = xss_clean($row["cust_address"]);
	$nestedData[] = xss_clean($row["destination_city"]);
	
	$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

?>