<?php
	require_once '../../autoload.php';

	$result = array (
			"status"	=> 'KO',
			"results"	=> array ()
	);
	

	$pdo = getPDO ();
	
	if ( $pdo )
	{
		$doc_id = ( isset($_POST["doc_id"]) )? $_POST["doc_id"] : -1;
		$action_id = ( isset($_POST["action_id"]) )? $_POST["action_id"] : -1;

		if ( $doc_id == -1 )
		{
			$s0 = $pdo->prepare ( "select document_id, staff_member_id, last_opened, action_at from action_status_link where action_id=:action_id" );
			$s0->bindParam ( ':action_id', $action_id );
		}
		else
		{
			$s0 = $pdo->prepare ( "select document_id, staff_member_id, last_opened, action_at from action_status_link where document_id = :doc_id and action_id= :action_id" );
			$s0->bindParam ( ':doc_id', $_POST["doc_id"] );
			$s0->bindParam ( ':action_id', $action_id );
		}

		if ( $s0->execute () )
		{
			$result['status'] = 'OK';

			$d0 = $s0->fetchAll ( PDO::FETCH_ASSOC );

			for ( $i=0; $i<count($d0); $i++ )
			{
				array_push ( $result['results'], array ( 	"doc_id" 			=> $d0[$i]['document_id'], 
															"staff_member_id" 	=> $d0[$i]['staff_member_id'],
															"last_opened"		=> $d0[$i]['last_opened'],
															"action_at"			=> $d0[$i]['action_at'] ) );
			}
		}
	}





	header('Content-type: application/json');
	
	echo json_encode ( $result );
?>