<?php
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
include "../inc/functions.php";
$allPermissions = $_SESSION['permission'];
$requestData= $_REQUEST;
$conditions="";
$status = $_REQUEST['status'];
$branch_id = $_REQUEST['branch_id'];
if($status!=""){
    $conditions.=" and pp.status=".$status;
}

if($branch_id!=""){
    $conditions.=" and pp.branch_id=".$branch_id;
}


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id', 
            2 =>'id',
            3 =>'id'
        );
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT pp.* FROM tbl_company pp WHERE 1=1 $conditions ORDER BY 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 pp.*
         FROM tbl_company pp 
         where 1=1 $conditions";
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( pp.name LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR pp.mobile_no LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR pp.address LIKE '%".$requestData['search']['value']."%' ) ";
        }
        	 
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalFiltered = $statement->rowCount();
        
/*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=$requestData['start']+1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
 	$nestedData[] = $i;
 	
 	$_details='';
        
        	
        if($row["status"]==0){	
            $s = "<a class='btn btn-danger btn-xs'><i class='fa fa-edit'></i> In-Active</a>";
        }else{
            $s = "<a class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</a>";
        }
        
        

 	
 	$action='';
 	if($session_id==1 || $allPermissions['company']['update']=="1"){ 
 	$action.="<a href='company_details.php?key=".id_encrypt($row['id'])."' class='btn btn-warning btn-xs mr-2'><i class='fa fa-backward'></i> Update Info</a>";
 	}
 	$nestedData[] = $action;
 	$nestedData[] = "<span class='label_title'>".$row["name"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["mobile_no"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["address"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["city"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["state"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["pincode"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["country"]."</span>";
 	
	$nestedData[] = $s;
	$data[] = $nestedData;
	$i++;
}

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

echo json_encode($json_data);  

?>