<?php
require_once __DIR__ . '/../config.php';
requireLogin();

$pageTitle = 'Inventory Management';
include __DIR__ . '/../includes/admin-header.php';
?>

        <div class="row g-4 mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">Inventory Items</h5>
                            <div class="d-flex gap-2">
                                <select class="form-select form-select-sm" id="categoryFilter" style="width: 150px;">
                                    <option value="">All Categories</option>
                                    <option value="Chemical">Chemical</option>
                                    <option value="Feed">Feed</option>
                                    <option value="Fuel">Fuel</option>
                                    <option value="Consumable">Consumable</option>
                                </select>
                                <button class="btn btn-sm btn-outline-warning" onclick="loadInventory(1)">Low Stock Only</button>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover" id="inventoryTable">
                                <thead>
                                    <tr>
                                        <th>Item Code</th>
                                        <th>Item Name</th>
                                        <th>Category</th>
                                        <th>UOM</th>
                                        <th>Stock On Hand</th>
                                        <th>Reorder Level</th>
                                        <th>Unit Cost</th>
                                        <th>Stock Value</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody id="inventoryBody">
                                    <tr><td colspan="9" class="text-center">Loading...</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
        function loadInventory(lowStock = 0) {
            const category = document.getElementById('categoryFilter').value;
            let url = '../api/inventory.php?low_stock=' + lowStock;
            if (category) url += '&category=' + category;
            
            fetch(url)
                .then(r => r.json())
                .then(response => {
                    const tbody = document.getElementById('inventoryBody');
                    if (!response.data || response.data.length === 0) {
                        tbody.innerHTML = '<tr><td colspan="9" class="text-center">No items found</td></tr>';
                        return;
                    }
                    tbody.innerHTML = response.data.map(item => {
                        const soh = parseFloat(item.StockOnHand) || 0;
                        const reorder = parseFloat(item.ReorderLevel) || 0;
                        const status = soh <= reorder ? '<span class="badge bg-danger">Low Stock</span>' : '<span class="badge bg-success">OK</span>';
                        return '<tr>' +
                            '<td>' + item.ItemCode + '</td>' +
                            '<td>' + item.ItemName + '</td>' +
                            '<td>' + item.Category + '</td>' +
                            '<td>' + item.UnitOfMeasure + '</td>' +
                            '<td>' + soh.toLocaleString() + '</td>' +
                            '<td>' + reorder.toLocaleString() + '</td>' +
                            '<td>$' + (parseFloat(item.UnitCost) || 0).toFixed(2) + '</td>' +
                            '<td>$' + (parseFloat(item.StockValue) || 0).toLocaleString() + '</td>' +
                            '<td>' + status + '</td>' +
                        '</tr>';
                    }).join('');
                });
        }

        document.getElementById('categoryFilter').addEventListener('change', () => loadInventory(0));
        loadInventory(0);
        </script>

<?php include __DIR__ . '/../includes/admin-footer.php'; ?>