<?php
include("../config.php3");
include($BIB."bib_generic.php3");

//CONSTANTES DU NOMS DES MOIS DE L'ANNEE
$MOIS = array("01"=>"JAN.","02"=>"FEV.","03"=>"MARS","04"=>"AVRIL","05"=>"MAI","06"=>"JUIN","07"=>"JUIL.","08"=>"AOUT","09"=>"SEPT.","10"=>"OCT.","11"=>"NOV.","12"=>"DEC.");


#############################################################################################################################
#>>>>>>>>>>>>Function d'affichage des liens de tri par mois
function printPagesLinks($Mounth,$Year,$MsgOrder,$btnMsgTraites)
{
	global $MOIS;
	
	$prevM = $Mounth;
	$prevY = $Year;
	
	//calcul le 1er suivant
	list($verynextM,$verynextY) = nextMounth($Mounth,$Year);
	//calcul le 1er precedent
	list($veryprevM,$veryprevY) = prevMounth($Mounth,$Year);
	
	
	
	//on se cale 6mois en arriere
	for($i=0;$i<=6;$i++)
	{	
		list($prevM,$prevY) = prevMounth($prevM,$prevY);
		//echo "<a href='?Mounth=".$prevM."&Year=".$prevY."'> ".$MOIS[$prevM]."</a> | ";
	}
		
	$nextM = $prevM;//$Mounth;
	$nextY = $prevY;//$Year;	
	
	echo "<a href='?Mounth=".$veryprevM."&Year=".$veryprevY."&MsgOrder=".$MsgOrder."&btnMsgTraites=".$btnMsgTraites."'><b> < </b></a> | ";
	//on affiche 12 mois a partir de 6 mois en arriere	
	for($i=0;$i<=12;$i++)
	{
		list($nextM,$nextY) = nextMounth($nextM,$nextY);
		if($i==6)
			echo "<b>".$MOIS[$nextM]." ".$nextY."</b> | ";
		else
			echo "<a href='?Mounth=".$nextM."&Year=".$nextY."&MsgOrder=".$MsgOrder."&btnMsgTraites=".$btnMsgTraites."'> ".$MOIS[$nextM]." ".$nextY."</a> | ";

	}	
	echo "<a href='?Mounth=".$verynextM."&Year=".$verynextY."&MsgOrder=".$MsgOrder."&btnMsgTraites=".$btnMsgTraites."'><b> > </b></a>";
	
}

//renvoit le mois suivant
function nextMounth($Mounth,$Year)
{
	if($Mounth < 12)
	{
		$nextM = sprintf("%02s",($Mounth+1));
		$nextY = $Year;
	}
	else
	{
		$nextM = "01";
		$nextY = ($Year+1);
	}
	return array($nextM,$nextY);
}
//renvoit le mois précédent
function prevMounth($Mounth,$Year)
{
	if($Mounth > 1)
	{
		$prevM = sprintf("%02s",($Mounth-1));
		$prevY = $Year;
	}
	else
	{
		$prevY = ($Year-1);
		$prevM =  "12";
	}
	return array($prevM,$prevY);
}


#############################################################################################################################
#>>>>>>>>>>>>Function d'affichage des stats
//On affiche les stats sur les types de question posées
function printStatsTypeQuestions()
{
	# quelques stats :
	$rows = main_db_query("SELECT COUNT(*) as total, Subject FROM SAV GROUP BY Subject");
	echo "<table border=1><tr class=trTitle><td>Sujet</td><td>Nb messages</td><td>Restant à traiter</tr>";
	while($row = db_fetch_array($rows))
	{
		echo "<tr><td>".$row['Subject'];
		echo "<td align=right>".$row['total'];
		
		$rows2 = main_db_query("SELECT COUNT(*) as total FROM SAV WHERE Subject='".$row['Subject']."' AND Traitement='0'");
		$row2 = db_fetch_array($rows2);
		echo "<td align=right>".$row2['total'];
	}
	echo "</table>";
}


//On affiche les stats sur les reponses apportées aux messages deja traités
function printStatsTypeReponses()
{
	echo "<table border=1 width=100%>".
		"<tr class=trTitle>".
		"<td>Pb Type<td>Réponse Type<td>%</tr>";
		
	$nbPbType = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM SAV"));

	$rows = main_db_query("SELECT COUNT(*) as total, PbType, Titre FROM SAV LEFT JOIN SAV_ReponsesType ON SAV_ReponsesType.Id=SAV.Traitement GROUP BY PbType, Traitement");
	while($row = db_fetch_array($rows))
	{
		echo "<tr><td>".$row['PbType'];
		echo "<td>".$row['Titre'];	
		echo "<td align=right>".number_format(($row['total'] / $nbPbType['total'] * 100))."%";	
	}
	echo "</table>";
}
#############################################################################################################################
#>>>>>>>>>>>>Function d'affichage des questions SAV
function afficheQuestionsSAV($MsgOrder,$sMsgTraites,$Mounth,$Year)
{
	global $SERVER_URL;
	
	$CurrentMounth = $Year."-".$Mounth;
	
	//Restriction selon l'ordre de tri
	switch($MsgOrder)
	{
		case "Date":
			$sOrder = "SAV.aDate DESC";
			break;
			
		case "Sujet":
			$sOrder = "SAV.Subject ASC";
			break;

		case "Email":
			$sOrder = "SAV.Email ASC";
			break;
			
		default:
			$sOrder = "SAV.Id DESC";
			break;		
	}

	# récup des réponses type
	$rows = main_db_query("SELECT * FROM SAV_ReponsesType ORDER BY Id ASC");
	while($row = db_fetch_assoc($rows))
	{
		$tabReponseType[$row['Id']] = $row['Titre'];
	}

			
	$rows = main_db_query("SELECT SAV.*,Players.NickName,Members.Name,Members.Origine,Members.FirstName,Members.Password,SAV_ReponsesType.Titre FROM SAV ".
			"LEFT JOIN Players ON Players.Id=SAV.PlayerId ".
			"LEFT JOIN Members ON Members.Id=Players.MemberId ".
			"LEFT JOIN SAV_ReponsesType ON SAV_ReponsesType.Id=SAV.Traitement ".
			"WHERE 1 ".$sMsgTraites." ".
			"AND aDate LIKE '".$CurrentMounth."-%' ".
			"ORDER BY ".$sOrder.", SAV.Id DESC");


	while($row = db_fetch_array($rows))
	{
		if ($row['Traitement'] == 0)
		{
			$Etat = "<font color=#FF0000>à traiter</font>";
		}
		else
		{
			$Etat = "<font color=green>Traité / Pb Type : <b>".$row['PbType']."</b> / Réponse type : <b>".$row['Titre']."</b></font>";
		}
		echo "<a name=\"".$row[Id]."\">
			<table border=0 width=100%>
			<tr><td valign=top>
			<table cellspacing=0 cellpadding=0></a>
			<tr>
			<td>
				<b>".$row[Origine]."</b> - <b>".$row[aDate]."</b>
			</td>
			</tr>
			<tr>
			<td>
				<u>Joueur :</u>
				<font color=#0000FF>				
				<b>".$row[Name]."&nbsp;".$row[FirstName]." &nbsp;</b></font><u>Pseudo :</u> &nbsp;
				
				$row[NickName] &nbsp;
				[<a href='ktf_utils.php3?ok=Send&NickName=".$row[NickName]."' target=_blank title='Cliquez pour accéder à la fiche du joueur'><font color=blue>fiche</font></a>] &nbsp;
				[<a href='".$SERVER_URL."login_box.php3?NickName=".$row['NickName']."&Password=".$row['Password']."&go=1' target=_blank title='Cliquez pour vous connecter au compte du joueur'><font color=blue>connexion</font></a>]
				
				</font>
			</td>
			</tr>
			<tr>
			<td>
			
			<u>Email :</u>	".$row[Email]." [";
			
			$rowNbMsg = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS total FROM SAV WHERE Email='".$row['Email']."'"));
			echo "<a title='Affiche tous les messages écrits par le joueur' href='Sav.php3?MsgOrder=Email&btnMsgTraites=Afficher#".$row[Id]."'><font color=blue>";
			echo $rowNbMsg['total']." message".($rowNbMsg['total'] > 1 ? "s" : "")."</font></a>";
			echo "]
			</td>
			</tr>
			<tr>
			<td>
				<u>Etat :</u> ".$Etat."
			</td>
			</tr>
			<tr>
			<td>
				<u>Sujet :</u> <b>".$row[Subject]."</b>
			</td>
			</tr>
			<tr>
			<td>
				<u>Message :</u>". $row[Contenu]."
			</td>
			</tr>
			</table>
			
			</td><td width=30% valign=top bgcolor=#F0F0F0>
			
				<b>Réponses type : </b><br>
			";
			echo "<li><a href=\"SAV_Transfert.php3?MsgOrder=".$MsgOrder."&Id=".$row['Id']."\">Transferer</a><br><br>";
			foreach($tabReponseType as $RTId => $RTTitre)
			{
				if ($RTId == 99)
					echo "<br><br>";
				echo "<li><a href=\"SAV_Repondre.php3?MsgOrder=".$MsgOrder."&ReponseId=".$RTId."&Id=".$row['Id']."\">".$RTTitre."</a>";
			}
			echo "</td></tr></table>";
			echo "<hr>";
	}

}
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Liste de mails envoyés au SAV </div>
<?php
//Message de validation d'un message envoyé
if ($Repondu == 1)
	echo "<font color=green>Message envoyé !</font>";
?>
<table width=100%><tr><td valign=top width=50%>
<form>
<input type=submit name="btnMsgTraites" value="<?php
//Définition du libellé du bouton d'affichage/masquage des msg traités
if (!isset($btnMsgTraites) or $btnMsgTraites == "Masquer")
{
	echo "Afficher";
	$btnMsgTraites = "Masquer";
	$sMsgTraites = "AND Traitement=0";
}
else
{
	echo "Masquer";
	$btnMsgTraites = "Afficher";
	$sMsgTraites = "";
}
?>">
&nbsp; les messages traités
<br><br>Ordonner les messages par (
<?php
//Par Défaut on ordonne par date
if (!isset($MsgOrder))
{
	$MsgOrder = "Date";	
}
echo $MsgOrder;
?>
) : 
<!-- Bouton de tri des message -->
<input type=submit name="MsgOrder" value="Date"> &nbsp; 
<input type=submit name="MsgOrder" value="Sujet"> &nbsp; 
<input type=submit name="MsgOrder" value="Email"> &nbsp; 
<input type=hidden name="Mounth" value="<?php echo $Mounth?>"> &nbsp; 
<input type=hidden name="Year" value="<?php echo $Year?>"> &nbsp; 
</form>
<?php

//On affiche les stats sur les types de question posées
printStatsTypeQuestions();

?>

<td valign=top>
<?php
	
//On affiche les stats sur les reponses apportées aux messages deja traités
printStatsTypeReponses();
?>
</td></tr></table>
<br><hr>
<?php
//Affichage des liens par mois
if(!$Mounth )
	$Mounth = date("m");
if(!$Year )
	$Year = date("Y");

/*		
list($nextM,$nextY) = nextMounth($Mounth,$Year);
list($prevM,$prevY) = prevMounth($Mounth,$Year);
*/
echo "<hr>";
printPagesLinks($Mounth,$Year,$MsgOrder,$btnMsgTraites);
echo "<hr><hr>";
afficheQuestionsSAV($MsgOrder,$sMsgTraites,$Mounth,$Year);

echo "<hr>";
printPagesLinks($Mounth,$Year,$MsgOrder,$btnMsgTraites);
echo "<hr>";
?>

<hr>
