<?php
header('Content-Type: text/html; charset=utf-8');
require 'database.inc.php';
if (Test_User($lg_user, $lg_password) == TRUE) {
	$alertemail = false;
	$alertecourrier = false;
	if (get_config("ALERT_EMAIL") == "1") {
		$ligne_email = Client::getClientByNoClient($NoClient);
		if (strlen($ligne_email["Email"]) == 0 && $ligne_email["IgnoreAlerteEmail"] == 0) {
			$alertemail = true;
		}
	}
	if (get_config("ALERT_COURRIER") == "1") {
		$ligne_courrier = Client::getClientByNoClient($NoClient);
		if (strtolower($ligne_courrier["MailingPapier"]) == "wrong") {
			$alertecourrier = true;
		}
	}
	$resultat_client = AlertesClients::getAlerteClientActifByNoClient($NoClient);
	$nb_resultat_client = count($resultat_client);
	$resultat_vhc = AlertesVehicules::getAlerteVehiculeActifByNoChassis($NoChasis);
	$nb_resultat_vhc = count($resultat_vhc);
	//$requette_vhc = "SELECT a.*, v.MarqueModel, v.Model FROM vehicules as v, alertes_vehicules as a WHERE v.Client = $NoClient AND v.NoChasis = a.NoChasis AND a.Etat = 'actif'";
	//$resultat_vhc = mysql_query($requette_vhc);
	//$nb_resultat_vhc = mysql_num_rows($resultat_vhc);
	if ($nb_resultat_client > 0 || $nb_resultat_vhc > 0 || $alertemail || $alertecourrier) {
		echo "$('#div_alert').html('');\n";
		if ($nb_resultat_client > 0 || $alertemail || $alertecourrier) {
			echo "$('#div_alert').append('<h2 style=\"padding: 10px 10px; margin: 0;\">Alerte client</h2>');\n";
			foreach ($resultat_client as $ligne_client) {
				echo "$('#div_alert').append('<p style=\"padding: 0px 20px 10px; margin: 0;\">" . str_replace("\r\n", "<br />", htmlentities($ligne_client["Message"], ENT_QUOTES)) . "</p>');\n";
			}
			if ($alertemail) {
				echo "$('#div_alert').append('<p style=\"padding: 0px 20px 10px; margin: 0;\" class=\"alertemail\">Manque adresse email <input type=\"text\" name=\"newemail\" class=\"newemailajax\"><button onclick=\"save_email_ajax($NoClient);\" type=\"button\">Sauver</button> <button onclick=\"ignore_alerte_email_ajax($NoClient);\" type=\"button\">Ignorer</button></p>');\n";
			}
			if ($alertecourrier) {
				echo "$('#div_alert').append('<p style=\"padding: 0px 20px 10px; margin: 0;\" class=\"alertecourrier\">Adresse courrier fausse <button onclick=\"change_adresse_courrier($NoClient);\" type=\"button\">Changer</button></p>');\n";
			}
		}
		if ($nb_resultat_vhc > 0) {
			echo "$('#div_alert').append('<h2 style=\"padding: 10px 10px; margin: 0;\">Alerte véhicule</h2>');\n";
			foreach ($resultat_vhc as $ligne_vhc) {
				echo "$('#div_alert').append('<p style=\"padding: 0px 20px 10px; margin: 0;\">" . $ligne_vhc["NoChasis"] . " " . $ligne_vhc["MarqueModel"] . " " . $ligne_vhc["Model"] . " :<br />" . str_replace("\r", " ", str_replace("\n", " ", str_replace("\n\r", "<br />", str_replace("\r\n", "<br />", htmlentities($ligne_vhc["Message"], ENT_QUOTES))))) . "</p>');\n";
			}
		}
		echo "$('#div_alert').show();\n";
	} else {
		echo "$('#div_alert').hide();\n";
	}
}
