<?php
require_once 'includes/auth_check.php';
require_once 'includes/db_connect.php';
$user_id = $_SESSION['user_id'];
$seeds = [];
$sql = "SELECT * FROM seeds WHERE user_id = ? ORDER BY strain_name ASC";
if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $seeds[] = $row; } $stmt->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">Samen-Verwaltung</h1><p class="card-text text-white-50 mt-2">Verwalte hier deine gesamte Sammlung an Cannabis-Samen.</p></div>
        <button class="btn btn-cazubu" data-bs-toggle="modal" data-bs-target="#seedModal">+ Neuer Samen</button>
    </div>
</div>

<table id="seeds-table" class="table table-hover cazubu-table-frameless">
    <thead class="table-dark">
        <tr>
            <th>Sortenname</th>
            <th>Interne Bez.</th>
            <th style="width: 25%;">Genetik</th>
            <th>Typ</th>
            <th>Anzahl</th>
            <th style="width: 120px;" class="no-sort">Aktionen</th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($seeds)): ?><tr><td colspan="6" class="text-center p-4"><i>Keine Samen angelegt.</i></td></tr><?php endif; ?>
        <?php foreach ($seeds as $seed): ?>
        <tr>
            <td class="align-middle">
                <span class="fw-bold"><?php echo htmlspecialchars($seed['strain_name']); ?></span>
                <?php if (!empty($seed['description'])): ?><span class="notes-icon" data-bs-toggle="tooltip" data-bs-placement="right" title="<?php echo htmlspecialchars($seed['description']); ?>"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chat-left-text-fill" viewBox="0 0 16 16"><path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H4.414a1 1 0 0 0-.707.293L.854 15.146A.5.5 0 0 1 0 14.793V2zm3.5 1a.5.5 0 0 0 0 1h9a.5.5 0 0 0 0-1h-9zm0 2.5a.5.5 0 0 0 0 1h9a.5.5 0 0 0 0-1h-9zm0 2.5a.5.5 0 0 0 0 1h5a.5.5 0 0 0 0-1h-5z"/></svg></span><?php endif; ?>
                <?php if (!empty($seed['info_url'])): ?><a href="<?php echo htmlspecialchars($seed['info_url']); ?>" target="_blank" title="Weitere Infos" class="info-link">🔗</a><?php endif; ?>
            </td>
            <td class="align-middle"><?php echo htmlspecialchars($seed['internal_name']); ?></td>
            <td class="align-middle">
                <div class="d-flex justify-content-between" style="font-size: 0.8em; color: #6c757d;">
                    <span>Sativa</span>
                    <span>Indica</span>
                </div>
                <div class="progress" role="progressbar" aria-label="Sativa/Indica ratio" style="height: 18px; font-size: .75rem;">
                    <div class="progress-bar text-bg-warning" style="width: <?php echo $seed['ratio_sativa']; ?>%"><?php echo $seed['ratio_sativa']; ?>%</div>
                    <div class="progress-bar text-bg-success" style="width: <?php echo 100 - $seed['ratio_sativa']; ?>%"><?php echo 100 - $seed['ratio_sativa']; ?>%</div>
                </div>
            </td>
            <td class="align-middle">
                <?php $badge_class = $seed['is_autoflower'] ? 'text-bg-info' : 'text-bg-dark'; $badge_text = $seed['is_autoflower'] ? 'Autoflower' : 'Photoperiodisch'; echo "<span class='badge {$badge_class}'>{$badge_text}</span>"; ?>
            </td>
            <td class="align-middle">
                <span class="badge text-bg-secondary fs-6"><?php echo $seed['stock_count']; ?></span>
            </td>
            <td>
                <button class="btn btn-sm btn-outline-primary edit-seed-btn" data-bs-toggle="modal" data-bs-target="#seedModal" data-seed='<?php echo json_encode($seed, JSON_HEX_APOS | JSON_HEX_QUOT); ?>'>✏️</button>
                <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" data-bs-target="#deleteConfirmModal" data-id="<?php echo $seed['id']; ?>" data-name="<?php echo htmlspecialchars($seed['strain_name']); ?>" data-type="seed">🗑️</button>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<div class="modal fade" id="seedModal" tabindex="-1"><div class="modal-dialog modal-lg"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="seedModalLabel">Samen</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><form id="seed-form"><input type="hidden" name="action" id="seed-form-action"><input type="hidden" name="id" id="seed-id"><div class="row"><div class="col-md-6"><div class="mb-3"><label class="form-label">Sortenname</label><input type="text" class="form-control" name="strain_name" required></div></div><div class="col-md-6"><div class="mb-3"><label class="form-label">Interne Bezeichnung (optional)</label><input type="text" class="form-control" name="internal_name"></div></div></div><div class="row"><div class="col-md-8"><div class="mb-3"><label class="form-label">Info-URL</label><input type="url" class="form-control" name="info_url" placeholder="https://..."></div></div><div class="col-md-4"><div class="mb-3"><label class="form-label">Anzahl</label><input type="number" class="form-control" name="stock_count" min="0" value="0" required></div></div></div><div class="mb-3"><label for="ratio_sativa" class="form-label">Genetik: <span id="sativa-value-label">50</span>% Sativa / <span id="indica-value-label">50</span>% Indica</label><input type="range" class="form-range" id="ratio_sativa" name="ratio_sativa" min="0" max="100" step="5" value="50"></div><div class="form-check mb-3"><input class="form-check-input" type="checkbox" name="is_autoflower" value="1" id="is_autoflower"><label class="form-check-label" for="is_autoflower">Selbstblühend (Autoflower)</label></div><div class="mb-3"><label class="form-label">Eigene Kurzbeschreibung</label><textarea class="form-control" name="description" rows="3"></textarea></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="seed-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'; ?>
