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

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

	$values = json_decode ( $_POST["values"], true );

	$pdo = getPDO ();
	
	if ( $pdo )
	{
		$s0 = $pdo->prepare ( "delete from default_action_needed where document_type = :document_type and action = :action" );
		$s0->bindParam ( ':document_type', 	$_POST["document_type"] );
		$s0->bindParam ( ':action', 		$_POST["action"] );

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

			for ($i=0; $i<count($values); $i++ )
			{
				$s1 = $pdo->prepare ( "insert into default_action_needed ( document_type, department_id, staff_role_id, action ) values ( :document_type, :department_id, :staff_role_id, :action )" );
				$s1->bindParam ( ':document_type', 	$_POST["document_type"] );
				$s1->bindParam ( ':action', 		$_POST["action"] );
				$s1->bindParam ( ':department_id', 	$values [$i][0] );
				$s1->bindParam ( ':staff_role_id', 	$values [$i][1] );

				if ( $s1->execute () != true )
					$result['status'] = 'KO';
			}
		}
	}

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