<?php

	error_log ( "##################################################" );
	error_log ( "### get_action_mandatory_from_default ###" );

	require_once '../../autoload.php';
	require_once '../common/tools.php';

	

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

	$ext_id		= $_POST["ext_id"];
	$action_id	= $_POST["action_id"];
	$ext_type	= $_POST["ext_type"];
	$doc_type	= $_POST["doc_type"];

	error_log ( "doc_type : " .$doc_type );
	error_log ( "ext_id : " .$ext_id );
	error_log ( "ext_type : " .$ext_type );
	

	$results = GetStaffFromExtID ( $ext_id, $ext_type );

	$pdo = getPDO ();
	
	if ( $pdo )
	{
		for ( $i=0; $i<count ($results); $i++ )
		{
			error_log ("= results [".$i."] : " .$results[$i]['inst_id']. " / " .$results[$i]['dep_id']. " / " .$results[$i]['staff_id']. " / " .$results[$i]['staff_role_id'] );

			$s0 = $pdo->prepare ( "select id from default_action_needed where action=:action_id and document_type=:doc_type and staff_role_id=:staff_role_id and department_id=:dep_id;" );
			$s0->bindParam ( ':action_id', 		$action_id				 		);
			$s0->bindParam ( ':doc_type', 		$doc_type				 		);
			$s0->bindParam ( ':dep_id', 		$results[$i]['dep_id'] 			);
			$s0->bindParam ( ':staff_role_id', 	$results[$i]['staff_role_id'] 	);

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

				$d0 = $s0->fetchAll ( PDO::FETCH_ASSOC );
				error_log ( "nb elems : " .count($d0) );
				for ( $j=0; $j<count ($d0); $j++ )
				{
					// array_push ( $result ['results'], $d0[$j]['id'] );
					array_push ( $result ['results'], $results[$i]['staff_id'] );
				}
			}
		}
	}

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