<?php

define('INSIDE' , true);
define('INSTALL' , false);
require_once dirname(__FILE__) .'/common.php';

$action_permitted = array("add", "get", "del", "sav");
$action  = $_GET['action'];

if (in_array($action, $action_permitted)) {
	if ($action == "add") {
		$page .= "
		<center>
		<br>
		<table width='600'>
		<td class='c' colspan='10' align='center'><b><font color='white'>Ajouter une Annonce</font></b></td></tr>
		<td class='c' colspan='10' align='center'><b>Ressources &agrave; Vendre</font></b></td></tr>

		<form action='annonce.php?action=sav' method='post'>
		<tr><th colspan='5'>M&eacute;tal</th><th colspan='5'><input type='texte' value='0' name='metalvendre' /></th></tr>
		<tr><th colspan='5'>Cristal</th><th colspan='5'><input type='texte' value='0' name='cristalvendre' /></th></tr>
		<tr><th colspan='5'>Deuterium</th><th colspan='5'><input type='texte' value='0' name='deutvendre' /></th></tr>
		<td class='c' colspan='10' align='center'><b>Ressources Souhait&eacute;es</font></b></td></tr>
		<tr><th colspan='5'>M&eacute;tal</th><th colspan='5'><input type='texte' value='0' name='metalsouhait' /></th></tr>
		<tr><th colspan='5'>Cristal</th><th colspan='5'><input type='texte' value='0' name='cristalsouhait' /></th></tr>
		<tr><th colspan='5'>Deuterium</th><th colspan='5'><input type='texte' value='0' name='deutsouhait' /></th></tr>
		<tr><th colspan='10'><input type='submit' value='Envoyer' /></th></tr>

		<form>
		</table>";

		display($page);
	}else if ($action == "sav") {
		$metalvendre = $_POST['metalvendre'];
		$cristalvendre = $_POST['cristalvendre'];
		$deutvendre = $_POST['deutvendre'];

		$metalsouhait = $_POST['metalsouhait'];
		$cristalsouhait = $_POST['cristalsouhait'];
		$deutsouhait = $_POST['deutsouhait'];

		$username = $user['username'];
		$galaxie = $user['galaxy'];
		$systeme = $user['system'];
		$planete = $user['planet'];

		doquery("INSERT INTO {{table}} SET
					user='{$username}',
					galaxie='{$galaxie}',
					systeme='{$systeme}',
					planete='{$planete}',
					metala='{$metalvendre}',
					cristala='{$cristalvendre}',
					deuta='{$deutvendre}',
					metals='{$metalsouhait}',
					cristals='{$cristalsouhait}',
					deuts='{$deutsouhait}'" , "annonce");
	}else if ($action == "del") {
		doquery("DELETE FROM {{table}} WHERE `id` = " . intval($_GET['id']) . ";","annonce");
		message("Annonce supprimée");
	}else if($action == "get") {
		//Récupération annonce
		$annonce = doquery("SELECT * FROM {{table}} WHERE `id` = '".intval($_GET['id'])."' ", "annonce", true);

		//Gestion ressources planète de l'annonceur
		$QryPlanet = "SELECT * FROM {{table}} ";
		$QryPlanet .= " WHERE galaxy = '".$annonce['galaxie']."' AND system = '". $annonce['systeme'] ."' AND planet = '". $annonce['planete'] ."' AND planettype = '1' ";
		$planetsAnnonceur = doquery($QryPlanet, 'planets', true);
			
		$planetsAnnonceur['metal'] = $planetsAnnonceur['metal'] - $annonce["metala"] + $annonce["metals"];
		$planetsAnnonceur['crystal'] = $planetsAnnonceur['crystal'] - $annonce["cristala"] + $annonce["cristals"];
		$planetsAnnonceur['deuterium'] = $planetsAnnonceur['deuterium'] - $annonce["deuta"] + $annonce["deuts"];
		
		$queryUpdate = "UPDATE {{table}} SET ";
		$queryUpdate .= " metal = '".$planetsAnnonceur['metal']."' AND crystal = '".$planetsAnnonceur['crystal']."' AND deuterium = '".$planetsAnnonceur['deuterium']."' ";
		$queryUpdate .= " WHERE galaxy = '". $annonce['galaxie'] ."' AND system = '". $annonce['systeme'] ."' AND planet = '". $annonce['planete'] ."' AND planettype = '1' ";
		$planete = doquery($queryUpdate, "planets");
		
		//Gestion ressources planète de l'acheteur
		$planetrow['metal'] = $planetrow['metal'] - $annonce["metala"] + $annonce["metals"];
		$planetrow['crystal'] = $planetrow['crystal'] - $annonce["cristala"] + $annonce["cristals"];
		$planetrow['deuterium'] = $planetrow['deuterium'] - $annonce["deuta"] + $annonce["deuts"];
		
		$queryUpdate = "UPDATE {{table}} SET ";
		$queryUpdate .= " metal = '".$planetrow['metal']."' AND crystal = '".$planetrow['crystal']."' AND deuterium = '".$planetrow['deuterium']."' ";
		$queryUpdate .= " WHERE galaxy = '". $planetrow['galaxy'] ."' and system = '". $planetrow['system'] ."' AND planet = '". $planetrow['planet'] ."' AND planettype = '".$planetrow['planettype']."' ";
		$planete = doquery($queryUpdate, "planets");		
		
		//suppression de l'annonce
		doquery("DELETE FROM {{table}} WHERE `id` = " . intval($_GET['id']) . ";","annonce");
	}
}

//Page principale
	$annonce = doquery("SELECT * FROM {{table}} ORDER BY `id` DESC ", "annonce");

	$page2 = "<HTML>
		<center>
		<br>
		<table width=\"600\">
		<td class=\"c\" colspan=\"10\"><font color=\"#FFFFFF\">Petites Annonces</font></td></tr>
		<tr><th colspan=\"3\">Infos de livraison</th><th colspan=\"3\">Ressources &agrave; vendre</th><th colspan=\"3\">Ressources souhait&eacute;es</th><th>Action</th></tr>
		<tr><th>Vendeur</th><th>Galaxie</th><th>Syst&egrave;me</th><th>M&eacute;tal</th><th>Cristal</th><th>Deuterium</th><th>M&eacute;tal</th><th>Cristal</th><th>Deuterium</th><th>Action</th></tr>
	";

	while ($b = mysql_fetch_array($annonce)) {
		$page2 .= '<tr><th> ';
		$page2 .= $b["user"] ;
		$page2 .= '</th><th>';
		$page2 .= $b["galaxie"];
		$page2 .= '</th><th>';
		$page2 .= $b["systeme"];
		$page2 .= '</th><th>';
		$page2 .= $b["metala"];
		$page2 .= '</th><th>';
		$page2 .= $b["cristala"];
		$page2 .= '</th><th>';
		$page2 .= $b["deuta"];
		$page2 .= '</th><th>';
		$page2 .= $b["metals"];
		$page2 .= '</th><th>';
		$page2 .= $b["cristals"];
		$page2 .= '</th><th>';
		$page2 .= $b["deuts"];
		$page2 .= '</th><th>';
		if ($b["user"] == $user["username"]) {
			$page2 .= "<a href=\"annonce.php?action=del&id={$b['id']}\">Supprimer</a>";
		}else{
			$page2 .= "<a href=\"annonce.php?action=get&id={$b['id']}\">Prendre</a>";
		}
		$page2 .= "</th></tr>";
	}

	$page2 .= "
<tr><th colspan=\"10\" align=\"center\"><a href=\"annonce.php?action=add\">Ajouter une Annonce</a></th></tr>
</td>
</table>
</HTML>";

display($page2);

// Créer par Tom1991 Copyright 2008
// Merci au site Spacon pour m'avoir donner l'inspiration
?>