<?php
// admin/low-stock.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

// Get low stock items
$stmt = $pdo->query("SELECT p.id, p.name, p.sku, p.stock, p.reorder_level, p.price,
                     c.name as category_name
                     FROM products p 
                     LEFT JOIN categories c ON p.category_id = c.id 
                     WHERE p.status = 1 AND p.stock <= p.reorder_level
                     ORDER BY p.stock ASC");
$low_stock_items = $stmt->fetchAll();

include 'header.php';
?>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-exclamation-triangle me-2"></i> Low Stock Report</h2>
        <a href="inventory.php" class="btn btn-secondary">
            <i class="fa-solid fa-arrow-left"></i> Back to Inventory
        </a>
    </div>
    
    <?php if (empty($low_stock_items)): ?>
    <div class="alert alert-success">
        <i class="fa-solid fa-check-circle me-2"></i> All products have sufficient stock!
    </div>
    <?php else: ?>
    <div class="alert alert-warning">
        <i class="fa-solid fa-exclamation-triangle me-2"></i> 
        <strong><?php echo count($low_stock_items); ?></strong> products are below or at reorder level.
    </div>
    
    <div class="admin-card">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Category</th>
                        <th>SKU</th>
                        <th>Current Stock</th>
                        <th>Reorder Level</th>
                        <th>Price</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($low_stock_items as $item): ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($item['name']); ?></strong></td>
                        <td><?php echo $item['category_name'] ?? 'N/A'; ?></td>
                        <td><?php echo $item['sku'] ?? '-'; ?></td>
                        <td class="text-danger fw-bold"><?php echo $item['stock']; ?></td>
                        <td><?php echo $item['reorder_level']; ?></td>
                        <td><?php echo number_format($item['price']); ?> TK</td>
                        <td>
                            <button onclick="editStock(<?php echo $item['id']; ?>)" class="btn btn-sm btn-warning">
                                <i class="fa-solid fa-edit"></i> Update
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>
</div>

<script>
function editStock(id) {
    window.location.href = 'inventory.php#editStock';
    // This will be handled by the inventory page
}
</script>

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