<?php
require_once __DIR__ . '/../includes/config.php';

$page_title = 'Stoc';

require_once __DIR__ . '/../includes/header.php';

// Get stock statistics
$result = $db->query("SELECT COUNT(*) as count FROM products WHERE stock > min_stock");
$ok_stock = $result->fetch_assoc()['count'];

$result = $db->query("SELECT COUNT(*) as count FROM products WHERE stock <= min_stock AND stock > 0");
$low_stock = $result->fetch_assoc()['count'];

$result = $db->query("SELECT COUNT(*) as count FROM products WHERE stock <= 0");
$out_of_stock = $result->fetch_assoc()['count'];

// Get products
$result = $db->query("
    SELECT p.*, 
           (SELECT COALESCE(SUM(oi.quantity), 0) FROM order_items oi JOIN orders o ON oi.order_id = o.id WHERE oi.product_id = p.id) as sold_count
    FROM products p
    ORDER BY p.stock ASC, p.name ASC
");

$products = [];
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $products[] = $row;
    }
}
?>

<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:14px;margin-bottom:20px">
    <div class="stat-card">
        <div class="stat-label" style="color:var(--green)">Stoc ok</div>
        <div class="stat-value" style="color:var(--green)"><?php echo $ok_stock; ?></div>
        <div class="stat-sub">produse disponibile</div>
    </div>
    <div class="stat-card">
        <div class="stat-label" style="color:var(--yellow)">Stoc scăzut</div>
        <div class="stat-value" style="color:var(--yellow)"><?php echo $low_stock; ?></div>
        <div class="stat-sub">sub limita minimă</div>
    </div>
    <div class="stat-card">
        <div class="stat-label" style="color:var(--red)">Epuizat</div>
        <div class="stat-value" style="color:var(--red)"><?php echo $out_of_stock; ?></div>
        <div class="stat-sub">produse la 0</div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="card-title">Gestiune stoc</div>
    </div>
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Produs</th>
                    <th>SKU</th>
                    <th>Stoc</th>
                    <th>Min.</th>
                    <th>Nivel</th>
                    <th>Vândute</th>
                    <th style="text-align:right">Ajustare</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($products as $p): ?>
                    <?php
                    $pct = min(100, round(($p['stock'] / max($p['min_stock'] * 3, 1)) * 100));
                    $bc = $p['stock'] <= 0 ? 'var(--red)' : ($p['stock'] <= $p['min_stock'] ? 'var(--yellow)' : 'var(--green)');
                    ?>
                    <tr>
                        <td style="font-weight:500"><?php echo htmlspecialchars($p['name']); ?></td>
                        <td class="td-muted"><?php echo htmlspecialchars($p['sku'] ?? '—'); ?></td>
                        <td><strong style="color:<?php echo $bc; ?>"><?php echo $p['stock']; ?></strong></td>
                        <td class="td-muted"><?php echo $p['min_stock']; ?></td>
                        <td style="min-width:100px">
                            <div style="width:100px;height:6px;background:var(--bg4);border-radius:3px;overflow:hidden;display:inline-flex">
                                <div style="height:100%;border-radius:3px;background:<?php echo $bc; ?>;width:<?php echo $pct; ?>%"></div>
                            </div>
                        </td>
                        <td class="td-muted"><?php echo $p['sold_count']; ?> buc.</td>
                        <td>
                            <form method="POST" action="<?php echo APP_URL; ?>/api/adjust-stock.php" style="display:flex;gap:6px;justify-content:flex-end;align-items:center">
                                <input type="hidden" name="product_id" value="<?php echo $p['id']; ?>">
                                <input type="number" name="quantity" placeholder="±" style="width:65px;text-align:center;background:var(--bg3);border:1px solid var(--border);color:var(--text);border-radius:7px;padding:5px 8px;font-family:'DM Sans',sans-serif;font-size:13px;outline:none" required>
                                <button type="submit" class="btn btn-ghost btn-sm">Aplică</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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