<?php

include ("config.php");

$highlight_menu = 5;
$highlight_cousu = 4;
$display_consu = true;
$display_dossier_suite = true;

$consu_type = 3;
$structures_select = 0;

_request("id_consult");

$consu->check_consultation($id_consult, $_SESSION["Sid_pat"], $_SESSION["Sid_user"]);

// Consultation
_post("consu_date", "consu_comment", "id_mod", "id_prop", "edit_submit");

// Informations générales
_post_array("corres_coords");

_post("sit_mat", "sit_fam", "sit_log", "sit_ress", "sit_tutelle", "id_couv", "sit_etude", "sit_csp", "sit_enf", "sit_enfchg", "sit_prof", "sit_pec");
_post_array("struct_type", "struct_add", "struct_date", "struct_interv", "struct_duree", "struct_res");

// Alcool / Tabac
_post("alc_conso", "alc_usage", "alc_debut", "alc_ivresse", "alc_abst", "alc_abst_duree", "alc_verre_jour", "alc_jour_sem", "alc_jour_mois", "alc_an", "alc_4_5_jour", "alc_4_5_occ", "alc_8_12_jour", "alc_8_12_occ", "alc_16_20_jour", "alc_16_20_occ", "alc_40_jour", "alc_40_occ", "alc_conso_sem", "alc_conso_jour", "alc_conso_occ", "alc_audit", "tab_conso", "tab_debut", "tab_usage", "alc_objectif");

// Drogue
_post("drog_canabis", "drog_heroine", "drog_codeine", "drog_opium", "drog_lsd", "drog_hallu", "drog_colle", "drog_amphe", "drog_mdma", "drog_stimul", "drog_stimul_desc", "drog_coke", "drog_crack", "drog_autre", "drog_autre_desc", "drog_intra");

// Médicaments
_post("medic_benzo", "medic_benzo_mesu", "medic_anxio", "medic_anxio_mesu", "medic_hypno", "medic_hypno_mesu", "medic_antidepr", "medic_antidepr_mesu", "medic_neuro", "medic_neuro_mesu", "medic_barbi", "medic_barbi_mesu", "medic_autres", "medic_autres_mesu", "medic_autres_desc", "medic_methadone", "medic_metha_mesu", "medic_subutex", "medic_subu_mesu", "medic_subst_autres", "medic_subst_autres_mesu", "medic_iv", "medic_anorexie", "medic_boulimie", "medic_jeupath", "medic_anxiete", "medic_depression", "medic_eval_psy", "medic_trouble_psy");

// Biologie
_post("bio_spe", "bio_posol", "bio_duree", "bio_date", "bio_vgm", "bio_ggt", "bio_asat", "bio_alat", "bio_cdt", "bio_alcoo", "bio_autre");

// Evaluation
_post("eval_qv", "eval_alcool", "eval_tabac", "eval_canabis", "eval_mesusmedi", "eval_prodill", "eval_troublcond", "eval_probjudi", "eval_probpro", "eval_probsoc", "eval_probfam", "eval_etatpsy", "eval_etatsoma", "eval_motiv");

// Prescriptions
_post_array("activites");

// Nouveau rendez-vous
_post("rdv_date");

_post("prop_tera");

// On récupere les informations de la dernière consultation
if (empty($edit_submit))
{
	if ($id_consult)
	{
		$last_consult = $consu->get_consultation($id_consult);
		$last_id_consult = $id_consult;
		$consu_date = format_date($last_consult["consu_date"]);

		$last_proposition = $prop->get_proposition($last_consult["id_prop"]);
		_array($last_proposition, "id_mod", "id_prop");
	}
	else
	{
		$last_consult = $consu->get_last_consultation($_SESSION["Sid_user"], $_SESSION["Sid_pat"]);
		$last_id_consult = $last_consult["id_consult"];
		$consu_date = date("d/m/Y");

		if ($last_consult["consu_type"] == 4)
		{
			$last_proposition = $prop->get_last_proposition($_SESSION["Sid_pat"]);
			_array($last_proposition, "id_mod", "id_prop");
		}
		else
		{
			$last_proposition = $prop->get_proposition($last_consult["id_prop"]);
			_array($last_proposition, "id_mod", "id_prop");
		}
	}

	$consu_comment = $last_consult["consu_comment"];

	$last_correspondants = $corres->list_correspondants($last_id_consult);
	$c = count($last_correspondants);
	for ($i = 0; $i < $c; $i++)
		$corres_coords[$last_correspondants[$i]["id_spe"]] = $last_correspondants[$i]["corres_coords"];

	$last_situpat = $situpat->get_situpat($last_id_consult);
	_array($last_situpat, "id_couv", "sit_mat", "sit_fam", "sit_log", "sit_ress", "sit_tutelle", "id_couv", "sit_etude", "sit_csp", "sit_enf", "sit_enfchg", "sit_prof", "sit_pec", "prop_tera");

	$last_structures = $structure->list_structure($last_id_consult);

	if (!empty($last_structures) && $c = count($last_structures))
	{
		for ($i = 0; $i < $c; $i++)
		{
			$struct_type[$i] = $last_structures[$i]["struct_type"];
			$struct_add[$i] = $last_structures[$i]["struct_add"];
			$struct_date[$i] = $last_structures[$i]["struct_date"];
			$struct_interv[$i] = $last_structures[$i]["struct_interv"];
			$struct_duree[$i] = $last_structures[$i]["struct_duree"];
			$struct_res[$i] = $last_structures[$i]["struct_res"];
		}
	}

	$last_debut = $debut->get_debut($_SESSION["Sid_pat"]);
	_array($last_debut, "alc_debut", "alc_conso", "alc_usage", "alc_ivresse", "alc_abst", "alc_abst_duree", "tab_debut", "tab_usage");

	$last_alctab = $alctab->get_alctab($last_id_consult);
	_array($last_alctab, "alc_verre_jour", "alc_jour_sem", "alc_jour_mois", "alc_an", "alc_4_5_jour", "alc_4_5_occ", "alc_8_12_jour", "alc_8_12_occ", "alc_16_20_jour", "alc_16_20_occ", "alc_40_jour", "alc_40_occ", "alc_conso_sem", "alc_conso_jour", "alc_conso_occ", "alc_audit", "tab_conso", "alc_objectif");

	$last_drogue = $drogue->get_drogue($last_id_consult);
	_array($last_drogue, "drog_canabis", "drog_heroine", "drog_codeine", "drog_opium", "drog_lsd", "drog_hallu", "drog_colle", "drog_amphe", "drog_mdma", "drog_stimul", "drog_stimul_desc", "drog_coke", "drog_crack", "drog_autre", "drog_autre_desc", "drog_intra");

	$last_medicament = $medic->get_medicament($last_id_consult);
	_array($last_medicament, "medic_benzo", "medic_benzo_mesu", "medic_anxio", "medic_anxio_mesu", "medic_hypno", "medic_hypno_mesu", "medic_antidepr", "medic_antidepr_mesu", "medic_neuro", "medic_neuro_mesu", "medic_barbi", "medic_barbi_mesu", "medic_autres", "medic_autres_mesu", "medic_autres_desc", "medic_methadone", "medic_metha_mesu", "medic_subutex", "medic_subu_mesu", "medic_subst_autres", "medic_subst_autres_mesu", "medic_iv", "medic_anorexie", "medic_boulimie", "medic_jeupath", "medic_anxiete", "medic_depression", "medic_eval_psy", "medic_trouble_psy");

	$last_biologie = $bio->get_bio($last_id_consult);
	_array($last_biologie, "bio_spe", "bio_posol", "bio_duree", "bio_date", "bio_vgm", "bio_ggt", "bio_asat", "bio_alat", "bio_cdt", "bio_alcoo", "bio_autre");

	$last_rosace = $rosace->get_rosace($last_id_consult);
	_array($last_rosace, "eval_qv", "eval_alcool", "eval_tabac", "eval_canabis", "eval_mesusmedi", "eval_prodill", "eval_troublcond", "eval_probjudi", "eval_probpro", "eval_probsoc", "eval_probfam", "eval_etatpsy", "eval_etatsoma", "eval_motiv");

	$last_activites = $activite->get_activite($last_id_consult);

	$activites = array();
	foreach($last_activites AS $values)
	{
		$activites[$values["id_act"]] = array("prescrit" => ($values["act_prescrit"] != "0000-00-00" ? format_date($values["act_prescrit"]) : ""), "effectue" => ($values["act_effectue"] != "0000-00-00" ? format_date($values["act_effectue"]) : ""));
	}
}

if ($edit_submit)
{
	if ($consu_date && ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $consu_date, $regs) && check_date($regs[1], $regs[2], $regs[3]))
	{
		if ($rdv_date)
		{
			if (ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $rdv_date, $regs) && check_date($regs[1], $regs[2], $regs[3]))
				$rdv->edit_rdv($_SESSION["Sid_pat"], sql_date($rdv_date));
			else
				$error = 7;
		}

		if (empty($error))
		{
			if ($id_consult = $consu->edit_consultation($id_consult, $_SESSION["Sid_pat"], $_SESSION["Sid_user"], $consu_type, sql_date($consu_date), $consu_comment))
			{
				$situpat->edit_situpat($id_consult, $id_mod, $id_couv, $sit_ress, $sit_tutelle, $sit_prof, $sit_mat, $sit_enf, $sit_enfchg, $sit_fam, $sit_log, $sit_csp, $sit_etude, $sit_pec, $prop_tera);

				$corres->purge_correspondants($id_consult);

				foreach ($corres_coords as $key => $value)
					$corres->edit_correspondants($id_consult, $key, $value, 0);

				$alctab->edit_alctab($id_consult, $alc_verre_jour, $alc_jour_sem, $alc_jour_mois, $alc_an, $alc_4_5_jour, $alc_4_5_occ, $alc_8_12_jour, $alc_8_12_occ, $alc_16_20_jour, $alc_16_20_occ, $alc_40_jour, $alc_40_occ, $alc_conso_sem, $alc_conso_jour, $alc_conso_occ, $alc_audit, $tab_conso, $alc_objectif);

				$bio->edit_bio($id_consult, $bio_spe, $bio_posol, $bio_duree, sql_date($bio_date), $bio_vgm, $bio_ggt, $bio_asat, $bio_alat, $bio_cdt, $bio_alcoo, $bio_autre);

				$last_debut = $debut->get_debut($_SESSION["Sid_pat"]);
				_array($last_debut, "alc_debut", "alc_conso", "alc_usage", "alc_ivresse", "alc_abst", "alc_abst_duree");

				$debut->edit_debut($_SESSION["Sid_pat"], $alc_debut, $alc_conso, $alc_usage, $alc_ivresse, $alc_abst, $alc_abst_duree, $tab_debut, $tab_usage);

				$drogue->edit_drogue($id_consult, $drog_canabis, $drog_heroine, $drog_codeine, $drog_opium, $drog_lsd, $drog_hallu, $drog_colle, $drog_amphe, $drog_mdma, $drog_stimul, $drog_stimul_desc, $drog_coke, $drog_crack, $drog_autre, $drog_autre_desc, $drog_intra);

				$medic->edit_medicament($id_consult, $medic_benzo, $medic_benzo_mesu, $medic_anxio, $medic_anxio_mesu, $medic_hypno, $medic_hypno_mesu, $medic_antidepr, $medic_antidepr_mesu, $medic_neuro, $medic_neuro_mesu, $medic_barbi, $medic_barbi_mesu, $medic_autres, $medic_autres_mesu, $medic_autres_desc, $medic_methadone, $medic_metha_mesu, $medic_subutex, $medic_subu_mesu, $medic_subst_autres, $medic_subst_autres_mesu, $medic_iv, $medic_anorexie, $medic_boulimie, $medic_jeupath, $medic_anxiete, $medic_depression, $medic_eval_psy, $medic_trouble_psy);

				$rosace->edit_rosace($id_consult, $eval_qv, $eval_alcool, $eval_tabac, $eval_canabis, $eval_mesusmedi, $eval_prodill, $eval_troublcond, $eval_probjudi, $eval_probpro, $eval_probsoc, $eval_probfam, $eval_etatpsy, $eval_etatsoma, $eval_motiv);

				$structure->purge_structure($id_consult);

				if (!empty($struct_type) && $c = count($struct_type))
				{
					for ($i = 0; $i < $c; $i++)
						$structure->edit_structure($id_consult, $struct_type[$i], $struct_add[$i], $struct_date[$i], $struct_interv[$i], $struct_duree[$i], $struct_res[$i]);
				}

				$activite->del_activite($id_consult);

				foreach ($activites AS $id_act => $values)
				{
					$act_prescrit = $values["prescrit"];
					$act_effectue = $values["effectue"];

					$activite->edit_activite($id_consult, $id_act, $act_prescrit, $act_effectue);
				}

				$q = "SELECT id_consult FROM consultation WHERE consu_date > '" . sql_date($consu_date) . "' && id_pat = '" . $_SESSION["Sid_pat"] . "'";
				if (queryCheck($q))
				{
					$q = "SELECT id_prop FROM consultation WHERE consu_date < '" . sql_date($consu_date) . "' && id_pat = '" . $_SESSION["Sid_pat"] . "' ORDER BY consu_date DESC LIMIT 1";
					$id_prop = queryOneField($q);
				}

				$consu->updatePropConsultation($id_consult, $id_prop);
				$consu->regenerateCSNumero($_SESSION["Sid_pat"], $id_prop);

// 				$consu->updateConsuNumero($_SESSION["Sid_pat"], $id_consult, $id_prop);

				header ("Location: consultation.php");
				exit;
			}
		}
	}
	else
		$error = 6;
}

include ("header.php");

$form->hidden_structures();

echo "<form action=\"dossier_suite.php\" method=\"post\" name=\"rose\">\n";

echo "<div id=\"div-info\">\n"; // Début div-info

	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";

	$form->info_generales($consu_date, $consu_comment, $corres_coords, $id_couv, $sit_ress, $sit_tutelle, $sit_prof, $sit_mat, $sit_enf, $sit_enfchg, $sit_fam, $sit_log, $sit_csp, $sit_etude, $sit_pec, $structures_select, $struct_type, $struct_add, $struct_date, $struct_interv, $struct_duree, $struct_res);

echo "</div>\n"; // Fin div-info

echo "<div id=\"div-alcool\">\n"; // Début div-alcool

	$form->alcool_tabac($alc_verre_jour, $alc_jour_sem, $alc_jour_mois, $alc_an, $alc_4_5_jour, $alc_4_5_occ, $alc_8_12_jour, $alc_8_12_occ, $alc_16_20_jour, $alc_16_20_occ, $alc_40_jour, $alc_40_occ, $alc_conso_sem, $alc_conso_jour, $alc_conso_occ, $alc_audit, $tab_conso, $alc_conso, $alc_usage, $alc_debut, $alc_ivresse, $alc_abst, $alc_abst_duree, $tab_debut, $tab_usage, $alc_objectif);

echo "</div>\n"; // Fin div-alcool

echo "<div id=\"div-drogue\">\n"; // Début div-drogue

	$form->drogue($drog_canabis, $drog_heroine, $drog_codeine, $drog_opium, $drog_lsd, $drog_hallu, $drog_colle, $drog_amphe, $drog_mdma, $drog_stimul, $drog_stimul_desc, $drog_coke, $drog_crack, $drog_autre, $drog_autre_desc, $drog_intra);

echo "</div>\n"; // Fin div-drogue

echo "<div id=\"div-medic\">\n"; // Début div-medic

	$form->medicaments($medic_benzo, $medic_benzo_mesu, $medic_anxio, $medic_anxio_mesu, $medic_hypno, $medic_hypno_mesu, $medic_antidepr, $medic_antidepr_mesu, $medic_neuro, $medic_neuro_mesu, $medic_barbi, $medic_barbi_mesu, $medic_autres, $medic_autres_mesu, $medic_autres_desc, $medic_methadone, $medic_metha_mesu, $medic_subutex, $medic_subu_mesu, $medic_subst_autres, $medic_subst_autres_mesu, $medic_iv, $medic_anorexie, $medic_boulimie, $medic_jeupath, $medic_anxiete, $medic_depression, $medic_eval_psy, $medic_trouble_psy);

echo "</div>\n"; // Fin div-medic

echo "<div id=\"div-bio\">\n"; // Début div-bio

	$form->biologie($bio_date, $bio_vgm, $bio_ggt, $bio_asat, $bio_alat, $bio_cdt, $bio_alcoo, $bio_autre);

echo "</div>\n"; // Fin div-bio

echo "<div id=\"div-rosace\">\n"; // Début div-rosace

	$form->rosace($eval_qv, $eval_alcool, $eval_tabac, $eval_canabis, $eval_mesusmedi, $eval_prodill, $eval_troublcond, $eval_probjudi, $eval_probpro, $eval_probsoc, $eval_probfam, $eval_etatpsy, $eval_etatsoma, $eval_motiv);

echo "</div>\n"; // Fin div-rosace

echo "<div id=\"div-prescription\">\n"; // Début div-prescription

	$form->prescriptions($bio_spe, $bio_posol, $bio_duree, $activites);

echo "</div>\n"; // Fin div-prescription

echo "<div id=\"div-consultation\">\n"; // Début div-consultation

echo "Commentaire sur cette consultation<br />\n";
echo "<textarea name=\"consu_comment\" id=\"consu_comment\" rows=\"6\" cols=\"80\">" . nobr($consu_comment) . "</textarea><br /><br />\n";
/*
echo "Proposition thérapeutique <select name=\"prop_tera\" size=\"1\">\n";
echo "<option value=\"\">-- Choisir --</option>\n";

foreach($prop_tera_tab AS $key => $value)
{
	if (ereg("^opt", $key, $regs))
		echo "<optgroup label=\"" . $value . "\">\n";
	elseif (ereg("^/opt", $key, $regs))
		echo "</optgroup>\n";
	else
		echo "<option value=\"" . $key. "\"" . ($key == $prop_tera ? " selected=selected" : "") . ">" . $value . "</option>\n";
}

echo "</select><br /><br />\n";
*/
echo "Nouveau rendez-vous <input class=\"int\" name=\"rdv_date\" id=\"rdv_date\" value=\"\" size=\"10\" maxlength=\"10\" type=\"text\" /><span><a href=\"#\" onclick=\"return showCalendar('rdv_date', '%d/%m/%Y');\"><img class=\"cal\" src=\"img/today.png\" alt=\"Calendrier\" /></a>&nbsp;(jj/mm/aaaa)</span>\n";
echo "<br /><br />\n";

echo "<input type=\"hidden\" name=\"id_mod\" value=\"" . $id_mod . "\" />";
echo "<input type=\"hidden\" name=\"id_prop\" value=\"" . $id_prop . "\" />";
echo "<input type=\"hidden\" name=\"id_consult\" value=\"" . $id_consult . "\" />";
echo "<input type=\"submit\" class=\"inb\" name=\"edit_submit\" id=\"edit_submit\" value=\"Enregistrer cette consultation\" />";

echo "</div>\n"; // Fin div-consultation

echo "</form>\n";

include ("footer.php");

?>