<?php

include ("config.php");

$highlight_menu = 3;

include ("header.php");

echo "<div id=\"list-patient\">\n";
echo "<table>\n";

echo "<tr>\n";
echo "<th class=\"center\" colspan=\"5\">Liste des patients</th>\n";
echo "</tr>\n";

$list_pat = $pat->list_patient();
$list_suivi = $suivi->list_suivi_util($_SESSION["Sid_user"]);

if ($c = count($list_pat))
{
	echo "<tr class=\"titre\">\n";
	echo "<td>1ère consultation le</td>\n";
	echo "<td>Nom</td>\n";
	echo "<td>Pr&eacute;nom</td>\n";
	echo "<td>Date de naissance</td>\n";
	echo "<td>Action</td>\n";
	echo "</tr>\n";

	for ($i = 0; $i < $c; $i++)
	{
		$class = ($i % 2 == 0 ? "" : "inter");

		if (find_in_array("id_pat", $list_pat[$i]["id_pat"], $list_suivi))
		{
			$pat_nom = $list_pat[$i]["pat_nom"];
			$pat_prenom = $list_pat[$i]["pat_prenom"];
		}
		else
		{
			$pat_nom = substr($list_pat[$i]["pat_nom"], 0, 3);
			$pat_prenom = substr($list_pat[$i]["pat_prenom"], 0, 1);
		}

		$firstConsultation = $consu->getFirstConsultation($list_pat[$i]["id_pat"]);
		$dataFirstConsultation = isset($firstConsultation["consu_date"]) ? format_date($firstConsultation["consu_date"]) : "&nbsp;";

		echo "<tr class=\"" . $class . "\">\n";
		echo "<td class=\"center\">" . $dataFirstConsultation . " </td>\n";
		echo "<td>" . $pat_nom . "</td>\n";
		echo "<td>" . $pat_prenom . "</td>\n";
		echo "<td class=\"center\">" . format_date($list_pat[$i]["pat_datenaiss"]) . "</td>\n";
		echo "<td class=\"action\">";

		if ($_SESSION["Sid_user"] == ADMIN_USER)
		{
			echo "<a class=\"slink\" href=\"patient_suivi.php?id_pat=" . $list_pat[$i]["id_pat"] . "\">Utilisateur</a> ";
		}

		if ($_SESSION["Sid_user"] == ADMIN_USER)
		{
//			echo "<a class=\"slink\" href=\"patient_edition.php?id_pat=" . $list_pat[$i]["id_pat"] . "\">Modifier</a> ";
			echo "<a class=\"sdel\" href=\"patient_suppression.php?id_pat=" . $list_pat[$i]["id_pat"] . "\">Supprimer</a>";
		}

		if (!empty($list_suivi) && $_SESSION["Sid_user"] != ADMIN_USER)
		{
			foreach ($list_suivi AS $key => $value)
			{
				if ($value["id_pat"] == $list_pat[$i]["id_pat"])
				{
					echo "<a class=\"slink\" href=\"patient_edition.php?id_pat=" . $list_pat[$i]["id_pat"] . "\">Modifier</a> ";
					echo "<a class=\"sdel\" href=\"patient_suppression.php?id_pat=" . $list_pat[$i]["id_pat"] . "\">Supprimer</a>";
				}
			}
		}

		echo "</td>\n";
		echo "</tr>\n";
	}
}
else
{
	echo "<tr>\n";
	echo "<td class=\"center error\" colspan=\"3\">Aucun patient enregistr&eacute;</td>\n";
	echo "</tr>\n";
}

echo "</table>\n";
echo "</div>\n";

echo "<p><a class=\"slink\" href=\"patient_edition.php\">Ajouter un patient</a></p>\n";

include ("footer.php");

?>