<?php
	require_once '../../autoload.php';
	
	require( '../GetDataFromDB.php' );
	
	RightsCheck ("VIEW_LEGALDOCUMENT_WORKING");

	$toProcess = true;
	
	// $id			 	= $_GET ['doc_id'];
	$name		 	= $_GET ['name'];
	$type		 	= $_GET ['type'];
	
	$draw			= $_GET ['draw'];
	$start			= $_GET ['start'];
	$length			= $_GET ['length'];
	
	$columns 		= $_GET ['columns'];
	$order			= $_GET ['order'];
	$orderCol		= $order [0]['column'];
	$orderDir		= $order [0]['dir'];
	$orderColName	= $columns [$orderCol]['data'];
	$orderby		= " ORDER BY ".$orderColName." ".$orderDir;
        
	$limit = '';

	if ( isset($_GET['start']) && $_GET['length'] != -1 ) 
	{
		$limit = " OFFSET ".intval($_GET['start'])." LIMIT ".intval($_GET['length']);
	}

    $psql = "select document_link.doc_id as id, name, legaldocument.type as type, legaldocument.comment as comment from legaldocument,v_last_documentstatus, document_link, v_notremoveddocument where
    v_notremoveddocument.id = document_link.doc_id and v_last_documentstatus.status_id = 14 and v_last_documentstatus.doc_id = document_link.doc_id and document_link.type = 'legaldocument' and legaldocument.id=document_link.ext_id and document_link.type='legaldocument' and legaldocument.removed = false and name ilike '%$name%' and legaldocument.type ilike'%$type%'";
    
    error_log ( "GetDTDocumentLight : " .$psql );

	$recordsTotal 		= 1000;
	$recordsFiltered	= 0;
	
	$data				= Array ();
	
	if ( $toProcess == true )
		$data = GetDataFromDB ( $psql, $recordsFiltered, $limit, $orderby );
	
	$arr = array (
			"draw"            => intval( $draw ),
			"recordsTotal"    => intval( $recordsTotal ),
			"recordsFiltered" => intval( $recordsFiltered ),
			"data"            => $data,
			"sql"			  => $psql.$orderby.$limit
	);

	$result = json_encode ( $arr );

	echo $result;
	
 
?>