<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";

$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="";

$status = $_POST['status'];
$product_type = $_POST['product_type'];
$product_details = $_POST['product_details'];

if($status!=""){
    $conditions.=" and p.status=".$status;
}
if($product_type!=""){
    $conditions.=" and p.type=".$product_type;
}
if($product_details!=""){
    $conditions.=" and p.product_ids=".$product_details;
}

$sql_query = "SELECT p.*, ip.name as product_details_name
                FROM `tbl_products_name` p
                LEFT JOIN tbl_ids_products ip ON ip.id=p.product_ids";
        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("$sql_query
    	            WHERE p.id!=0 ".$conditions." ORDER BY p.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        $sql = "$sql_query
    	        WHERE p.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search'])){   
        	$sql.=" AND ( p.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='modal/products-update.php?id=".$row["id"]."' data-toggle='ajax-modal'  title='Edit Products' 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 Products' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
    }
		                
	$action.="</div></div>";
	

    if($row['type']==1){
        $product_type = "<span class='btn btn-primary btn-xs'>Product Type</span>";
    }else if($row['type']==2){
        $product_type = "<span class='btn btn-info btn-xs'>Ornament Type</span>";
    }

	$nestedData[] = $action;
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["product_hsn_code"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["product_details_name"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".$row['quantity']."</div>";
	$nestedData[] = "<div class='text-center'>".$product_type."</div>";
	
	
	$statement = $pdo->prepare("SELECT * FROM `tbl_size` s WHERE s.product_name_id=?");
	$statement->execute(array($row["id"]));
	$query = $statement->fetchAll(PDO::FETCH_ASSOC);
	$totalData = $statement->rowCount();
	$content="";
	if($totalData>0){
	    
	    foreach($query as $size){
	        $content.="<span class='btn btn-warning btn-xs' style='margin-left: 2px; padding: 0px 5px 0px 5px;'>".$size['size']."</span>";
	    }
	}
		
	$nestedData[] = "<div class='text-center'>".$content."</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); 




?>