<?php
function affichage_guerre($tab){
	global $tableau_result;
	$tableau_result=array();
	$tab_equipe1=split(';',$tab['alliance1']);
	array_pop($tab_equipe1);
	$equipe1='';
	$tab_equipe2=split(';',$tab['alliance2']);
	array_pop($tab_equipe2);
	$equipe2='';
	foreach($tab_equipe1 as $valeur){
		$query="SELECT tag FROM matgraph_ally WHERE id='".$valeur."'";
		$sql=mysql_query($query);
		list($tag)=mysql_fetch_array($sql);
		$equipe1.="<option value='".$valeur."'>".$tag."</option>\n";		
	}
	foreach($tab_equipe2 as $valeur){
		$query="SELECT tag FROM matgraph_ally WHERE id='".$valeur."'";
		$sql=mysql_query($query);
		list($tag)=mysql_fetch_array($sql);
		$equipe2.="<option value='".$valeur."'>".$tag."</option>\n";		
	}
	$message='';
	if(isset($_REQUEST['recherche']) && isset($_REQUEST['form'])){
		$message.="<form name=\"result_search-".$_REQUEST['form']."\"><input type=\"hidden\" id=\"result\" value=\"";
		if($_REQUEST['recherche']!=''){
			$query="SELECT id,tag FROM matgraph_ally WHERE tag LIKE '".$_REQUEST['recherche']."%'";
		}else{
			$query="SELECT id,tag FROM matgraph_ally WHERE 1";
		}
		$sql=mysql_query($query) or die(mysql_error());
		while($row=mysql_fetch_array($sql)){
			$result.=$row['tag']."~".$row['id'].";";
		}
		$message.=$result."\" /></form>";
	}else{
	$message.='<div id="resultat1'.$tab['id'].'" style="display:none;"></div>
	<table style="text-align:center;vertical-align:top;">
		<tr>
			<td style="height:35px;">
				<b>Equipe 1</b>
			</td>
			<td rowspan="2">
			<FORM NAME="list_equipe" action="">
	<input type="text" name="search" />
  <input type="button" value="Filtrer" onclick="search_ally_guerre(this.form.search,this.form.equipeposs,\'resultat1'.$tab['id'].'\')" /><br/><br/>
    <SELECT NAME="equipeposs" size="8" style="width:150px;">
	';
	$query="SELECT id,tag FROM matgraph_ally";
	$sql=mysql_query($query);
	while($row=mysql_fetch_array($sql)){
		$message.="<option value=\"".$row['id']."\">".$row['tag']."</option>";
	}
	$message.='	
    </SELECT>
</FORM>
			</td>
			<td style="height:35px;">
				<b>Equipe 2</b>
			</td>
		</tr>
		<tr>
			<td>
<FORM NAME="alliance1'.$tab['id'].'" action="">
    <SELECT NAME="equipe1" size="8" style="width:150px;">
	'.$equipe1.'	
    </SELECT>
	<div style="width:80px;height:30px;">
   		<input type="button" onclick="deletemenu(this.form.equipe1)" value="=&gt;" />
   		<input type="button" onclick="selectmenu(document.forms[\'list_equipe\'].equipeposs,this.form.equipe1)" value="&lt;=" />
    </div>
</FORM>';
	$message.='</td>
			<td>
	<FORM NAME="alliance2'.$tab['id'].'" action="">
    <SELECT NAME="equipe2" size="8" style="width:150px;">
	'.$equipe2.'	
    </SELECT>
	<div style="width:80px;height:30px;">
   		<input type="button" onclick="selectmenu(document.forms[\'list_equipe\'].equipeposs,this.form.equipe2)" value="=&gt;" />
   		<input type="button" onclick="deletemenu(this.form.equipe2)" value="&lt;=" />
    </div>
</FORM></td></tr><tr><td colspan="3">
	<form action="" method="post" name="guerre'.$tab['id'].'">
		<input type="hidden" name="equipe1" />
		<input type="hidden" name="equipe2" />
		<input type="hidden" name="id" value="'.$tab['id'].'" />
		<input type="button" value="Editer la Guerre" onclick="format_guerre(\''.$tab['id'].'\')">
</form></td></tr></table>';
	}
	return $message;
}
if(isset($_REQUEST['equipe1']) && isset($_REQUEST['equipe2']) && $_REQUEST['equipe1']!='' && $_REQUEST['equipe2']!=''){
	$alliance1 = $_POST["equipe1"];
	$alliance2 = $_POST["equipe2"];
	$id = $_POST['id'];
	
	$tableau_alliance1=split(';',$alliance1);
	$tableau_alliance2=split(';',$alliance2);
	array_pop($tableau_alliance1);
	array_pop($tableau_alliance2);
	$nom_alliance1='';
	foreach($tableau_alliance1 as $valeur_id){
		$query="SELECT nom FROM matgraph_ally WHERE id='".$valeur_id."'";
		$sql=mysql_query($query);
		list($nom)=mysql_fetch_array($sql);
		if($nom_alliance1==""){
			$nom_alliance1=$nom."(".$valeur_id.")";
		}else{
			$nom_alliance1.="-".$nom."(".$valeur_id.")";
		}
	}
	$nom_alliance2='';
	foreach($tableau_alliance2 as $valeur_id){
		$query="SELECT nom FROM matgraph_ally WHERE id='".$valeur_id."'";
		$sql=mysql_query($query);
		list($nom)=mysql_fetch_array($sql);
		if($nom_alliance2==""){
			$nom_alliance2=$nom."(".$valeur_id.")";
		}else{
			$nom_alliance2.="-".$nom."(".$valeur_id.")";
		}
	}

	$req = "UPDATE matgraph_guerre SET alliance1='".$alliance1."', alliance2='".$alliance2."' WHERE id = '".$id."'";
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		
	$sql4 = "INSERT INTO `matgraph_logadmin` ( `id` , `mdj` , `date` , `action` ) VALUES (
	'', '".$login."',now(), 'Edit guerre n° ".$id." | ".addslashes($nom_alliance1)." Vs ".addslashes($nom_alliance2)."')";
	$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());

	echo '<script type="text/javascript">
<!--
window.location.replace("index.php?action=guerre");
-->
</script>';
}else{
echo"<div align='center'><br><br><table width=\"659\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr style =\"background-color:#222222;\">
  	<td>&nbsp;</td>
    <td><div align=\"center\"><b>Alliance 1</b></div></td>
    <td><div align=\"center\"><b>Alliance 2</b></div></td>
    <td><div align=\"center\"><b>Début</b></div></td>
	<td><div align=\"center\"><b>Del</b></div></td>
  </tr>";
  
$req = "SELECT * FROM matgraph_guerre ORDER by date_debut DESC";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
  $i=1;
  While ( $row = mysql_fetch_array($res) ) {
	echo"<tr";
	// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo' style ="background-color:#111111;"' ;
			}
	$tab_equipe1=split(';',$row['alliance1']);
	array_pop($tab_equipe1);
	$equipe1='';
	foreach($tab_equipe1 as $valeur){
		$req1 = "SELECT tag FROM matgraph_ally WHERE id=".$valeur."";
		$res1 = mysql_query($req1) or die('Erreur SQL! <br>'.$req1.'<br>'.mysql_error());
		list($tag)= mysql_fetch_array($res1);
		if($equipe1==''){
			$equipe1.=$tag;	
		}else{
			$equipe1.="-".$tag;
		}
	}
	
	$tab_equipe2=split(';',$row['alliance2']);
	array_pop($tab_equipe2);
	$equipe2='';
	foreach($tab_equipe2 as $valeur){
		$req1 = "SELECT tag FROM matgraph_ally WHERE id=".$valeur."";
		$res1 = mysql_query($req1) or die('Erreur SQL! <br>'.$req1.'<br>'.mysql_error());
		list($tag)= mysql_fetch_array($res1);
		if($equipe2==''){
			$equipe2.=$tag;	
		}else{
			$equipe2.="-".$tag;
		}
	}
	
	echo">
	<td><input style=\"color: #5596bf; ; background-color: black ; border: 1px solid silver;\" type=\"button\" id=\"aguerre".$row['id']."\" onclick=\"aff_div('guerre".$row['id']."');\" value=\"+\" /></td>
    <td><div align=\"center\">".$equipe1."</div></td>
    <td><div align=\"center\">".$equipe2."</div></td>
    <td><div align=\"center\">$row[date_debut]</div></td>
	<td align=\"center\">
	<form name=\"suprimer\" method=\"post\" action=\"?action=guerre_del\" target=\"_parent\">
           <input type=\"hidden\" name=\"id\" value=\"$row[id]\">
          <input type=\"image\" src=\"../imgs/del.gif\" Style=\"border:0px solid #7E7E7E;width:25px;\">
                  </form>
				  </td>
  </tr>
  <tr><td colspan=\"5\">
  	<div id=\"guerre".$row['id']."\" style=\"display:none;text-align:left;padding-left:1cm;\">".affichage_guerre($row)."</div>
  </td></tr>";
  $i++;
  }
}
  
echo"</table>";

echo"<br><br><div align='center'><a href=\"?action=guerre_ajout\">Ajouter une Guerre</a><br/><br/></div></div>";
}
?>
