<?php 
ob_start();
session_start();
include "../config/config.php";
$allPermissions = $_SESSION['permission'];
$today_date = date('Y-m-d');



$requestData= $_REQUEST;

if($requestData['length']==-1){
    $requestData['length']=100000;
}


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'business_name', 
            2 =>'name',
            3 =>'mobile_no',
            4 =>'tax_number',
            5 =>'credit_limit',
            6 =>'mobile_no',
            7 =>'address',
            8 =>'customer_route_id',
            9 =>'pay_term_type',
            10 =>'balance',
            11 =>'balance',
            12 =>'groupname',
            13 =>'routename',
            14 =>'rate_name',
            15 =>'status',
            16 =>'status',
            17 =>'status',
            18 =>'status'
        );
        
$conditions="";




/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT p.*,tbl_ids_products.name as product_name,tbl_ids_purity.name as carat_name, tbl_users.name as user_name   FROM `tbl_prices` p 
LEFT JOIN tbl_ids_products ON tbl_ids_products.id=p.product_id
LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity_id
LEFT JOIN tbl_users ON tbl_users.id=p.updated_by;
    	WHERE p.status=1  ".$conditions." ORDER BY p.id ASC");
    	
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql="SELECT p.*,tbl_ids_products.name as product_name,tbl_ids_purity.name as carat_name, tbl_users.name as user_name   FROM `tbl_prices` p 
LEFT JOIN tbl_ids_products ON tbl_ids_products.id=p.product_id
LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity_id
LEFT JOIN tbl_users ON tbl_users.id=p.updated_by;
WHERE p.status=1 and ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( p.product_name LIKE '%".$_REQUEST['search']."%' "; 
        	$sql.=" OR p.user_name LIKE '%".$_REQUEST['search']."%'";
        	$sql.=" OR p.carat_name 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='price-update.php?id=".$row["id"]."' title='Update Price' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";

	
	$action.="</div></div>";
	
	$updated_date = date('Y-m-d', strtotime($row["updated_at"]));
	if($updated_date==$today_date){
	    $update_status = "<span class='btn btn-success btn-xs'>Today Updated</span>";
	}else{
	    $update_status = "<span class='btn btn-danger btn-xs'>Update Pending</span>";
	}
		 	
		 	
		 	$nestedData[] = $action;	
        	$nestedData[] = "<div class='text-center'>".$i."</div>";
        	$nestedData[] = "<div class='text-center'>".$row["product_name"]."</div>";
        	$nestedData[] = "<div class='text-center'>".$row["carat_name"]."</div>";
        	
        	$nestedData[] = "<div class='text-center'><i class='fa fa-inr'></i> ".number_format($row["amount"],2)."</div>";
        	$nestedData[] = "<div class='text-center'>".$update_status."</div>";
        	$nestedData[] = "<div class='text-center'>".$row["user_name"]."</div>";
        	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["updated_at"]))."</div>";

        	
	
	$data[] = $nestedData;
	$i++;
	

}

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

echo json_encode($json_data); 




?>