<?php
require_once 'includes/auth_check.php';
require_once 'includes/db_connect.php';
$user_id = $_SESSION['user_id'];
$status_filter = $_GET['status'] ?? 'Eingepflanzt';
$allowed_stati = ['Eingepflanzt', 'Trocknend', 'Geerntet'];
if (!in_array($status_filter, $allowed_stati)) { $status_filter = 'Eingepflanzt'; }
$counts = [];
$count_sql = "SELECT status, COUNT(*) as count FROM plants WHERE user_id = ? GROUP BY status";
if ($stmt_count = $mysqli->prepare($count_sql)) { $stmt_count->bind_param("i", $user_id); $stmt_count->execute(); $result_count = $stmt_count->get_result(); while($row = $result_count->fetch_assoc()) { $counts[$row['status']] = $row['count']; } $stmt_count->close(); }
$plants = [];
$sql = "SELECT 
            p.id, p.plant_date, p.phase, 
            (SELECT file_path FROM plant_images WHERE plant_id = p.id ORDER BY uploaded_at DESC LIMIT 1) AS latest_image_path, 
            s.strain_name, s.internal_name, 
            z.name AS zone_name, 
            c.name AS container_name,
            (SELECT value FROM sensor_data WHERE plant_id = p.id AND sensor_type = 'Temperatur' ORDER BY timestamp DESC LIMIT 1) AS current_temp,
            (SELECT value FROM sensor_data WHERE plant_id = p.id AND sensor_type = 'Feuchtigkeit' ORDER BY timestamp DESC LIMIT 1) AS current_humidity
        FROM plants p 
        LEFT JOIN seeds s ON p.seed_id = s.id 
        LEFT JOIN zones z ON p.zone_id = z.id 
        LEFT JOIN containers c ON p.container_id = c.id 
        WHERE p.user_id = ? AND p.status = ? 
        ORDER BY p.plant_date DESC";
if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("is", $user_id, $status_filter); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $plants[] = $row; } $stmt->close(); }
$zones = []; $seeds = [];
$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_seeds = "SELECT id, strain_name, internal_name, stock_count FROM seeds WHERE user_id = ? AND stock_count > 0 ORDER BY strain_name ASC";
if ($stmt_seeds = $mysqli->prepare($sql_seeds)) { $stmt_seeds->bind_param("i", $user_id); $stmt_seeds->execute(); $result_seeds = $stmt_seeds->get_result(); while ($row = $result_seeds->fetch_assoc()) { $seeds[] = $row; } $stmt_seeds->close(); }
require_once 'includes/header.php';
?>
<div class="card header-card mb-4">
    <div class="card-body d-flex justify-content-between align-items-center">
        <div><h1 class="mb-0">Pflanzen-Übersicht</h1><p class="card-text mt-2">Übersicht über alle deine Pflanzen.</p></div>
        <button class="btn btn-cazubu" data-bs-toggle="modal" data-bs-target="#plantModal">+ Neue Pflanze anlegen</button>
    </div>
</div>
<ul class="nav nav-pills plant-tabs">
    <li class="nav-item"><a class="nav-link <?php if($status_filter == 'Eingepflanzt') echo 'active'; ?>" href="?status=Eingepflanzt">Wachsend (<?php echo $counts['Eingepflanzt'] ?? 0; ?>)</a></li>
    <li class="nav-item"><a class="nav-link <?php if($status_filter == 'Trocknend') echo 'active'; ?>" href="?status=Trocknend">Trocknend (<?php echo $counts['Trocknend'] ?? 0; ?>)</a></li>
    <li class="nav-item"><a class="nav-link <?php if($status_filter == 'Geerntet') echo 'active'; ?>" href="?status=Geerntet">Geerntet (<?php echo $counts['Geerntet'] ?? 0; ?>)</a></li>
</ul>
<table id="plants-table" class="table table-hover cazubu-table-frameless">
    <thead>
        <tr>
            <th style="width: 8%;" class="no-sort">Foto</th>
            <th style="width: 25%;">Sorte / Interne Bez.</th>
            <th style="width: 20%;">Zone / Gefäß</th>
            <th style="width: 12%;">Phase</th>
            <th style="width: 8%;">Alter</th>
            <th style="width: 8%;">Temp.</th>
            <th style="width: 9%;">Feucht.</th>
            <th style="width: 10%;" class="no-sort">Aktionen</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($plants as $plant): ?>
        <tr>
            <td><img src="<?php echo !empty($plant['latest_image_path']) ? htmlspecialchars($plant['latest_image_path']) : 'assets/dummy_plant.png'; ?>" alt="Pflanzenfoto" class="img-fluid rounded" style="width: 60px; height: 60px; object-fit: cover;" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-html="true" data-bs-placement="right" data-bs-content="<img src='<?php echo !empty($plant['latest_image_path']) ? htmlspecialchars($plant['latest_image_path']) : 'assets/dummy_plant.png'; ?>' class='img-fluid' style='max-width: 200px;'>"></td>
            <td class="align-middle"><strong><?php echo htmlspecialchars($plant['strain_name']); ?></strong><small class="d-block text-muted"><?php if (!empty($plant['internal_name'])) echo htmlspecialchars($plant['internal_name']); ?></small></td>
            <td class="align-middle"><strong class="d-block"><?php echo htmlspecialchars($plant['container_name'] ?? 'N/A'); ?></strong><small class="text-muted"><?php echo htmlspecialchars($plant['zone_name'] ?? 'N/A'); ?></small></td>
            <td class="align-middle"><?php $phase = $plant['phase']; $badge_class = 'text-bg-secondary'; if ($phase == 'Keimend') $badge_class = 'text-bg-light'; elseif ($phase == 'Setzling') $badge_class = 'text-bg-success'; elseif ($phase == 'Wachstum') $phase_badge_class = 'text-bg-primary'; elseif ($phase == 'Blütenphase') $badge_class = 'text-bg-warning'; elseif ($phase == 'Ernte' || $phase == 'Getrocknet') $badge_class = 'text-bg-dark'; echo "<span class='badge {$badge_class}'>".htmlspecialchars($phase)."</span>"; ?></td>
            <td class="align-middle"><?php echo (new DateTime())->diff(new DateTime($plant['plant_date']))->days; ?> Tage</td>
            <td class="align-middle"><?php echo $plant['current_temp'] ? number_format($plant['current_temp'], 1) . '°C' : '-'; ?></td>
            <td class="align-middle"><?php echo $plant['current_humidity'] ? number_format($plant['current_humidity'], 1) . '%' : '-'; ?></td>
            <td class="align-middle text-center"><a href="plant_detail.php?id=<?php echo $plant['id']; ?>" class="btn btn-sm btn-outline-dark"><i class="bi bi-search"></i> Details</a></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<div class="modal fade" id="plantModal" tabindex="-1"><div class="modal-dialog modal-lg"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Neue Pflanze anlegen</h5><button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button></div><div class="modal-body"><form id="plant-form"><input type="hidden" name="action" value="add_plant"><div class="row"><div class="col-md-6 mb-3"><label class="form-label">Zone</label><select class="form-select" name="zone_id" id="zone-select" required><option value="">1. Zone auswählen</option><?php foreach ($zones as $zone) { echo "<option value='{$zone['id']}'>" . htmlspecialchars($zone['name']) . "</option>"; } ?></select></div><div class="col-md-6 mb-3"><label class="form-label">Pflanzgefäß</label><select class="form-select" name="container_id" id="container-select" required disabled><option value="">2. Zuerst Zone wählen</option></select></div></div><div class="mb-3"><label class="form-label">Samen</label><select class="form-select" name="seed_id" required><option value="">3. Samen auswählen</option><?php foreach ($seeds as $seed) { echo "<option value='{$seed['id']}'>" . htmlspecialchars($seed['strain_name']) . " (" . htmlspecialchars($seed['internal_name']) . ") [" . $seed['stock_count'] . " Stk.]</option>"; } ?></select></div><div class="form-check mb-3"><input class="form-check-input" type="checkbox" name="reduce_seed_stock" value="1" id="reduce_seed_stock" checked><label class="form-check-label" for="reduce_seed_stock">Samenanzahl im Inventar automatisch um 1 reduzieren</label></div><div class="mb-3"><label class="form-label">Datum der Pflanzung</label><input type="date" class="form-control" name="plant_date" value="<?php echo date('Y-m-d'); ?>" 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-cazubu" form="plant-form">Speichern</button></div></div></div></div>
<?php require_once 'includes/footer.php'; ?>
