<?php
if (!isset($_SESSION['ID'])) {
} else {
	if ("Anlegen" == $_POST['action']) {
		$sql = 'INSERT INTO vv (ID, TID) VALUES (NULL, "'.$_SESSION['TID'].'")';
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			die ('Datensatz konnte nicht angelegt werden: '.$db->error);
		}
		if (!$stmt->execute()) {
			die ('Datensatz konnte nicht angelegt werden: '.$stmt->error);
		}
		$sql = 'SELECT last_insert_id() AS last';
		$result = $db->query($sql);
		$nt = $result->fetch_assoc();
		$id = $nt['last'];
		echo "<h1>neuen Vermieter / Verkäufer anlegen</h1>\n";
	} else {
		echo "<h1>Vermieter / Verkäufer bearbeiten</h1>\n";
		$id = $_POST['ID'];
	}
	$sql1 = "SELECT * FROM vv WHERE ID = \"".$id."\" ";
	$result1 = $db->query($sql1);
	$nt1 = $result1->fetch_assoc();
	$sql2 = "SELECT Anrede FROM Anreden ORDER BY Anrede ASC";
	$result2 = $db->query($sql2);
	$sql6 = "SELECT ID, Vorname, Nachname FROM Team ORDER BY ID ASC";
	$result6 = $db->query($sql6);
	echo "<br />\n";
	echo "<form action=\"index.php?section=Personen&t=vv\" method=\"post\">\n";
	echo "	<input type=\"hidden\" name=\"ID\" value=\"".$id."\" />\n";
	if (isset($_POST['WID'])) {
		echo "	<input type=\"hidden\" name=\"WID\" value=\"".$_POST['WID']."\" />\n";
		echo "	<input type=\"hidden\" name=\"OTYP\" value=\"".$_POST['OTYP']."\" />\n";
	}
	if (in_array("Makler_Interessenten", $_SESSION['Rechte'])) {
		echo "	<label class=\"hinweis\">Teammitglied zuordnen</label>\n";
		echo "	<select name=\"TID\">\n";
		while ($nt6 = $result6->fetch_assoc()) {
			echo "		<option value=\"".$nt6['ID']."\"";
			if ($nt6['ID'] == $nt1['TID']) {
				echo " selected=\"selected\"";
			}
			echo ">".$nt6['Nachname']." ".$nt6['Vorname']."</option>\n";
		}
		echo "	</select>\n";
		echo "	<br />\n";
		echo "	&nbsp;<br />\n";
	} else {
		echo "	<input type=\"hidden\" name=\"TID\" value=\"".$_SESSION['TID']."\" />\n";
	}
	echo "<fieldset>\n";
	echo "	<legend>Kontaktdaten</legend>\n";
	echo "	<label>Anrede</label>\n";
	echo "	<select name=\"Anrede\">\n";
	while ($nt2 = $result2->fetch_assoc()) {
		echo "		<option value=\"".$nt2['Anrede']."\"";
		if ($nt2['Anrede'] == $nt1['Anrede']) {
			echo " selected=\"selected\"";
		}
		echo ">".$nt2['Anrede']."</option>\n";
	}
	echo "	</select>\n";
	echo "	<br />\n";
	echo "	<label>Vorname</label>\n";
	echo "	<input type=\"text\" name=\"Vorname\" value=\"".$nt1['Vorname']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Nachname</label>\n";
	echo "	<input type=\"text\" name=\"Nachname\" value=\"".$nt1['Nachname']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Adresse</label>\n";
	echo "	<input type=\"text\" name=\"Adresse\" value=\"".$nt1['Adresse']."\" />\n";
	echo "	<br />\n";
	echo "	<label>PLZ</label>\n";
	echo "	<input type=\"text\" name=\"PLZ\" value=\"".$nt1['PLZ']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Ort</label>\n";
	echo "	<input type=\"text\" name=\"Ort\" value=\"".$nt1['Ort']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Telefon</label>\n";
	echo "	<input type=\"text\" name=\"Telefon\" value=\"".$nt1['Telefon']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Fax</label>\n";
	echo "	<input type=\"text\" name=\"Fax\" value=\"".$nt1['Fax']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Mobil</label>\n";
	echo "	<input type=\"text\" name=\"Mobil\" value=\"".$nt1['Mobil']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Firma</label>\n";
	echo "	<input type=\"text\" name=\"Firma\" value=\"".$nt1['Firma']."\" />\n";
	echo "	<br />\n";
	echo "	<label>Email</label>\n";
	echo "	<input type=\"text\" name=\"Email\" value=\"".$nt1['Email']."\" />\n";
	echo "	<br />\n";
	echo "</fieldset>\n";
	echo "<fieldset>\n";
	echo "	<legend>Sonstiges</legend>\n";
	echo "	<br />\n";
	echo "	<textarea name=\"Sonstiges\">".$nt1['Sonstiges']."</textarea>\n";
	echo "	<br />\n";
	echo "</fieldset>\n";
	echo "<fieldset>\n";
	echo "	<legend>Intern</legend>\n";
	echo "	<br />\n";
	echo "	<textarea name=\"Intern\">".$nt1['Intern']."</textarea>\n";
	echo "</fieldset>\n";
	echo "	<br />&nbsp;<br />\n";
	echo "	<input type=\"submit\" name=\"action\" value=\"Speichern\" />\n";
	if ("Anlegen" == $_POST['action']) {
		echo "	<input type=\"submit\" name=\"action\" value=\"Abbrechen\" />\n";
	} else {
		echo "	<input type=\"reset\" value=\"Rücksetzen\" />\n";
	}
	echo "</form>\n";

	
}
?>