<?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'
            );
            
    $conditions="";
    
    $status = $_POST['status'];
    $category_id = $_POST['category_id'];
    $sub_category_id = $_POST['sub_category_id'];

    if($status!=""){
        $conditions.=" and m.status=".$status;
    }
    if($category_id!=""){
        $conditions.=" and m.category_id=".$category_id;
    }
    if($sub_category_id!=""){
        $conditions.=" and m.subcategory_id =".$sub_category_id;
    }

            
    /*Fetch All Data Start*/        
            
        	$statement = $pdo->prepare("SELECT m.*,c.name as category_name,sub.name as sub_category_name
                        	FROM tbl_models as m
                        	LEFT JOIN tbl_category as c ON c.id= m.category_id
                            LEFT JOIN tbl_sub_category as sub ON sub.id= m.subcategory_id
                        	WHERE m.id!=0 ".$conditions." ORDER BY m.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 m.*,c.name as category_name,sub.name as sub_category_name
                	FROM tbl_models as m
                	LEFT JOIN tbl_category as c ON c.id= m.category_id
                    LEFT JOIN tbl_sub_category as sub ON sub.id= m.subcategory_id
                	WHERE m.id!=0 ".$conditions;
             
            if( !empty($_REQUEST['search']) ) 
            {   
            	$sql.=" AND ( m.model_name LIKE '%".$_REQUEST['search']."%' "; 
            	$sql.=" OR m.name LIKE '%".$_REQUEST['search']."%' ) "; 
            	$sql.=" OR m.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);
//  error_log($sql);
$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='model-add.php?id=".$row["id"]."'  title='Update Details' 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 Model' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
	                    <i class='fa fa-trash-o'></i>
	                </button>";
    	}
		                
	$action.="</div></div>";
	
	
	$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".ucwords($row["model_name"])."</div>";
	$nestedData[] = "<div class='text-center'>".$row["model_hsn_code"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["category_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["sub_category_name"]."</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>";
	}
	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["created_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); 




?>