<?php
if (!in_array("Rechte", $_SESSION['Rechte'])) {
	exit(ZUGRIFF_ERROR);
}
if (!isset($_SESSION['ID'])) {
} else {
	$sql0 =		"SELECT Vorname, Nachname FROM Team WHERE ID = \"".$_POST['ID']."\";";
	$result0 =	$db->query($sql0);
	$nt0 =		$result0->fetch_assoc();
	$sql1 =		"SELECT Count, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum, Datum AS Tmp_Date, Akt_Datum AS Tmp_Akt FROM admins WHERE ID = \"".$_POST['ID']."\";";
	$result1 =	$db->query($sql1);
	$sql2 =		"SELECT count(*) AS Anz FROM objekte;";
	$result2 =	$db->query($sql2);
	$nt2 =		$result2->fetch_assoc();
	$sql3 =		"SELECT count(*) AS Anz FROM objekte WHERE TID = \"".$_POST['ID']."\";";
	$result3 =	$db->query($sql3);
	$nt3 =		$result3->fetch_assoc();
	$sql4 =		"SELECT ID, KAT, O_TYP, V_TYP, Kurz, Art, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum, Count FROM objekte WHERE Aktiv = \"1\" AND TID = \"".$_POST['ID']."\" ORDER BY KAT DESC, O_TYP DESC, V_TYP DESC, ID ASC;";
	$result4 =	$db->query($sql4);
	$sql5 =		"SELECT ID, KAT, O_TYP, V_TYP, Kurz, Art, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum, Count FROM objekte WHERE Aktiv = \"0\" AND TID = \"".$_POST['ID']."\" ORDER BY KAT DESC, O_TYP DESC, V_TYP DESC, ID ASC;";
	$result5 =	$db->query($sql5);
	$sql6 =		"SELECT count(*) AS Anz FROM inter;";
	$result6 =	$db->query($sql6);
	$nt6 =		$result6->fetch_assoc();
	$sql7 =		"SELECT count(*) AS Anz FROM inter WHERE TID = \"".$_POST['ID']."\";";
	$result7 =	$db->query($sql7);
	$nt7 =		$result7->fetch_assoc();
	$sql8 =		"SELECT count(*) AS Anz FROM vv;";
	$result8 =	$db->query($sql8);
	$nt8 =		$result8->fetch_assoc();
	$sql9 =		"SELECT count(*) AS Anz FROM vv WHERE TID = \"".$_POST['ID']."\";";
	$result9 =	$db->query($sql9);
	$nt9 =		$result9->fetch_assoc();
	$sql10 =	"SELECT ID, Vorname, Nachname, O_TYP, V_TYP, ORTneu, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum, Count FROM inter WHERE Aktiv = \"1\" AND TID = \"".$_POST['ID']."\" ORDER BY O_TYP DESC, V_TYP DESC, Nachname ASC, Vorname ASC;";
	$result10 =	$db->query($sql10);
	$sql11 =	"SELECT ID, Vorname, Nachname, O_TYP, V_TYP, ORTneu, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum, Count FROM inter WHERE Aktiv = \"0\" AND TID = \"".$_POST['ID']."\" ORDER BY O_TYP DESC, V_TYP DESC, Nachname ASC, Vorname ASC;";
	$result11 =	$db->query($sql11);
	$sql12 =	"SELECT ID, Vorname, Nachname, DATE_FORMAT(Datum, \"%d.%m.%Y\") AS Datum, DATE_FORMAT(Akt_Datum, \"%d.%m.%Y\") AS Akt_Datum FROM vv WHERE TID = \"".$_POST['ID']."\" ORDER BY ID ASC;";
	$result12 =	$db->query($sql12);
	
	echo "<h2>".$nt0['Vorname']." ".$nt0['Nachname']."</h2>\n";
	if ($nt1 = $result1->fetch_assoc()) {
		$Tage = (int)((time() - strtotime($nt1['Tmp_Date'])) / 86400);
		echo "<label>Zugang eingerichtet:</label>\n";
		echo "<label class=\"links\">".$nt1['Datum']."</label>\n";
		echo "<label>Letzte Anmeldung:</label>\n";
		echo "<label class=\"rechts\">".$nt1['Akt_Datum']."</label>\n";
		echo "<br />\n";
		echo "<p>".$nt1['Count']." Anmeldungen in ".$Tage." Tagen</p>\n";
		echo "<br />\n";
	} else {
		echo "<p>Der Makler hat keinen Zugang zum Administrationsbereich</p>\n";
	}
	echo "<h2>Dem Makler zugeordnet: ";
	echo "<span style=\"float:right;\">";
	echo $nt3['Anz']." von ".$nt2['Anz']." Immobilien<br />";
	echo $nt7['Anz']." von ".$nt6['Anz']." Interessenten<br />";
	echo $nt9['Anz']." von ".$nt8['Anz']." Vermieter / Verkäufer<br />";
	echo "</span>";
	echo "</h2>\n";
	echo "<br class=\"fix\" />\n";
	echo "<table>\n";
	echo "	<colgroup>\n";
	echo "		<col width=\"70px\" />\n";
	echo "		<col width=\"120px\" />\n";
	echo "		<col width=\"30px\" />\n";
	echo "		<col width=\"220px\" />\n";
	echo "		<col width=\"120px\" />\n";
	echo "		<col span=\"2\" width=\"80px\" />\n";
	echo "		<col width=\"40px\" />\n";
	echo "	</colgroup>\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"8\">";
	echo "<h3>Aktive Immobilien</h3>";
	echo "</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td>Kategorie</td>\n";
	echo "		<td>Bereich</td>\n";
	echo "		<td>ID</td>\n";
	echo "		<td>Überschrift</td>\n";
	echo "		<td>Art</td>\n";
	echo "		<td>Erstellt</td>\n";
	echo "		<td>Aktualisiert</td>\n";
	echo "		<td>Zugriffe</td>\n";
	echo "	</tr>\n";
	while ($nt4 = $result4->fetch_assoc()) {
		if ($tmp_kat != $nt4['KAT']) {
			$tmp_kat = $nt4['KAT'];
			echo "	<tr style=\"border-top: 1px solid black;\" >\n";
			if ("wohnen" == $tmp_kat) {
				echo "<td>Wohnen</td>\n";
			} elseif ("gewerbe" == $tmp_kat) {
				echo "<td>Gewerbe</td>\n";
			} elseif ("andere" == $tmp_kat) {
				echo "<td>Sonstige</td>\n";
			}
		} else {
			echo "	<tr>\n";
			echo "<td>&nbsp;</td>\n";
		}
		if (($tmp_otyp != $nt4['O_TYP']) or ($tmp_vtyp != $nt4['V_TYP'])) {
			$tmp_otyp = $nt4['O_TYP'];
			$tmp_vtyp = $nt4['V_TYP'];
			echo "<td>".$tmp_otyp." - ".$tmp_vtyp."</td>\n";
		} else {
			echo "<td>&nbsp;</td>\n";
		}
		echo "		<td>".$nt4['ID']."</td>\n";
		echo "		<td>".substr($nt4['Kurz'],0 , 30)."</td>\n";
		echo "		<td>".$nt4['Art']."</td>\n";
		echo "		<td>".$nt4['Datum']."</td>\n";
		echo "		<td>".$nt4['Akt_Datum']."</td>\n";
		echo "		<td>".$nt4['Count']."</td>\n";
		echo "	</tr>\n";
	}
	$tmp_otyp = "";
	$tmp_vtyp = "";

	echo "	<tr>\n";
	echo "		<td colspan=\"8\">";
	echo "<h3>Inaktive Immobilien</h3>\n";
	echo "		</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td>Kategorie</td>\n";
	echo "		<td>Bereich</td>\n";
	echo "		<td>ID</td>\n";
	echo "		<td>Überschrift</td>\n";
	echo "		<td>Art</td>\n";
	echo "		<td>Erstellt</td>\n";
	echo "		<td>Aktualisiert</td>\n";
	echo "		<td>Zugriffe</td>\n";
	echo "	</tr>\n";
	while ($nt5 = $result5->fetch_assoc()) {
		if ($tmp_kat != $nt5['KAT']) {
			$tmp_kat = $nt5['KAT'];
			echo "	<tr style=\"border-top: 1px solid black;\" >\n";
			if ("wohnen" == $tmp_kat) {
				echo "<td>Wohnen</td>\n";
			} elseif ("gewerbe" == $tmp_kat) {
				echo "<td>Gewerbe</td>\n";
			} elseif ("andere" == $tmp_kat) {
				echo "<td>Sonstige</td>\n";
			}
		} else {
		echo "	<tr>\n";
			echo "<td>&nbsp;</td>\n";
		}
		if (($tmp_otyp != $nt5['O_TYP']) or ($tmp_vtyp != $nt5['V_TYP'])) {
			$tmp_otyp = $nt5['O_TYP'];
			$tmp_vtyp = $nt5['V_TYP'];
			echo "<td>".$tmp_otyp." - ".$tmp_vtyp."</td>\n";
		} else {
			echo "<td>&nbsp;</td>\n";
		}
		echo "		<td>".$nt5['ID']."</td>\n";
		echo "		<td>".substr($nt5['Kurz'],0 , 30)."</td>\n";
		echo "		<td>".$nt5['Art']."</td>\n";
		echo "		<td>".$nt5['Datum']."</td>\n";
		echo "		<td>".$nt5['Akt_Datum']."</td>\n";
		echo "		<td>".$nt5['Count']."</td>\n";
		echo "	</tr>\n";
	}
	$tmp_otyp = "";
	$tmp_vtyp = "";

	echo "	<tr>\n";
	echo "		<td colspan=\"8\">";
	echo "<h3>Aktive Interessenten</h3>";
	echo "</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"2\">Bereich</td>\n";
	echo "		<td>ID</td>\n";
	echo "		<td>Nachname, Vorname</td>\n";
	echo "		<td>Ort</td>\n";
	echo "		<td>Erstellt</td>\n";
	echo "		<td>Aktualisiert</td>\n";
	echo "		<td>Kontakte</td>\n";
	echo "	</tr>\n";
	while ($nt10 = $result10->fetch_assoc()) {
		if (($tmp_otyp != $nt10['O_TYP']) or ($tmp_vtyp != $nt10['V_TYP'])) {
			$tmp_otyp = $nt10['O_TYP'];
			$tmp_vtyp = $nt10['V_TYP'];
			echo "	<tr style=\"border-top: 1px solid black;\" >\n";
			echo "<td colspan=\"2\">".$tmp_otyp." - ".$tmp_vtyp."</td>\n";
		} else {
			echo "	<tr>\n";
			echo "<td colspan=\"2\">&nbsp;</td>\n";
		}
		echo "		<td>".$nt10['ID']."</td>\n";
		echo "		<td>".$nt10['Nachname'].", ".$nt10['Vorname']."</td>\n";
		echo "		<td>".$nt10['ORTneu']."</td>\n";
		echo "		<td>".$nt10['Datum']."</td>\n";
		echo "		<td>".$nt10['Akt_Datum']."</td>\n";
		echo "		<td>".$nt10['Count']."</td>\n";
		echo "	</tr>\n";
	}
	$tmp_otyp = "";
	$tmp_vtyp = "";

	echo "	<tr>\n";
	echo "		<td colspan=\"8\">";
	echo "<h3>Inaktive Interessenten</h3>";
	echo "</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"2\">Bereich</td>\n";
	echo "		<td>ID</td>\n";
	echo "		<td>Nachname, Vorname</td>\n";
	echo "		<td>Ort</td>\n";
	echo "		<td>Erstellt</td>\n";
	echo "		<td>Aktualisiert</td>\n";
	echo "		<td>Kontakte</td>\n";
	echo "	</tr>\n";
	while ($nt11 = $result11->fetch_assoc()) {
		if (($tmp_otyp != $nt11['O_TYP']) or ($tmp_vtyp != $nt11['V_TYP'])) {
			$tmp_otyp = $nt11['O_TYP'];
			$tmp_vtyp = $nt11['V_TYP'];
			echo "	<tr style=\"border-top: 1px solid black;\" >\n";
			echo "<td colspan=\"2\">".$tmp_otyp." - ".$tmp_vtyp."</td>\n";
		} else {
			echo "	<tr>\n";
			echo "<td colspan=\"2\">&nbsp;</td>\n";
		}
		echo "		<td>".$nt11['ID']."</td>\n";
		echo "		<td>".$nt11['Nachname'].", ".$nt11['Vorname']."</td>\n";
		echo "		<td>".$nt11['ORTneu']."</td>\n";
		echo "		<td>".$nt11['Datum']."</td>\n";
		echo "		<td>".$nt11['Akt_Datum']."</td>\n";
		echo "		<td>".$nt11['Count']."</td>\n";
		echo "	</tr>\n";
	}
	$tmp_otyp = "";
	$tmp_vtyp = "";

	echo "	<tr>\n";
	echo "		<td colspan=\"8\">";
	echo "<h3>Vermieter / Verkäufer</h3>";
	echo "</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td>ID</td>\n";
	echo "		<td colspan=\"4\">Nachname, Vorname</td>\n";
	echo "		<td>Erstellt</td>\n";
	echo "		<td>Aktualisiert</td>\n";
	echo "		<td>Objekte</td>\n";
	echo "	</tr>\n";
	echo "	<tr style=\"border-top: 1px solid black;\" >\n";
	$tmp = 0;
	while ($nt12 = $result12->fetch_assoc()) {
		$sqlob =	"SELECT Count(*) AS Anz FROM objekte WHERE VVID = \"".$nt12['ID']."\";";
		$resultob =	$db->query($sqlob);
		$ntob =		$resultob->fetch_assoc();
		if ($tmp != 0) {
			echo "	<tr>\n";
		} else {
			$tmp = 1;
		}
		echo "		<td>".$nt12['ID']."</td>\n";
		echo "		<td colspan=\"4\">".$nt12['Nachname'].", ".$nt12['Vorname']."</td>\n";
		echo "		<td>".$nt12['Datum']."</td>\n";
		echo "		<td>".$nt12['Akt_Datum']."</td>\n";
		echo "		<td>".$ntob['Anz']."</td>\n";
		echo "	</tr>\n";
	}

	echo "</table>\n";
}
?>