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

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

if($status!=""){
    $conditions.=" and tbl_products_name.status=".$status;
}
if($stock_quantity!=""){
    if($stock_quantity==1){
        $conditions.=" and p.total_pieces!=0";
    }else if($stock_quantity==2){
        $conditions.=" and p.total_pieces=0";
    }
}
if($product_type!=""){
    $conditions.=" and tbl_products_name.type=".$product_type;
}


        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT tbl_products_name.*,
COALESCE(p.total_quantity, 0) AS total_quantity, 
COALESCE(p.total_pieces, 0) AS total_pieces 
FROM `tbl_products_name` 


LEFT JOIN  
(SELECT  
product_name, 
SUM(quantity) AS total_quantity, 
count(id) as total_pieces 
FROM tbl_product_variants 
GROUP BY product_name) 
p ON tbl_products_name.id = p.product_name


    	WHERE tbl_products_name.id!=0 ".$conditions." ORDER BY tbl_products_name.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 tbl_products_name.*,
COALESCE(p.total_quantity, 0) AS total_quantity, 
COALESCE(p.total_pieces, 0) AS total_pieces,
COALESCE(p.total_cross_weight, 0) AS total_cross_weight,
COALESCE(p.total_stone_weight, 0) AS total_stone_weight,
COALESCE(p.total_net_weight, 0) AS total_net_weight,
COALESCE(p.total_others_weight, 0) AS total_others_weight 
FROM `tbl_products_name` 

LEFT JOIN  
(SELECT  
product_name,
SUM(quantity) AS total_quantity, 
count(id) as total_pieces,
SUM(cross_weight) AS total_cross_weight, 
SUM(stone_weight) AS total_stone_weight, 
SUM(net_weight) AS total_net_weight, 
SUM(others_weight) AS total_others_weight
FROM tbl_product_variants WHERE sales_status=1
GROUP BY product_name) p ON tbl_products_name.id = p.product_name
    	WHERE tbl_products_name.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( tbl_products_name.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']."   ";
       	
       	error_log($sql);
        $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-primary btn-xs'>Ornament Type</span>";
    }



    /*if($row['total_quantity']==0){
        $stock_quantity = $row['total_quantity'];
        $total_cross_wt = 0.00;
        $total_stone_wt = 0.00;
        $total_net_wt = 0.00;
        $total_others_wt = 0.00;
    }else{
        $stock_quantity = $row['total_pieces'];
        $total_cross_wt = $row['total_cross_weight'];
        $total_stone_wt = $row['total_stone_weight'];
        $total_net_wt = $row['total_net_weight'];
        $total_others_wt = $row['total_others_weight'];
    }*/
    
        $stock_quantity = $row['total_pieces'];
        $total_cross_wt = $row['total_cross_weight'];
        $total_stone_wt = $row['total_stone_weight'];
        $total_net_wt = $row['total_net_weight'];
        $total_others_wt = $row['total_others_weight'];
        
        
//	$nestedData[] = $action;
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$stock_quantity."</div>";
	$nestedData[] = "<div class='text-center'>".$total_cross_wt."</div>";
	$nestedData[] = "<div class='text-center'>".$total_stone_wt."</div>";
	$nestedData[] = "<div class='text-center'>".$total_net_wt."</div>";
	$nestedData[] = "<div class='text-center'>".$total_others_wt."</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); 




?>