<?php

include ("config.php");

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

_post("query_field");

$fields_typo = array (

	// Informations générales
	"opt1" => "Informations générales",
	"id_couv" => "Couverture sociale",
	"sit_ress" => "Ressources",
	"sit_mat" => "Situation matrimoniale",
	"sit_enfchg" => "Nombre d'enfants à charge",
	"sit_fam" => "Situation familiale",
	"sit_log" => "Logement",
	"sit_csp" => "Catégorie socioprofessionnelle",
	"sit_etude" => "Niveau d'étude",
	"sit_pec" => "Prise en charge",
	"/opt1" => "",

	"opt14" => "Structures",
	"struct_add" => "Type d'addiction",
	"struct_res" => "Résultat",
	"/opt" => "",

	"opt5" => "Alcool",
	"alc_conso" => "Alcool",
	"alc_usage" => "Consommateur actuel",
	"alc_debut" => "Age au début de la consommation",
	"alc_ivresse" => "Nombre d'ivresses au cours du mois passé	",
	"alc_abst" => "Nombre d'abstinences déclarées > 3 mois",
	"alc_abst_duree" => "Durée d'abstinence déclarée la plus longue en mois",
	"alc_verre_jour" => "Rythme",
	"alc_jour_sem" => "Jours / semaine",
	"alc_jour_mois" => "Jours / mois",
	"alc_an" => "Jours / an",
	"alc_4_5_jour" => "4 à 5° (bière, cidre) par jour",
	"alc_4_5_occ" => "4 à 5° (bière, cidre) par occasion",
	"alc_8_12_jour" => "8 à 12° (vin, bière forte) par jour",
	"alc_8_12_occ" => "8 à 12° (vin, bière forte) par occasion",
	"alc_16_20_jour" => "16 à 20° (apéritif) par jour",
	"alc_16_20_occ" => "16 à 20° (apéritif) par occasion",
	"alc_40_jour" => "Plus de 40° par jour",
	"alc_40_occ" => "Plus de 40° par occasion",
	"alc_conso_sem" => "Nombre de jours de consommation/semaine",
	"alc_conso_jour" => "Nombre moyen de verres par jour",
	"alc_conso_occ" => "Nombre moyen de verres par occasion",
	"alc_audit" => "Score AUDIT",
	"/opt5" => "",

	// Tabac
	"opt6" => "Tabac",
	"tab_conso" => "Consommation",
	"tab_debut" => "Durée en années",
	"tab_usage" => "Nombre de gr de tabac/jour",
	"/opt6" => "",

	// Drogue
	"opt2" => "Drogue",
	"drog_canabis" => "Cannabis et dérivés",
	"drog_heroine" => "Héroine",
	"drog_codeine" => "Codéine",
	"drog_opium" => "Autres opiacés et dérivés",
	"drog_lsd" => "LSD + autres acides",
	"drog_hallu" => "Autres hallucinogènes",
	"drog_colle" => "Solvants et colles",
	"drog_amphe" => "Amphétamines",
	"drog_mdma" => "Ecstasy et dérivés",
	"drog_stimul" => "Autres stimulants",
	"drog_coke" => "Cocaïne et coca (feuilles)",
	"drog_crack" => "Crack",
	"drog_autre" => "Autres produits illicites",
	"drog_intra" => "Voie I.V",
	"/opt2" => "",

	// Médicaments
	"opt7" => "Médicaments selon prescription",
	"medic_benzo" => "Benzodiazépines",
	"medic_anxio" => "Autres anxiolytiques",
	"medic_hypno" => "Hypnotiques",
	"medic_antidepr" => "Antidépresseurs",
	"medic_neuro" => "Neuroleptiques",
	"medic_barbi" => "Barbituriques",
	"medic_autres" => "Autres psychotropes",
	"medic_methadone" => "Méthadone",
	"medic_subutex" => "Subutex",
	"medic_subst_autres" => "Autres produits de substitution",
	"/opt7" => "",

	"opt8" => "Médicaments mésusage",
	"medic_benzo_mesu" => "Benzodiazépines",
	"medic_anxio_mesu" => "Autres anxiolytiques",
	"medic_hypno_mesu" => "Hypnotiques",
	"medic_antidepr_mesu" => "Antidépresseurs",
	"medic_neuro_mesu" => "Neuroleptiques",
	"medic_barbi_mesu" => "Barbituriques",
	"medic_autres_mesu" => "Autres psychotropes",
	"medic_metha_mesu" => "Méthadone",
	"medic_subu_mesu" => "Subutex",
	"medic_subst_autres_mesu" => "Autres produits de substitution",
	"/opt8" => "",

	"opt10" => "Produits de substitution",
	"medic_iv" => "Mésusage I.V",
	"/opt10" => "",

	"opt9" => "Troubles de conduites alimentaires",
	"medic_anorexie" => "Anorexie",
	"medic_boulimie" => "Boulimie",
	"/opt9" => "",

	"opt11" => "Addictions comportementales",
	"medic_jeupath" => "Jeu pathologique",
	"/opt11" => "",

	"opt12" => "Evaluation psychiatrique",
	"medic_anxiete" => "Anxiété",
	"medic_depression" => "Dépression",
	"medic_trouble_psy" => "Troubles psychiatriques",
	"/opt12" => "",

	// Biologie
	"opt3" => "Biologie",
	"bio_vgm" => "VGM",
	"bio_ggt" => "GGT",
	"bio_asat" => "ASAT",
	"bio_alat" => "ALAT",
	"bio_cdt" => "CDT",
	"bio_alcoo" => "Alcoolémie",
	"/opt3" => "",

	"opt15" => "Prescriptions",
	"act_prescrit" => "Activités prescrites",
	"/opt15" => "",

	// Evaluation
	"opt4" => "Evaluation",
	"eval_qv" => "Qualité de vie",
	"eval_motiv" => "Motivation",
	"/opt4" => "",

	// Rosace
	"opt13" => "Rosace",
	"eval_alcool" => "Consommation d'alcool",
	"eval_tabac" => "Consommation de tabac",
	"eval_canabis" => "Consommation de canabis",
	"eval_mesusmedi" => "Mésusage de médicaments",
	"eval_prodill" => "Produits illicites, Mésusage",
	"eval_troublcond" => "Troubles des conduites alimentaires",
	"eval_probjudi" => "Problème judiciaire",
	"eval_probpro" => "Problème professionnel",
	"eval_probsoc" => "Problème socio-financier",
	"eval_probfam" => "Problème familial",
	"eval_etatpsy" => "Etat psychique",
	"eval_etatsoma" => "Etat somatique",
	"/opt13" => "",
);

include ("header.php");

echo "<form action=\"filtre.php\" method=\"post\">\n";

echo "<select name=\"query_field\" size=\"1\">\n";
echo "<option value=\"\">-- Choisir --</option>\n";

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

echo "</select>\n";

echo "<input class=\"inb\" type=\"submit\" name=\"edit\" value=\"Exécuter le filtre\" />\n";
echo "</form>\n";

if ($query_field)
{
	$alc_verre_jour_tab = array (
		1 => "Quotidien",
		2 => "Non quotidien",
	);

	$usage_tab = array (
		0 => "Non usage",
		1 => "Usage actuel",
		2 => "Ancien usage",
		3 => "Non r&eacute;ponse",
	);

	$medic_anorexie_tab = array (
		0 => "Non",
		1 => "Oui",
	);

	$medic_boulimie_tab = array (
		0 => "Non",
		1 => "Oui",
	);

	$medic_jeupath_tab = array (
		0 => "Non",
		1 => "Oui",
	);

	$id_act_tab = array (
		1 => "Di&eacute;teticienne",
		2 => "Psychologue",
		3 => "Infirmier",
		4 => "Relaxation",
		5 => "Information",
		6 => "Motivationnel",
		7 => "Maintien de l'abstinence ",
		8 => "Groupe du Mardi",
		9 => "D&eacute;tente et bien &ecirc;tre",
		10 => "Relaxation",
		11 => "Expression th&eacute;&acirc;trale et corporelle",
		12 => "Mouvement d'anciens buveurs",
	);

	$act_faite_tab = &$id_act_tab;

	$eval_tabac_tab = array (
		0 => "Aucun probl&egrave;me",
		1 => "Probl&egrave;me inapparent",
		2 => "Plaintes uniquements",
		3 => "Cons&eacute;quences concr&egrave;tes",
	);

	$eval_alcool_tab = &$eval_tabac_tab;
	$eval_canabis_tab = &$eval_tabac_tab;
	$eval_mesusmedi_tab = &$eval_tabac_tab;
	$eval_prodill_tab = &$eval_tabac_tab;
	$eval_troublcond_tab = &$eval_tabac_tab;
	$eval_probjudi_tab = &$eval_tabac_tab;
	$eval_probpro_tab = &$eval_tabac_tab;
	$eval_probsoc_tab = &$eval_tabac_tab;
	$eval_probfam_tab = &$eval_tabac_tab;
	$eval_etatpsy_tab = &$eval_tabac_tab;
	$eval_etatsoma_tab = &$eval_tabac_tab;

	$drog_canabis_tab = &$usage_tab;
	$drog_heroine_tab = &$usage_tab;
	$drog_codeine_tab = &$usage_tab;
	$drog_opium_tab = &$usage_tab;
	$drog_lsd_tab = &$usage_tab;
	$drog_hallu_tab = &$usage_tab;
	$drog_colle_tab = &$usage_tab;
	$drog_amphe_tab = &$usage_tab;
	$drog_mdma_tab = &$usage_tab;
	$drog_stimul_tab = &$usage_tab;
	$drog_coke_tab = &$usage_tab;
	$drog_crack_tab = &$usage_tab;
	$drog_autre_tab = &$usage_tab;
	$drog_intra_tab = &$usage_tab;

	$medic_benzo_tab = &$usage_tab;
	$medic_anxio_tab = &$usage_tab;
	$medic_hypno_tab = &$usage_tab;
	$medic_antidepr_tab = &$usage_tab;
	$medic_neuro_tab = &$usage_tab;
	$medic_barbi_tab = &$usage_tab;
	$medic_autres_tab = &$usage_tab;
	$medic_methadone_tab = &$usage_tab;
	$medic_subutex_tab = &$usage_tab;
	$medic_subst_autres_tab = &$usage_tab;

	$medic_benzo_mesu_tab = &$usage_tab;
	$medic_anxio_mesu_tab = &$usage_tab;
	$medic_hypno_mesu_tab = &$usage_tab;
	$medic_antidepr_mesu_tab = &$usage_tab;
	$medic_neuro_mesu_tab = &$usage_tab;
	$medic_barbi_mesu_tab = &$usage_tab;
	$medic_autres_mesu_tab = &$usage_tab;
	$medic_methadone_mesu_tab = &$usage_tab;
	$medic_subutex_mesu_tab = &$usage_tab;
	$medic_subst_autres_mesu_tab = &$usage_tab;

	$denied_field = array (
		"id_consult",
		"id_mod",
		"id_pat",
		"sit_prof",
		"sit_enf",
	);

	$tables = array (
		"situpat",
		"structure",
		"alctab",
		"debut",
		"drogue",
		"medicament",
		"activite",
		"bio",
		"rosace",
	);

	$tablesLabel = array (
		"situpat" => "Informations générales",
		"alctab" => "Alcool / Tabac",
		"drogue" => "Drogue",
		"medicament" => "Médicaments",
		"bio" => "Biologie",
		"rosace" => "Evaluation",
	);

	$fields = "";

	$c = count($tables);

	for ($i = 0; $i < $c; $i++)
	{
		$q = "SHOW COLUMNS FROM " . $tables[$i];
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
				{
					if (ereg("int", $n["Type"], $regs) && !in_array($n["Field"], $denied_field))
						$fields[$tables[$i]][] = $n["Field"];
				}
			}
		}
		else
			echo "Erreur mécanique " . mysql_errno() . " : " . mysql_error();
	}

	$sFields = "";

	foreach($fields AS $table => $lFields)
	{
		$c = count($lFields);
		for ($i = 0; $i < $c; $i++)
		{
		  if (isset($tableSort) && is_array($tableSort)) {
		    foreach($tableSort AS $key => $value)
		      {
			if (ereg("^" . $key, $lFields[$i], $regs))
			  $sFields[$value][] = $lFields[$i];
		      }
		  }
		}
	}

	$medic_iv_tab = array (
		1 => "Oui",
		2 => "Non",
	);

	$medic_jeupath_tab = $medic_iv_tab;

	$id_couv_tab = &$couv_tab;
	$struct_add_tab = &$produits_tab;
	$struct_res_tab = &$resultats_tab;

	$join_tables = array (
		"situpat" => " LEFT JOIN situpat ON (situpat.id_consult = consultation.id_consult)",
		"structure" => " LEFT JOIN structure ON (structure.id_consult = consultation.id_consult)",
		"alctab" => " LEFT JOIN alctab ON (alctab.id_consult = consultation.id_consult)",
		"debut" => " LEFT JOIN debut ON (debut.id_pat = consultation.id_pat)",
		"drogue" => " LEFT JOIN drogue ON (drogue.id_consult = consultation.id_consult)",
		"medicament" => " LEFT JOIN medicament ON (medicament.id_consult = consultation.id_consult)",
		"activite" => " LEFT JOIN activite ON (activite.id_consult = consultation.id_consult)",
		"bio" => " LEFT JOIN bio ON (bio.id_consult = consultation.id_consult)",
		"rosace" => " LEFT JOIN rosace ON (rosace.id_consult = consultation.id_consult)",
	);

	$tmp = "";

	foreach($fields AS $table => $lFields)
	{
		if (in_array($query_field, $lFields))
		{
			$tmp = $table;
			break;
		}
	}

	if ($query_field == "act_prescrit")
	{
		$q = "SELECT c.consu_date, c.consu_type, a.id_act, a.act_prescrit, a.act_effectue FROM consultation c LEFT JOIN activite a ON (a.id_consult = c.id_consult) WHERE 1 && c.id_pat = '" . $_SESSION["Sid_pat"] . "' && a.act_prescrit != '0000-00-00' ORDER BY c.consu_date DESC";
		$listAct = queryList($q);
		$nbAct = count($listAct);

		if ($nbAct)
		{
			echo "<br />";
			echo "<table>";

			echo "<tr>";
			echo "<th>Date de consultation</th>";
			echo "<th>Type de consultation</th>";
			echo "<th>Activité</th>";
			echo "<th>Prescrit le</th>";
			echo "<th>Effectué le</th>";
			echo "</tr>";

			foreach ($listAct as $act)
			{
				echo "<tr>";
				echo "<td>" . format_date($act["consu_date"]) . "</td>";
				echo "<td>" . $consu_type_tab[$act["consu_type"]] . "</td>";
				echo "<td>" . $id_act_tab[$act["id_act"]] . "</td>";
				echo "<td>" . format_date($act["act_prescrit"]) . "</td>";
				echo "<td>" . ($act["act_effectue"] != "0000-00-00" ? format_date($act["act_prescrit"]) : "&nbsp;") . "</td>";
				echo "</tr>";
			}
			echo "</table>";
		}
	}
	else
	{
		$q = "SELECT consultation.consu_date, consultation.consu_type, " . $tmp . "." . $query_field . " FROM consultation" . $join_tables[$tmp] . " WHERE 1 && consultation.id_pat = '" . $_SESSION["Sid_pat"] . "' ORDER BY consultation.consu_date DESC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				$nFields = mysql_num_fields($r);

				echo "<br />";
				echo "<table>\n";
				echo "<tr>\n";

				echo "<th>Date de consultation</th>\n";
				for ($i = 1; $i < $nFields; $i++)
				{
					if (mysql_field_name($r, $i) == "consu_type")
					{
						echo "<th>Type de consultation</th>\n";
						continue;
					}

					echo "<th>" . (!empty($fields_typo[mysql_field_name($r, $i)]) ? $fields_typo[mysql_field_name($r, $i)] : mysql_field_name($r, $i)) . "</th>\n";
				}

				echo "</tr>\n";

				while ($n = mysql_fetch_assoc($r))
				{
					echo "<tr>\n";
					echo "<td>" . format_date($n["consu_date"]) . "</td>\n";

					for ($i = 1; $i < $nFields; $i++)
					{
						echo "<td>";

						if (isset(${mysql_field_name($r, $i) . "_tab"}))
						{
							if (!empty(${mysql_field_name($r, $i) . "_tab"}[$n[mysql_field_name($r, $i)]]))
								echo ${mysql_field_name($r, $i) . "_tab"}[$n[mysql_field_name($r, $i)]];
						}
						else
							echo $n[mysql_field_name($r, $i)];

						echo "</td>\n";
					}

					echo "</tr>\n";
				}

				echo "</table>\n";
			}
		}
		else
			echo "Erreur mécanique " . mysql_errno() . " : " . mysql_error();
	}
}

include ("footer.php");

?>