<?php
/**
 * Enterprise HRM ERP System - Inventory Items Management
 */

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/check_admin_access.php';

$pageTitle = 'Inventory Items';
$currentModule = 'settings';

$siteId = getCurrentSiteId();

// Get inventory items
$items = [];
try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT * FROM inventory_items
        WHERE site_id = ?
        ORDER BY item_name
    ");
    $stmt->execute([$siteId]);
    $items = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Inventory items error: " . $e->getMessage());
}

include __DIR__ . '/../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Inventory Items Management</h2>
        <a href="<?php echo BASE_URL; ?>/admin/inventory_items_create.php" class="btn btn-primary">Add Inventory Item</a>
    </div>
    
    <?php if (count($items) > 0): ?>
    <div style="margin-top: 20px; overflow-x: auto;">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Item Code</th>
                    <th>Item Name</th>
                    <th>Category</th>
                    <th>Unit</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($item['item_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($item['item_name']); ?></td>
                    <td><?php echo htmlspecialchars($item['item_category'] ?? 'N/A'); ?></td>
                    <td><?php echo htmlspecialchars($item['unit']); ?></td>
                    <td>
                        <span class="status-badge status-<?php echo $item['is_active'] ? 'active' : 'inactive'; ?>">
                            <?php echo $item['is_active'] ? 'Active' : 'Inactive'; ?>
                        </span>
                    </td>
                    <td class="table-actions-cell">
                        <a href="<?php echo BASE_URL; ?>/admin/inventory_items_edit.php?id=<?php echo $item['item_id']; ?>" 
                           class="btn btn-sm btn-primary">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div class="table-empty" style="margin-top: 30px;">
        <div class="table-empty-icon">📦</div>
        <div class="table-empty-message">No inventory items found</div>
        <div class="table-empty-submessage">
            <a href="<?php echo BASE_URL; ?>/admin/inventory_items_create.php" class="btn btn-primary">Add First Inventory Item</a>
        </div>
    </div>
    <?php endif; ?>
</div>

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

