<?php
if (!isset($_SESSION))
{
	session_start();
}
require_once('header.php');
?>
<script>
function cat_kies(obj)
{
	CHOOSE = obj.options[obj.selectedIndex].value;
	if (CHOOSE != "niets")
	{
		document.location.href = 'waarden.php?cat='+CHOOSE;
	}
}
</script>
<?php
echo "<h1>Ruilwaarden</h1>";
if ($aangemeld == "ja")
{
	echo "<center>";
	echo "<form action='/waarden.php?' method='get' onsubmit='return false'>";
	echo "<select onchange='cat_kies(this);' name='kies'>";
	echo "<option value='niets'>Kies een collectie...</option>";
	$res = mysql_query("SELECT id, naam FROM catalogus WHERE id!=1 AND id!=2 AND id!=31 AND id!=32;");
	if (mysql_num_rows($res) >= 1)
	{
		while ($row = mysql_fetch_array($res))
		{
			$bijvoegsel = "";
			if(ISSET($_GET['cat']))
			{
				if ($_GET['cat'] == $row['id'])
				{
					$bijvoegsel = " SELECTED";
				}
			}
			$aantal = halen("SELECT count(*) FROM items WHERE cat='".$row['id']."';");
			$aantal_ruil = halen("SELECT count(*) FROM ruilen WHERE cat=".$row['id'].";");
			if (($aantal >= 1) && ($aantal_ruil >= 1))
			{
				echo "<option value='".$row['id']."'".$bijvoegsel.">".$row['naam']."</option>";
			}
		}
	}
	echo "</select>";
	echo "</form>";
	echo "</center><br>";
	if(ISSET($_GET['cat']))
	{
		echo "<center>";
		echo "<img src='hotel/images/catalogus/logo/".$_GET['cat'].".gif'><br><br>";
		echo "<table border='1' class='tabel'>";
		echo "<tr><td></td><td>Naam</td><td>In omloop</td><td>Catalogusprijs</td><td>Ruilwaarde</td><td>Koers</td></tr>";
		$csql = "SELECT cat, catnr, naam  FROM prijslijst WHERE cat=".$_GET['cat'].";";
		$cres = mysql_query($csql);
		if (mysql_num_rows($cres) >= 1)
		{
			while ($row = mysql_fetch_array($cres))
			{
				$aantal = halen("SELECT count(*) FROM items WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
				$aantal_ruil = halen("SELECT count(*) FROM ruilen WHERE cat=".$row['cat']." AND catnr='".$row['catnr']."';");
				$cat_prijs = halen("SELECT prijs FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
    			if (($aantal >= 1) && ($aantal_ruil >= 1))
    			{
	    			echo "<tr>";
	    			echo "<td><img src='hotel/images/items/".$_GET['cat']."/".$row['catnr']."/icon.gif'></td>";
	    			echo "<td>".$row['naam']."</td>";
	    			echo "<td>".$aantal."</td>";
	    			echo "<td>".$cat_prijs."</td>";
	    			$cijfer = $cat_prijs;
			    	$cijfers = 1;
			    	$ruil_prijs = round(halen("SELECT AVG(prijs) FROM ruilen WHERE cat=".$row['cat']." AND catnr=".$row['catnr'].";"));
			    	if($ruil_prijs == "0")
			    	{
				    	$ruil_prijs = "?";
			    	}
					echo "<td>".$ruil_prijs."</td>";
					if ($ruil_prijs == "?")
					{
						$ruil_koers = "onbekend";
					}
					else
					{
						if ($ruil_prijs > $cat_prijs)
						{
							$ruil_koers = "gestegen";
						}
						elseif ($ruil_prijs < $cat_prijs)
						{
							$ruil_koers = "gedaald";
						}
						else
						{
							$ruil_koers = "gelijk";
						}
					}
	    			echo "<td><img src='img/waarden/".$ruil_koers.".gif'></td>";
    				echo "</tr>";
				}
			}
		}
		echo "</table>";
		echo "</center>";
	}
	action("bekijkt de ruilwaarden");
}
else
{
	echo "Oeps! Hiervoor moet je aangemeld zijn!";
}
require_once('footer.php');
?>