<?php
if ( $_SESSION['IsAdmin'] )
{
	if ("Anlegen" == $_POST['action']) {
		$sql = 'INSERT INTO user ( userCreateDate, userCreateUser ) VALUES ( now(), "' . $_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);
		}
		$_POST['ID'] = $stmt->insert_id;
	}
	$sql1		= 'SELECT *, DATE_FORMAT ( userCreateDate, "%d.%m.%Y" ) AS userCreateDate FROM user WHERE UserId = "'.$_POST['ID'].'"; ';
	$result1	= $db->query($sql1);
	$nt1		= $result1->fetch_assoc();
	$sql2		= 'SELECT * FROM objekte WHERE TID = "' . $_POST['ID'] . '";';
	$result2	= $db->query($sql2);
?>
<h2>Daten für Mitarbeiter <i><?php echo $nt1['userFirstname'] . " " . $nt1['userLastname'] ; ?></i> bearbeiten</h2><br />
<form action="index.php?<?php echo $_SERVER['QUERY_STRING']; ?>" method="post">
	<input type="hidden" name="userDeact" value="N" />
	<input type="hidden" name="userIsAdmin" value="N" />
	<input type="hidden" name="userIsBuero" value="N" />
	<input type="hidden" name="ID" value="<?php echo $_POST['ID']; ?>" />
	<label>Anrede:</label>
	<input type="text" name="userTitle" value="<?php echo ( isset ( $_POST['userTitle'] ) ) ? $_POST['userTitle'] : $nt1['userTitle']; ?>" />
	<br />
	<label>Vorname:</label>
	<input type="text" name="userFirstname" value="<?php echo ( isset ( $_POST['userFirstname'] ) ) ? $_POST['userFirstname'] : $nt1['userFirstname']; ?>" />
	<br />
	<label>Nachname:</label>
	<input type="text" name="userLastname" value="<?php echo ( isset ( $_POST['userLastname'] ) ) ? $_POST['userLastname'] : $nt1['userLastname']; ?>" />
	<br />
	<label>Email:</label>
	<input type="text" name="userEmail" value="<?php echo ( isset ( $_POST['userEmail'] ) ) ? $_POST['userEmail'] : $nt1['userEmail']; ?>" />
	<br />
	<label>Passwort:</label>
	<input type="text" name="userPassword" value="<?php echo ( isset ( $_POST['userPassword'] ) ) ? $_POST['userPassword'] : $nt1['userPassword']; ?>" />
	<br />
	<label>Telefon:</label>
	<input type="text" name="userPhone" value="<?php echo ( isset ( $_POST['userPhone'] ) ) ? $_POST['userPhone'] : $nt1['userPhone']; ?>" />
	<br />
	<label>Mobil:</label>
	<input type="text" name="userMobile" value="<?php echo ( isset ( $_POST['userMobile'] ) ) ? $_POST['userMobile'] : $nt1['userMobile']; ?>" />
	<br />
	<label>Administrator:</label>
	<input type="checkbox" name="userIsAdmin" value="Y" <?php echo ( 'Y' == $nt1['userIsAdmin'] ) ? "checked=\"checked\" " : "" ; ?> />
	<br />&nbsp;<br />
	<label>Bürokraft:</label>
	<input type="checkbox" name="userIsBuero" value="Y" <?php echo ( 'Y' == $nt1['userIsBuero'] ) ? "checked=\"checked\" " : "" ; ?> />
	<br />&nbsp;<br />
	<label>Benutzer deaktivieren:</label>
	<input type="checkbox" name="userDeact" value="Y" <?php echo ( 'Y' == $nt1['userDeact'] ) ? "checked=\"checked\" " : "" ; ?> />
	<br />&nbsp;<br />
	<label>Angelegt von:</label>
	<label><?php echo fnc_getUsernameById ( $nt1['userCreateUser'], 'logfiles' ); ?></label>
	<br />
	<label>Angelegt am:</label>
	<label><?php echo $nt1['userCreateDate'] ; ?></label>
	<br />
<?php
if ( 'Y' == $nt1['userDeact'] )
	{
?>
	<label>Deaktiviert von:</label>
	<label><?php echo fnc_getUsernameById ( $nt1['userCreateUser'], 'logfiles' ); ?></label>
	<br />
	<label>Deaktiviert am:</label>
	<label><?php echo $nt1['userCreateDate'] ; ?></label>
	<br />&nbsp;<br />
	<label>Anzahl Objekte:</label>
	<label><?php echo $result2->num_rows; ?></label>
	<br />
<?php
	}
?>
	
	&nbsp;<br />
	&nbsp;<br />
	<input type="submit" name="action" value="Speichern" />
<?php
	if ("Anlegen" == $_POST['action']) {
		echo "	<input type=\"submit\" name=\"action\" value=\"Abbrechen\" />\n";
	} else {
		echo "	<input type=\"submit\" name=\"action\" value=\"Löschen\" />\n";
		echo "	<input type=\"reset\" value=\"Rücksetzen\" />\n";
	}
	echo "</fieldset>\n";
	echo "</form>\n";

	
}
?>