<?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'];
$product_type = $_POST['product_type'];
$category_id = $_POST['category_id'];
$purity_id = $_POST['purity_id'];
$stock_quantity = $_POST['stock_quantity'];
$purchase_id = $_POST['purchase_id'];

if($status!=""){
    $conditions.=" and p.status=".$status;
}
if($product_type!=""){
    $conditions.=" and p.type=".$product_type;
}
if($category_id!=""){
    $conditions.=" and p.category=".$category_id;
}
if($purity_id!=""){
    $conditions.=" and p.purity=".$purity_id;
}
if($stock_quantity!=""){
    if($stock_quantity==1){
        $conditions.=" and p.quantity!=0";
    }else if($stock_quantity==2){
        $conditions.=" and p.quantity=0";
    }
    
}


        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT p.*,tbl_category.name as category_name, 
tbl_sub_category.name as sub_category_name,
tbl_wastage.name as wastage_name,
tbl_ids_purity.name as purity_name,
tbl_products_name.name as products_name
FROM `tbl_product_variants`  p
LEFT JOIN tbl_category ON tbl_category.id=p.category
LEFT JOIN tbl_sub_category ON tbl_sub_category.id=p.sub_category
LEFT JOIN tbl_wastage ON tbl_wastage.id=p.wastage
LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
    	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="SELECT p.*,tbl_category.name as category_name, 
tbl_sub_category.name as sub_category_name,
tbl_wastage.name as wastage_name,
tbl_ids_purity.name as purity_name,
tbl_products_name.name as products_name
FROM `tbl_product_variants`  p
LEFT JOIN tbl_category ON tbl_category.id=p.category
LEFT JOIN tbl_sub_category ON tbl_sub_category.id=p.sub_category
LEFT JOIN tbl_wastage ON tbl_wastage.id=p.wastage
LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
    	WHERE p.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( p.product_name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR tbl_category.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-view.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>";
		  if($row['barcode_items_id']==0){              
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='products-edit.php?id=".$row["id"]."' title='Edit Users' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		  }
		  if($row['barcode_items_id']==0){
		                $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-warning btn-xs'>Plain</span>";
	}else{
	    $product_type = "<span class='btn btn-primary btn-xs'>Stone</span>";
	}
	
	$categories = $row['category_name'];
	
	if($row['sub_category_name']==""){
	    $sub_category = "";
	}else{
	    $sub_category = " - ".$row['sub_category_name'];
	}
	
	$total_cross_weight = $row["cross_weight"]*$row['quantity'];
	$total_stone_weight = $row["stone_weight"]*$row['quantity'];
	$total_net_weight = $row["net_weight"]*$row['quantity'];
	
	
	//$nestedData[] = $action;
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["products_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["hsn_code"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row['quantity']."</div>";
	$nestedData[] = "<div class='text-center'>".$product_type."</span></div>";
	$nestedData[] = "<div class='text-center'>".$categories.$sub_category."</div>";
	$nestedData[] = "<div class='text-center'>".$row["cross_weight"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["stone_weight"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["net_weight"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["others_weight"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["purity_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["wastage_name"]."</div>";
	
	
    
    $nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["created_at"]))."</div>";
    $nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["updated_at"]))."</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'><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); 




?>