<?php
require_once 'includes/auth_check.php';
require_once 'includes/db_connect.php';
$user_id = $_SESSION['user_id'];
$zones = []; 
$containers = [];
$sql_zones = "SELECT id, name FROM zones WHERE user_id = ? ORDER BY name ASC";
if ($stmt_zones = $mysqli->prepare($sql_zones)) { 
    $stmt_zones->bind_param("i", $user_id); 
    $stmt_zones->execute(); 
    $result_zones = $stmt_zones->get_result(); 
    while ($row = $result_zones->fetch_assoc()) { $zones[] = $row; } 
    $stmt_zones->close(); 
}
$sql_containers = "SELECT c.id, c.name, z.name AS zone_name, c.zone_id FROM containers c JOIN zones z ON c.zone_id = z.id WHERE c.user_id = ? ORDER BY c.name ASC";
if ($stmt_containers = $mysqli->prepare($sql_containers)) { 
    $stmt_containers->bind_param("i", $user_id); 
    $stmt_containers->execute(); 
    $result_containers = $stmt_containers->get_result(); 
    while ($row = $result_containers->fetch_assoc()) { $containers[] = $row; } 
    $stmt_containers->close(); 
}
require_once 'includes/header.php';
?>
<div class="card header-card mb-4">
    <div class="card-body">
        <h1 class="mb-0">Inventar-Verwaltung</h1>
        <p class="card-text text-white-50 mt-2">Verwalten Sie hier Ihre Zonen und Pflanzgefäße.</p>
    </div>
</div>
<div class="row">
    <div class="col-lg-6 mb-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h3 class="text-white mb-0">Zonen</h3>
            <button class="btn btn-sm btn-cazubu" data-bs-toggle="modal" data-bs-target="#zoneModal">+ Neue Zone</button>
        </div>
        <table class="table table-hover cazubu-table-frameless">
            <thead class="table-dark">
                <tr>
                    <th>Name</th>
                    <th style="width: 120px;">Aktionen</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($zones)) echo '<tr><td colspan="2" class="text-center p-4"><i>Keine Zonen angelegt.</i></td></tr>'; ?>
                <?php foreach ($zones as $zone): ?>
                <tr>
                    <td class="align-middle"><?php echo htmlspecialchars($zone['name']); ?></td>
                    <td>
                        <button class="btn btn-sm btn-outline-primary edit-zone-btn" data-bs-toggle="modal" data-bs-target="#zoneModal" data-id="<?php echo $zone['id']; ?>" data-name="<?php echo htmlspecialchars($zone['name']); ?>"><i class="bi bi-pencil-fill"></i></button>
                        <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" data-bs-target="#deleteConfirmModal" data-id="<?php echo $zone['id']; ?>" data-name="<?php echo htmlspecialchars($zone['name']); ?>" data-type="zone"><i class="bi bi-trash-fill"></i></button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="col-lg-6 mb-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h3 class="text-white mb-0">Pflanzgefäße</h3>
            <button class="btn btn-sm btn-cazubu" data-bs-toggle="modal" data-bs-target="#containerModal">+ Neues Pflanzgefäß</button>
        </div>
        <table class="table table-hover cazubu-table-frameless">
            <thead class="table-dark">
                <tr>
                    <th>Name</th>
                    <th>Zone</th>
                    <th style="width: 120px;">Aktionen</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($containers)) echo '<tr><td colspan="3" class="text-center p-4"><i>Keine Pflanzgefäße angelegt.</i></td></tr>'; ?>
                <?php foreach ($containers as $container): ?>
                <tr>
                    <td class="align-middle"><?php echo htmlspecialchars($container['name']); ?></td>
                    <td class="align-middle"><span class="badge text-bg-dark"><?php echo htmlspecialchars($container['zone_name']); ?></span></td>
                    <td>
                        <button class="btn btn-sm btn-outline-primary edit-container-btn" data-bs-toggle="modal" data-bs-target="#containerModal" data-id="<?php echo $container['id']; ?>" data-name="<?php echo htmlspecialchars($container['name']); ?>" data-zoneid="<?php echo $container['zone_id']; ?>">✏️</button>
                        <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" data-bs-target="#deleteConfirmModal" data-id="<?php echo $container['id']; ?>" data-name="<?php echo htmlspecialchars($container['name']); ?>" data-type="container">🗑️</button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<div class="modal fade" id="zoneModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="zoneModalLabel">Zone</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><form id="zone-form"><input type="hidden" name="action" id="zone-form-action"><input type="hidden" name="id" id="zone-id"><div class="mb-3"><label for="zone-name" class="form-label">Name der Zone</label><input type="text" class="form-control" id="zone-name" name="name" required></div></form></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Schließen</button><button type="submit" class="btn btn-primary" form="zone-form">Speichern</button></div></div></div></div>
<div class="modal fade" id="containerModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="containerModalLabel">Pflanzgefäß</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><form id="container-form"><input type="hidden" name="action" id="container-form-action"><input type="hidden" name="id" id="container-id"><div class="mb-3"><label class="form-label">Name des Pflanzgefäßes</label><input type="text" class="form-control" name="name" required></div><div class="mb-3"><label class="form-label">Zugehörige Zone</label><select class="form-select" name="zone_id" required><option value="">Bitte wählen...</option><?php foreach ($zones as $zone) { echo "<option value='{$zone['id']}'>" . htmlspecialchars($zone['name']) . "</option>"; } ?></select></div></form></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Schließen</button><button type="submit" class="btn btn-primary" form="container-form">Speichern</button></div></div></div></div>
<div class="modal fade" id="deleteConfirmModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Löschen bestätigen</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><p>Sind Sie sicher, dass Sie <strong id="item-type-to-delete"></strong> "<strong id="item-name-to-delete"></strong>" endgültig löschen möchten?</p><p class="text-danger" id="delete-warning"></p></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button><button type="button" class="btn btn-danger" id="confirmDeleteBtn">Ja, endgültig löschen</button></div></div></div></div>
<?php require_once 'includes/footer.php'; ?>
