<?php

if (!isset($_GET['obj'])) {
		if (isset($_POST['formaction'])) {
			if (("" == $_POST['Zvon']) AND ("" == $_POST['Zbis'])) {
			}
			elseif (("" !== $_POST['Zvon']) AND ("" == $_POST['Zbis'])) {
				$filter = $filter. "AND Zimmer >= ".$_POST['Zvon']." ";
				$zimmer = "mehr als ".$_POST['Zvon']." Zimmer";
			}
			elseif (("" == $_POST['Zvon']) AND ("" !== $_POST['Zbis'])) {
				$filter = $filter. "AND Zimmer <= ".$_POST['Zbis']." ";
				$zimmer = "bis ".$_POST['Zbis']." Zimmer";
			}
			else {
				$filter = $filter. "AND Zimmer BETWEEN ".$_POST['Zvon']." AND ".$_POST['Zbis']." ";
				$zimmer = $_POST['Zvon']." bis ".$_POST['Zbis']." Zimmer";
			}
			if (("" == $_POST['Wvon']) AND ("" == $_POST['Wbis'])) {
			}
			elseif (("" !== $_POST['Wvon']) AND ("" == $_POST['Wbis'])) {
				$filter = $filter. "AND WohnF >= ".$_POST['Wvon']." ";
				$wohn = "mehr als ".$_POST['Wvon']." m&sup2;";
			}
			elseif (("" == $_POST['Wvon']) AND ("" !== $_POST['Wbis'])) {
				$filter = $filter. "AND WohnF <= ".$_POST['Wbis']." ";
				$wohn = "bis ".$_POST['Wbis']." m&sup2;";
			}
			else {
				$filter = $filter. "AND WohnF BETWEEN ".$_POST['Wvon']." AND ".$_POST['Wbis']." ";
				$wohn = $_POST['Wvon']." bis ".$_POST['Wbis']." m&sup2;";
			}
			if (("" == $_POST['Kvon']) AND ("" == $_POST['Kbis'])) {
			}
			elseif (("" !== $_POST['Kvon']) AND ("" == $_POST['Kbis'])) {
				$filter = $filter. "AND Mkalt >= ".$_POST['Kvon']." ";
				$kalt = "mehr als ".$_POST['Kvon']." EUR";
			}
			elseif (("" == $_POST['Kvon']) AND ("" !== $_POST['Kbis'])) {
				$filter = $filter. "AND Mkalt <= ".$_POST['Kbis']." ";
				$kalt = "bis ".$_POST['Kbis']." EUR";
			}
			else {
				$filter = $filter. "AND Mkalt BETWEEN ".$_POST['Kvon']." AND ".$_POST['Kbis']." ";
				$kalt = $_POST['Kvon']." bis ".$_POST['Kbis']." EUR";
			}
		}
	else
		{
			$zimmer = '';
			$wohn = '';
			$kalt = '';
			$filter = '';
		}
	echo "<h1>Häuser zur Miete</h1>\n";
	
	$sql = 'SELECT * FROM objekte WHERE Aktiv = 1 AND Deaktiv = 0 AND O_TYP = "Haus" AND V_TYP = "Miete" '.$filter.' ORDER BY Akt_Datum DESC';
	$result = $db->query($sql);
	$anzahl = $result->num_rows;

	echo "<form class=\"suche\" action=\"index.php?section=immobilien&topic=haus_miete\" method=\"post\">\n";
	echo "<table>\n";
	echo "    <colgroup>\n";
	echo "        <col width=\"105px\" />\n";
	echo "        <col width=\"75px\" />\n";
	echo "        <col width=\"42px\" />\n";
	echo "        <col width=\"80px\" />\n";
	echo "        <col width=\"152px\" />\n";
	echo "        <col width=\"232px\" />\n";
	echo "    </colgroup>\n";
	echo "    <tr>\n";
	echo "        <th colspan=\"5\">Suchkriterien</th>\n";
	if (1 !== $anzahl) {
		echo "        <th>$anzahl Häuser im Angebot</th>\n";
	}
	else {
		echo "        <th>$anzahl Haus im Angebot</th>\n";
	}
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "        <td>Zimmer:</td>\n";
	echo "        <td><input type=\"text\" name=\"Zvon\" size=\"5\" /></td>\n";
	echo "        <td>bis</td>\n";
	echo "        <td><input type=\"text\" name=\"Zbis\" size=\"5\" /></td>\n";
	echo "        <td></td>\n";
	if (isset($_POST['formaction'])) {
		echo "        <td>mit folgenden Kriterien:</td>\n";
	}
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "        <td>Wohnfläche:</td>\n";
	echo "        <td><input type=\"text\" name=\"Wvon\" size=\"5\" /></td>\n";
	echo "        <td>bis</td>\n";
	echo "        <td><input type=\"text\" name=\"Wbis\" size=\"5\" /></td>\n";
	echo "        <td>m&sup2;</td>\n";
	echo "        <td>$zimmer</td>\n";
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "        <td>Kaltmiete:</td>\n";
	echo "        <td><input type=\"text\" name=\"Kvon\" size=\"5\" /></td>\n";
	echo "        <td>bis</td>\n";
	echo "        <td><input type=\"text\" name=\"Kbis\" size=\"5\" /></td>\n";
	echo "        <td>EUR</td>\n";
	echo "        <td>$wohn</td>\n";
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "        <td colspan=\"5\"><input type=\"submit\" name=\"formaction\" value=\"Suchen\" /></td>\n";
	echo "        <td>$kalt</td>\n";
	echo "    </tr>\n";
	echo "</table>\n";
	echo "<br />\n";
	echo "</form>\n";
	echo "<br />\n";

	echo "<table class=\"overview\">\n";
	echo "	<colgroup>\n";
	echo "		<col width=\"125\" />\n";
	echo "		<col span=\"5\" width=\"117\" />\n";
	echo "	</colgroup>\n";
	echo "	<tr>\n";
	echo "		<th>&nbsp;</th>\n";
	echo "		<th>Zimmer</th>\n";
	echo "		<th>Wohnfläche</th>\n";
	echo "		<th>Kaltmiete</th>\n";
	echo "		<th colspan=\"2\">Ausstattung</th>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"6\"><hr class=\"noDisplay\" /> &nbsp;</td>\n";
	echo "	<tr>\n";
	while ($nt = $result->fetch_assoc()) {
		unset ( $spezial );
		if ( '1' == $nt['Balkon'] )
			{
				$spezial[] = "<td><img src=\"img/balkon.png\" alt=\"Balkon\" /></td>\n";
			}
		if ( '1' == $nt['Terrasse'] )
			{
				$spezial[] = "<td><img src=\"img/terrasse.png\" alt=\"Terrasse\" /></td>\n";
			}
		if ( 'keineAngabe' != $nt['EnerTyp'] )
			{
				$spezial[] = "<td><img src=\"img/energie.png\" alt=\"Energieausweis\" /></td>\n";
			}
		if ( '1' == $nt['Garten'] )
			{
				$spezial[] = "<td><img src=\"img/garten.png\" alt=\"Garten\" /></td>\n";
			}
		if ( '1' == $nt['Garage'] )
			{
				$spezial[] = "<td><img src=\"img/garage.png\" alt=\"Garage\" /></td>\n";
			}
		if ( '1' == $nt['EinbauK'] )
			{
				$spezial[] = "<td><img src=\"img/ebk.png\" alt=\"Einbauküche\" /></td>\n";
			}
		if ( ! isset ( $spezial ) )
			{
				$spezial[] = "<td>&nbsp;</td>\n";
			}
		while ( count( $spezial ) < 4 )
			{
				$spezial[] = "<td>&nbsp;</td>\n";
			}
		echo "<form action=\"index.php?".$_SERVER['QUERY_STRING']."&obj=".$nt['ID']."\" method=\"post\">\n";
		echo "<input type=\"hidden\" name=\"ID\" value=\"".$nt['ID']."\" />\n";
		echo "<input type=\"hidden\" name=\"Count\" value=\"".$nt['Count']."\" />\n";
		echo "    <tr>\n";
		echo "        <td rowspan=\"4\"><input type=\"image\" value=\"Zum Objekt\" src=\"img/objekte/".$nt['ID']."_1.jpg\" width=\"80px\" height=\"60px\" /></td>\n";
		echo "        <td colspan=\"5\"><input class=\"overview\" type=\"submit\" value=\"".$nt['Kurz']."\" /></td>\n";
		echo "    </tr>\n";
		echo "    <tr>\n";
		echo "    </tr>\n";
		echo "    <tr>\n";
		echo "        <td>" . $nt['Zimmer'] . "</td>\n";
		echo "        <td>" . $nt['WohnF'] . " m&sup2;</td>\n";
		echo "        <td>" . number_format ( $nt['Mkalt'], 0, ',', '.' ) . " &euro;</td>\n";
		echo $spezial[0];
		echo $spezial[1];
		echo "    </tr>\n";
		echo "    <tr>\n";
		echo "        <td colspan=\"3\">" . $nt['Ort'] . "</td>\n";
		echo $spezial[2];
		echo $spezial[3];
		echo "    </tr>\n";
		echo "    <tr>\n";
		echo "        <td colspan=\"6\"><hr /></td>\n";
		echo "    </tr>\n";
		echo "</form>\n";
	}
	echo "</table>\n";
} else {
	if (isset($_POST['ID'])) {
		$Zaehler =	(int)($_POST['Count']) + 1;
		$sqlu =		"UPDATE objekte SET Count = \"".$Zaehler."\" WHERE ID = \"".$_POST['ID']."\";";
		$stmtu = $db->prepare($sqlu);
		if ($stmtu) {
			if ($stmtu->execute()) {
			}
		}
		unset($_POST);
	}
	include "inhalte/wohn/hmobj.php";
}

?>