<?php 
if (!isset($_SESSION))
{
	session_start();
}
require_once ('header.php');
function credits($credits)
{
	if ($credits == 1)
	{
	    return "Credit";
	}
	else
	{
		return "Credits";
	}
}
echo "<h1>Marktplaats</h1>";
if ($aangemeld == "ja")
{
	$tralala = status($_SESSION['naam'], $_SESSION['member_posts']);
	$money_nu_member = halen("SELECT money FROM members WHERE naam='".$_SESSION['naam']."'");
	if (ISSET($_GET['sell']))
	{
		if ($tralala != "staff")
		{
			$item_cat = halen("SELECT cat FROM items WHERE nr='".$_GET['sell']."';");
			$item_catnr = halen("SELECT catnr FROM items WHERE nr='".$_GET['sell']."';");
			$item_owner = halen("SELECT eigenaar FROM items WHERE nr = '".$_GET['sell']."';");
			$item_used = halen("SELECT kamerused FROM items WHERE nr = '".$_GET['sell']."';");
			if (($_SESSION['naam'] == $item_owner) AND ($item_used == 0))
			{
				echo "<div class='reactionsz'><form action='' method='get'><b>Verkoop voor:</b><input type='text' name='sellzoveel' size='9' maxlength='9'> Credit(s)<input type='hidden' name='sellnr' value='".$_GET['sell']."'> <input type='submit' value='Ok'></form></div><br>";
				echo "<script>alert (\"Kies bovenaan op de pagina voor hoeveel credits je het meubel wilt verkopen.\");</script>";
			}
		}
		else
		{
			echo "<script>alert (\"Staffleden kunnen niets verkopen.\");</script>";
		}
	}
	elseif (ISSET($_GET['sellzoveel']))
	{
		if (($tralala != "staff") and ($_SESSION['naam'] != "test"))
		{
			$item_cat = halen("SELECT cat FROM items WHERE nr='".$_GET['sellnr']."';");
			$item_catnr = halen("SELECT catnr FROM items WHERE nr='".$_GET['sellnr']."';");
			$item_owner = halen("SELECT eigenaar FROM items WHERE nr = '".$_GET['sellnr']."';");
			$item_used = halen("SELECT kamerused FROM items WHERE nr = '".$_GET['sellnr']."';");
			if (($_SESSION['naam'] == $item_owner) AND ($item_used == 0))
			{
				if($_GET['sellzoveel'] > 10000)
				{
					echo "<script>alert(\"Niet overdrijven met je prijs!\");</script>";
				}
				elseif($_GET['sellzoveel'] < 1)
				{
					echo "<script>alert(\"Je prijs moet hoger dan 0 zijn!\");</script>";
				}
				else
				{
					$item_naam = halen("SELECT naam FROM prijslijst WHERE cat='".$item_cat."' AND catnr='".$item_catnr."';");
					mysql_query("UPDATE members SET money = '".$money_nu_member."' WHERE naam = '" . $_SESSION['naam'] . "'");
					action("verkoopt '".$item_naam."'");
					mysql_query("UPDATE items SET forsale='".$_GET['sellzoveel']."' WHERE nr='".$_GET['sellnr']."';"); 
				}
			}
			
		}
		else
		{
			echo "<script>alert (\"Staffleden kunnen niets verkopen.\");</script>";
		}
	}
	elseif (ISSET($_GET['stopsell']))
	{
		$item_cat = halen("SELECT cat FROM items WHERE nr='".$_GET['stopsell']."';");
		$item_catnr = halen("SELECT catnr FROM items WHERE nr='".$_GET['stopsell']."';");
		$item_naam = halen("SELECT naam FROM prijslijst WHERE cat='".$item_cat."' AND catnr='".$item_catnr."';");
		action("stopt met het verkopen van '".$item_naam."'");
		mysql_query("UPDATE items SET forsale='0' WHERE nr='".$_GET['stopsell']."';"); 
	}
	elseif (ISSET($_GET['buyfromowner']))
	{
		$item_prijs = halen("SELECT forsale FROM items WHERE nr='".$_GET['buyfromowner']."';");
		$item_used = halen("SELECT kamerused FROM items WHERE nr='".$_GET['buyfromowner']."';");
		if (($item_prijs != 0) AND ($item_used == 0))
		{
			$item_cat = halen("SELECT cat FROM items WHERE nr='".$_GET['buyfromowner']."';");
			$item_catnr = halen("SELECT catnr FROM items WHERE nr='".$_GET['buyfromowner']."';");
			$item_naam = halen("SELECT naam FROM prijslijst WHERE cat='".$item_cat."' AND catnr='".$item_catnr."';");
			$item_owner = halen("SELECT eigenaar FROM items WHERE nr = '".$_GET['buyfromowner']."';");
			$money_nu_seller = halen("SELECT money FROM members WHERE naam='".$item_owner."';");
			$jemoetzoveelhebben = $item_prijs;
			if ($money_nu_member < $jemoetzoveelhebben)
			{
				echo "<script>alert(\"Oeps! Zoveel credits heb je niet!\");</script>";
			}
			else
			{
				$money_nu_seller = $money_nu_seller + $item_prijs;
				$money_nu_member = $money_nu_member - $item_prijs;
				action("koopt '".$item_naam."' van ".$item_owner." voor €".$item_prijs);
				mysql_query("UPDATE members SET money = '".$money_nu_seller."' WHERE naam = '".$item_owner."'");
				mysql_query("UPDATE members SET money = '".$money_nu_member."' WHERE naam = '".$_SESSION['naam']."'");
				mysql_query("UPDATE items SET forsale=0, eigenaar='".$_SESSION['naam']."', kamerused=0, h=1 WHERE nr='".$_GET['buyfromowner']."';"); 
				mysql_query("INSERT INTO ruilen SET cat='".$item_cat."', catnr='".$item_catnr."', prijs='".$item_prijs."';");
			}
		}
	}
	elseif (ISSET($_GET['wisbuy']))
	{
		if ($tralala != "jabbo")
		{
			mysql_query("UPDATE items SET forsale=0 WHERE nr='".$_GET['wisbuy']."';"); 
		}
	}
	else
	{
		action("bekijkt de marktplaats");
	}
	echo "<b>Je hebt nog ".$money_nu_member." ".credits($money_nu_member)."</b><br><br>";
	echo "<div style='width: 564px; background-color: #D3E6F1; padding: 4px;'><font face='Verdana' size='1'><img src='img/markt_pijl1.gif' align='left'>&nbsp;<b>Aanbiedingen</b></font></div>";
	$csql = "SELECT * FROM items WHERE forsale!=0 ORDER BY forsale DESC;";
	$cres = mysql_query($csql);
	$ietstekoop = "nee";
	if (mysql_num_rows($cres) >= 1) 
	{ 
	    while ($row = mysql_fetch_array($cres)) 
	    {
		    $item_naam = halen("SELECT naam FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
			$item_img = "hotel/images/items/".$row['cat']."/".$row['catnr']."/icon.gif";
		    $item_prijs = halen("SELECT prijs FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
	    	$categorie = halen("SELECT cat FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
    		echo "<div class='reactionsz'><center><b><u>".$item_naam."</u></b> Prijs: ".$row['forsale']." ".credits($row['forsale']);
    		if ($_SESSION['naam'] != $row['eigenaar'])
    		{
	    		echo "&nbsp;&nbsp;&nbsp;<a href='?buyfromowner=".$row['nr']."'>Koop</a>";
	    		if ($tralala != "jabbo")
				{
					echo "&nbsp;&nbsp;&nbsp;<a href='?wisbuy=".$row['nr']."'>Wis</a>";
				}
    		}
    		echo "<br><img src='".$item_img."'></center>";
    		echo "<div align='right'><b>Aangeboden door ".$row['eigenaar']."</b>";
    		echo "</div></div>";
    		$ietstekoop = "ja";
		}
	}
	if ($ietstekoop == "nee")
	{
		echo "Op dit moment niets. Slechte tijden voor de handel!";
	}
	echo "<p>";
	echo "<div style='width: 564px; background-color: #D3E6F1; padding: 4px;'><font face='Verdana' size='1'><img src='img/markt_pijl1.gif' align='left'>&nbsp;<b>Jouw meubels</b></font></div>";
	$csql = "SELECT * FROM items WHERE eigenaar='".$_SESSION['naam']."' AND kamerused=0 AND cat!=32;"; 
	$cres = mysql_query($csql); 
	if (mysql_num_rows($cres) >= 1) 
	{ 
	    while ($row = mysql_fetch_array($cres)) 
	    {
		    $item_naam = halen("SELECT naam FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
		    $item_img = "hotel/images/items/".$row['cat']."/".$row['catnr']."/icon.gif";
		    $item_prijs = halen("SELECT prijs FROM prijslijst WHERE cat='".$row['cat']."' AND catnr='".$row['catnr']."';");
		    if ($item_prijs == 1)
		    {
			    $credits = "Credit";
		    }
		    else
		    {
				$credits = "Credits";
		    }   
			if ($row['forsale'] == 0)
			{
				echo "<table border='0' cellspacing='0' cellpadding='0' style='width: 564px; height:36px; padding: 4px; margin:10px 0 0 0;border-width:1px;border-color:#60A1C7;border-style:solid;line-height:6px;'>";
				echo "<tr>";
				echo "<td width='15%' rowspan='3'><font face='Verdana' size='1'><img src='".$item_img."'></font></td>";
				echo "<td width='70%' colspan='2'><font face='Verdana' size='1' color='#60A1C7'><b>".$item_naam."</b></font></td>";
				echo "<td width='15%' rowspan='3' align='right'><font face='Verdana' size='1'>";
				echo "<a href='?sell=".$row['nr']."'><img src='img/markt_sell.gif' border='0'></a>";
				echo "</font></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td width='35%'><font face='Verdana' size='1'>Catalogusprijs:</font></td>";
				echo "<td width='35%'><font face='Verdana' size='1'>".$item_prijs." ".$credits."</font></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td width='35%'><font face='Verdana' size='1'>Eigenaar:</font></td>";
				echo "<td width='35%'><font face='Verdana' size='1'>thomas</font></td>";
				echo "</tr>";
				echo "</table>";
    		}
    		else
    		{
	    		echo "<table border='0' cellspacing='0' cellpadding='0' style='width: 564px; height:36px; padding: 4px; margin:10px 0 0 0;border-width:1px;border-color:#60A1C7;border-style:solid;line-height:6px;'>";
				echo "<tr>";
				echo "<td width='15%' rowspan='3'><font face='Verdana' size='1'><img src='".$item_img."'></font></td>";
				echo "<td width='70%' colspan='2'><font face='Verdana' size='1' color='#60A1C7'><b>".$item_naam."</b> <b><i><font color='blue'>(in aanbieding)</i></b></font></font></td>";
				echo "<td width='15%' rowspan='3' align='right'><font face='Verdana' size='1'>";
				echo "<a href='?stopsell=".$row['nr']."'><img src='img/markt_del.gif' border='0'></a><br>";
				echo "<a href='?sell=".$row['nr']."'><img src='img/markt_sell.gif' border='0'></a>";
				echo "</font></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td width='35%'><font face='Verdana' size='1'>Catalogusprijs:</font></td>";
				echo "<td width='35%'><font face='Verdana' size='1'>".$item_prijs." ".$credits."</font></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td width='35%'><font face='Verdana' size='1'>Eigenaar:</font></td>";
				echo "<td width='35%'><font face='Verdana' size='1'>thomas</font></td>";
				echo "</tr>";
				echo "</table>";
    		}
		}
	}
	else
	{
		echo "Je Hand is leeg. Je moet meubels eerst uit je kamer halen voordat je ze kunt verkopen!";
	}
}
else
{
	echo "Oeps! Hiervoor moet je aangemeld zijn!";
}
require_once('footer.php');
?>