<?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>Sorte</th>
            <th>Interne Bez.</th>
            <th>Genetik</th>
            <th>Anzahl</th>
            <th>Info</th>
            <th style="width: 120px;" class="no-sort text-center">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>
                                            <strong><?php echo htmlspecialchars($seed['strain_name']); ?></strong>
                                        </td>
                                        <td><?php echo htmlspecialchars($seed['internal_name']); ?></td>
                                        <td>
                                            <?php 
                                                $sativa = intval($seed['ratio_sativa'] ?? 50);
                                                $indica = 100 - $sativa;
                                            ?>
                                            <div class="progress" style="height: 6px; min-width: 80px;" title="<?php echo $sativa; ?>% Sativa / <?php echo $indica; ?>% Indica">
                                                <div class="progress-bar bg-success" role="progressbar" style="width: <?php echo $sativa; ?>%"></div>
                                                <div class="progress-bar bg-warning" role="progressbar" style="width: <?php echo $indica; ?>%"></div>
                                            </div>
                                            <small class="text-muted" style="font-size: 0.75em;"><?php echo $sativa; ?>% S / <?php echo $indica; ?>% I</small>
                                        </td>
                                        <td>
                                            <span class="badge <?php echo $seed['stock_count'] > 0 ? 'text-bg-success' : 'text-bg-danger'; ?>">
                                                <?php echo $seed['stock_count']; ?> Stk.
                                            </span>
                                        </td>
                                        <td>
                                            <?php if(!empty($seed['info_url'])): ?>
                                                <a href="<?php echo htmlspecialchars($seed['info_url']); ?>" target="_blank" class="btn btn-sm btn-outline-dark" title="Link zum Breeder"><i class="bi bi-link-45deg"></i></a>
                                            <?php else: ?>
                                                <span class="text-muted">-</span>
                                            <?php endif; ?>
                                        </td>
                                                                        <td class="text-center">
                                                                            <button class="btn btn-sm btn-outline-secondary edit-seed-btn" 
                                                                                    data-bs-toggle="modal" data-bs-target="#seedModal" 
                                                                                    data-seed='<?php echo json_encode($seed, JSON_HEX_APOS | JSON_HEX_QUOT); ?>'>
                                                                                <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 $seed['id']; ?>" 
                                                                                    data-name="<?php echo htmlspecialchars($seed['strain_name']); ?>" 
                                                                                    data-type="seed">
                                                                                <i class="bi bi-trash-fill"></i>
                                                                            </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-cazubu" 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'; ?>
