<?php
require_once __DIR__ . '/../includes/init.php';
require_role('admin');

$pdo = db();
$stmt = $pdo->query("SELECT * FROM products ORDER BY CASE product_role WHEN 'membrana' THEN 1 WHEN 'akcesoria' THEN 2 ELSE 3 END, created_at DESC");
$products = $stmt->fetchAll();

$page_title = 'Zarządzanie produktami';
?>
<?php require_once __DIR__ . '/../includes/html_head.php'; ?>
<body>

<?php include 'menu.php'; ?>

<main class="container my-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h2"><?php echo $page_title; ?></h1>
        <a href="edit_product.php" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> Dodaj produkt
        </a>
    </div>

    <div class="card">
        <div class="card-body">
            <table class="table table-striped table-hover">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Nazwa</th>
                        <th>Typ</th>
                        <th>Cena netto</th>
                        <th>Cena brutto</th>
                        <th>Aktywny</th>
                        <th>Akcje</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($products)):
                        ?>
                        <tr>
                            <td colspan="7" class="text-center">Nie znaleziono produktów.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($products as $product): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($product['id']); ?></td>
                            <td><?php echo htmlspecialchars($product['name']); ?></td>
                            <td><?php echo htmlspecialchars($product['product_role']); ?></td>
                            <td><?php echo number_format($product['price_net'], 2, ',', ' '); ?> zł</td>
                            <td><?php echo number_format($product['price_gross'], 2, ',', ' '); ?> zł</td>
                            <td>
                                <?php if ($product['is_active']): ?>
                                    <span class="badge bg-success">Tak</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">Nie</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="edit_product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-secondary">
                                    <i class="bi bi-pencil-fill"></i> Edytuj
                                </a>
                                <a href="toggle_active.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-info">
                                    <?php echo $product['is_active'] ? 'Dezaktywuj' : 'Aktywuj'; ?>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>

</body>
</html>