Files
netwatch/app/modules/buildings/edit.php
2026-02-16 13:56:01 +01:00

197 lines
5.0 KiB
PHP

<?php
/**
* app/modules/buildings/edit.php
*/
// =========================
// Kontext bestimmen
// =========================
$buildingId = (int)($_GET['id'] ?? 0);
$building = null;
if ($buildingId > 0) {
$building = $sql->single(
"SELECT * FROM buildings WHERE id = ?",
"i",
[$buildingId]
);
}
$isEdit = !empty($building);
$pageTitle = $isEdit ? "Gebäude bearbeiten: " . htmlspecialchars($building['name']) : "Neues Gebäude";
$prefillLocationId = (int)($_GET['location_id'] ?? 0);
// =========================
// Standorte laden
// =========================
$locations = $sql->get("SELECT id, name FROM locations ORDER BY name", "", []);
$selectedLocationId = $building['location_id'] ?? $prefillLocationId;
?>
<div class="building-edit">
<h1><?php echo $pageTitle; ?></h1>
<form method="post" action="?module=buildings&action=save" class="edit-form">
<?php if ($isEdit): ?>
<input type="hidden" name="id" value="<?php echo $buildingId; ?>">
<?php endif; ?>
<!-- =========================
Basisdaten
========================= -->
<fieldset>
<legend>Allgemein</legend>
<div class="form-group">
<label for="name">Name <span class="required">*</span></label>
<input type="text" id="name" name="name" required
value="<?php echo htmlspecialchars($building['name'] ?? '); ?>"
placeholder="z.B. Gebäude A, Verwaltungsgebäude">
</div>
<div class="form-group">
<label for="location_id">Standort <span class="required">*</span></label>
<select id="location_id" name="location_id" required>
<option value="">- Wählen -</option>
<?php foreach ($locations as $location): ?>
<option value="<?php echo $location['id']; ?>"
<?php echo ((int)$selectedLocationId === (int)$location['id']) ? 'selected' : '; ?>>
<?php echo htmlspecialchars($location['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="comment">Beschreibung</label>
<textarea id="comment" name="comment" rows="3"
placeholder="Adresse, Besonderheiten, etc."><?php echo htmlspecialchars($building['comment'] ?? '); ?></textarea>
</div>
</fieldset>
<!-- =========================
Aktionen
========================= -->
<fieldset class="form-actions">
<button type="submit" class="button button-primary">Speichern</button>
<a href="?module=buildings&action=list" class="button">Abbrechen</a>
<?php if ($isEdit): ?>
<a href="#" class="button button-danger" onclick="confirmDelete(<?php echo $buildingId; ?>)">Löschen</a>
<?php endif; ?>
</fieldset>
</form>
</div>
<style>
.building-edit {
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.edit-form {
background: white;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
}
.edit-form fieldset {
margin: 20px 0;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
}
.edit-form legend {
padding: 0 10px;
font-weight: bold;
font-size: 1.1em;
}
.form-group {
margin: 15px 0;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.form-group input[type="text"],
.form-group select,
.form-group textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: inherit;
}
.form-group textarea {
resize: vertical;
}
.required {
color: red;
}
.form-actions {
display: flex;
gap: 10px;
margin-top: 30px;
}
.button {
padding: 10px 15px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
border: none;
cursor: pointer;
font-size: 0.95em;
}
.button-primary {
background: #28a745;
}
.button-danger {
background: #dc3545;
}
.button:hover {
opacity: 0.8;
}
</style>
<script>
function confirmDelete(id) {
if (confirm('Dieses Gebaeude wirklich loeschen? Alle Stockwerke werden geloescht.')) {
fetch('?module=buildings&action=delete', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: 'id=' + encodeURIComponent(id)
})
.then((res) => res.json())
.then((data) => {
if (data && data.status === 'ok') {
window.location.href = '?module=buildings&action=list';
return;
}
alert((data && data.error) ? data.error : 'Loeschen fehlgeschlagen');
})
.catch(() => {
alert('Loeschen fehlgeschlagen');
});
}
}
</script>