<?php
require_once '../../includes/auth_middleware.php';
require_once '../../config/database.php';
require_once '../../includes/header.php';
checkRole(['admin']);

$ingredients = $pdo->query("SELECT * FROM ingredients")->fetchAll();
?>

<div class="container">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
        <h2>Manajemen Stok</h2>
        <button class="btn-primary" style="width: auto; padding: 10px 20px;">+ Tambah Bahan</button>
    </div>

    <div class="card">
        <table style="width: 100%; border-collapse: collapse;">
            <thead>
                <tr style="border-bottom: 2px solid #eee; text-align: left;">
                    <th style="padding: 10px;">Bahan</th>
                    <th>Stok</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ingredients as $ing): 
                    $is_low = $ing['stock_qty'] <= $ing['min_stock_level'];
                ?>
                <tr style="border-bottom: 1px solid #eee;">
                    <td style="padding: 10px;"><?= $ing['name'] ?></td>
                    <td><?= $ing['stock_qty'] ?> <?= $ing['unit'] ?></td>
                    <td>
                        <?php if ($is_low): ?>
                            <span style="color: var(--accent); font-weight: bold;">⚠️ KRITIS</span>
                        <?php else: ?>
                            <span style="color: #10B981;">Aman</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>