<?php

include ("config.php");

$highlight_menu = 3;

_request("id_pat");
_post("id_zone", "pat_sexe", "pat_nom", "pat_prenom", "pat_jour", "pat_mois", "pat_annee", "pat_nom_ep", "pat_adresse", "pat_code", "pat_ville", "pat_tel", "pat_mail", "pat_envoi", "pat_qui", "pat_hab", "pat_contrat", "edit_submit");

if ($_SESSION["Sid_user"] == ADMIN_USER && $id_pat)
{
	header ("Location: patient.php");
	exit();
}

if (empty($edit_submit) && $info_pat = $pat->get_patient($id_pat))
{
	$id_zone = $info_pat["id_zone"];
	$pat_sexe = $info_pat["pat_sexe"];
	$pat_nom = $info_pat["pat_nom"];
	$pat_prenom = $info_pat["pat_prenom"];
	$pat_datenaiss = $info_pat["pat_datenaiss"];
	ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", $pat_datenaiss, $regs);
	$pat_jour = $regs[3];
	$pat_mois = $regs[2];
	$pat_annee = $regs[1];
	$pat_nom_ep = $info_pat["pat_nom_ep"];
	$pat_adresse = $info_pat["pat_adresse"];
	$pat_code = $info_pat["pat_code"];
	$pat_ville = $info_pat["pat_ville"];
	$pat_tel = $info_pat["pat_tel"];
	$pat_mail = $info_pat["pat_mail"];
	$pat_envoi = $info_pat["pat_envoi"];
	$pat_qui = $info_pat["pat_qui"];
	$pat_hab = $info_pat["pat_hab"];
	$pat_contrat = $info_pat["pat_contrat"];
}

if ($edit_submit)
{
	if ($pat_nom && $pat_prenom && $pat_jour && $pat_mois && $pat_annee)
	{
		if (empty($pat_mail) || check_mail($pat_mail))
		{
			if (check_date($pat_jour, $pat_mois, $pat_annee))
			{
				$pat_datenaiss = $pat_annee . "-" . (strlen($pat_mois) == 1 ? "0" . $pat_mois : $pat_mois) . "-" . (strlen($pat_jour) == 1 ? "0" . $pat_jour : $pat_jour);

				if ($_SESSION["Sid_user"] == ADMIN_USER || !$pat->check_doublon($id_pat, $pat_nom, $pat_prenom, $pat_datenaiss))
				{
					if ($tmpID = $pat->edit_patient($id_pat, $id_zone, $pat_sexe, $pat_nom, $pat_prenom, $pat_datenaiss, $pat_nom_ep, $pat_adresse, $pat_code, $pat_ville, $pat_tel, $pat_mail, $pat_envoi, $pat_qui, $pat_hab, $pat_contrat))
					{
						$_SESSION["Sid_pat"] = $tmpID;
						session_write_close();
						if ($_SESSION["Sid_user"] == ADMIN_USER)
							header ("Location: patient.php");
						else
							header ("Location: rdv.php");
						exit();
					}
				}
				else
					$error = 8;
			}
			else
				$error = 5;
		}
		else
			$error = 3;
	}
	else
		$error = 2;
}

include ("header.php");

if (!empty($error))
{
	if (is_int($error))
		echo "<p class=\"center error\">" . $error_list[$error] . "</p>\n";
	else
		echo "<p class=\"center error\">" . $error . "</p>\n";
}

echo "<p class=\"center\">(" . req() . ") Champs &agrave; remplir obligatoirement</p>\n";

echo "<div id=\"form-patient\">\n";
echo "<form action=\"patient_edition.php\" method=\"post\">\n";
echo "<table class=\"center\">\n";

echo "<tr>\n";
echo "<th class=\"center\" colspan=\"2\">Informations concernant ce patient</th>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_nom\">Nom</label> " . req() . "</td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_nom\" id=\"pat_nom\" value=\"" . $pat_nom . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_prenom\">Pr&eacute;nom</label> " . req() . "</td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_prenom\" id=\"pat_prenom\" value=\"" . $pat_prenom . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\">Date de naissance " . req() . "</td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_jour\" id=\"pat_jour\" value=\"" . $pat_jour . "\" maxlength=\"2\" size=\"2\" />&nbsp;<input type=\"text\" class=\"int\" name=\"pat_mois\" id=\"pat_mois\" value=\"" . $pat_mois . "\" maxlength=\"2\"  size=\"2\" />&nbsp;<input type=\"text\" class=\"int\" name=\"pat_annee\" id=\"pat_annee\" value=\"" . $pat_annee . "\" maxlength=\"4\"  size=\"4\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\">Sexe</td>\n";
echo "<td>" . select("pat_sexe", $sexe_tab, $pat_sexe) . "</td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_nom_ep\">Nom d'&eacute;pouse</label></td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_nom_ep\" id=\"pat_nom_ep\" value=\"" . $pat_nom_ep . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_adresse\">Adresse</label></td>\n";
echo "<td><textarea name=\"pat_adresse\" id=\"pat_adresse\">" . nobr($pat_adresse) . "</textarea></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_code\">Code Postal</label></td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_code\" id=\"pat_code\" value=\"" . $pat_code . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_ville\">Ville</label></td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_ville\" id=\"pat_ville\" value=\"" . $pat_ville . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_tel\">T&eacute;l&eacute;phone</label></td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_tel\" id=\"pat_tel\" value=\"" . $pat_tel . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\"><label for=\"pat_mail\">E-mail</label></td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_mail\" id=\"pat_mail\" value=\"" . $pat_mail . "\" /></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\">Zone g&eacute;ographique</td>\n";
echo "<td>" . $menu->select_zone_geo($id_zone) . "</td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"r\">Date du contrat N°1</td>\n";
echo "<td><input type=\"text\" class=\"int\" name=\"pat_contrat\" id=\"pat_contrat\" value=\"" . format_date($pat_contrat) . "\" size=\"10\" maxlength=\"10\" /><span><a href=\"#\" onclick=\"return showCalendar('pat_contrat', '%d/%m/%Y');\"><img class=\"cal\" src=\"img/today.png\" alt=\"Calendrier\" /></a>&nbsp;(jj/mm/aaaa)</span></td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td class=\"center\" colspan=\"2\">";
echo "<input type=\"hidden\" name=\"id_pat\" value=\"" . $id_pat . "\" />";
echo "<input class=\"inb\" type=\"submit\" name=\"edit_submit\" value=\"Enregistrer\" />";
echo "</td>\n";
echo "</tr>\n";

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

echo "<p><a class=\"slink\" href=\"patient.php\">Retour &agrave; la liste des patients</a></p>\n";

include ("footer.php");

?>