<?php

include ("config.php");

$highlight_menu = 5;
$highlight_cousu = 5;
$display_consu = true;
$display_dossier_final = true;

$consu_type = 4;
$structures_select = 0;

_request("id_consult");

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

// Consultation
_post("consu_date", "consu_comment", "fin_conseil", "fin_courrier", "fin_add", "fin_motiv", "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");

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

// Satisfaction
_post("sat_secret", "sat_info", "sat_rdv", "sat_demandes", "sat_accueil", "sat_soins", "sat_objectif");

// Proposition
_post("id_prop", "id_mod", "prop_date", "prop_contrat", "prop_objectif", "prop_orientation", "prop_interv", "prop_sortie", "prop_motif");

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

_post("prop_tera");
// _post("propPresc");

_post("last_id_prop");

// 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"]);
		$last_id_prop = $last_proposition["id_prop"];

		$last_proposition = $prop->get_last_proposition($_SESSION["Sid_pat"]);
		_array($last_proposition, "id_prop", "id_mod", "prop_date", "prop_contrat", "prop_objectif", "prop_orientation", "prop_interv", "prop_sortie", "prop_motif");
// 		$propPresc = $last_proposition["id_user"];
	}
	else
	{
		$last_consult = $consu->get_last_consultation($_SESSION["Sid_user"], $_SESSION["Sid_pat"]);
		$last_id_consult = $last_consult["id_consult"];
		$last_proposition = $prop->get_proposition($last_consult["id_prop"]);
		$last_id_prop = $last_proposition["id_prop"];
// 		$propPresc = $last_proposition["id_user"];
		$consu_date = date("d/m/Y");
	}

	$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);
	$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_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_sat = $sat->get_satisfaction($last_id_consult);
	_array($last_sat, "sat_secret", "sat_info", "sat_rdv", "sat_demandes", "sat_accueil", "sat_soins", "sat_objectif");

	$last_final = $final->get_final($last_id_consult);
	_array($last_final, "fin_conseil", "fin_courrier", "fin_add", "fin_motiv");
}

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 (empty($id_consult))
				$prop->close_proposition($last_id_prop);

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

				$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]);
				}

				$sat->del_satisfaction($id_consult);

				$sat->edit_satisfaction($id_consult, $sat_secret, $sat_info, $sat_rdv, $sat_demandes, $sat_accueil, $sat_soins, $sat_objectif);

				$prop->edit_proposition($id_prop, $_SESSION["Sid_pat"], $id_mod, sql_date($prop_date), sql_date($prop_contrat), $prop_objectif, $prop_orientation, $prop_interv, $prop_sortie, $prop_motif);

				$final->edit_final($id_consult, $fin_add, $fin_motiv, $fin_courrier, $fin_conseil, 0, 0, 0, 0);

				$consu->updatePropConsultation($id_consult, $last_id_prop);

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

include ("header.php");

$form->hidden_structures();

echo "<form action=\"dossier_final.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-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-proposition\">\n"; // Début div-proposition

	$form->proposition($id_prop, $id_mod, $prop_date, $prop_contrat, $prop_objectif, $prop_orientation, $prop_interv, $prop_sortie, $prop_motif);

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

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

	$form->satisfaction($sat_secret, $sat_info, $sat_rdv, $sat_demandes, $sat_accueil, $sat_soins, $sat_objectif);

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

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 "<table>\n";

echo "<tr>\n";
echo "<td>Courrier au m&eacute;decin traitant</td>\n";
echo "<td><input type=\"radio\" name=\"fin_courrier\" value=\"1\"" . ($fin_courrier == 1 ? " checked=checked" : "") . " /> oui <input type=\"radio\" name=\"fin_courrier\" value=\"0\"" . ($fin_courrier != 1 ? " checked=checked" : "") . " /> non</td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td>Conseil minimal</td>\n";
echo "<td><input type=\"radio\" name=\"fin_conseil\" value=\"1\"" . ($fin_conseil == 1 ? " checked=checked" : "") . " /> oui <input type=\"radio\" name=\"fin_conseil\" value=\"0\"" . ($fin_conseil != 1 ? " checked=checked" : "") . " /> non</td>\n";
echo "</tr>\n";

echo "<tr>\n";
echo "<td>Nouveau rendez-vous</td>\n";
echo "<td><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></td>\n";
echo "</tr>\n";

echo "</table>\n";

echo "<br />\n";

echo "<input type=\"hidden\" name=\"id_consult\" value=\"" . $id_consult . "\" />";
echo "<input type=\"hidden\" name=\"last_id_prop\" value=\"" . $last_id_prop . "\" />";
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");

?>