<?php
session_start();

require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Inventory.php';

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);

if (!isset($_SESSION['user_id']) || !$auth->isSuperAdmin($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}

$item = null;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $itemId = filter_input(INPUT_POST, 'item_id', FILTER_VALIDATE_INT);
    if (!$itemId) {
        $error = 'Please enter a valid item ID.';
    } else {
        $inventory = new Inventory($conn);
        $item = $inventory->getItemById($itemId);
        if (!$item) {
            $error = 'No item was found for that ID.';
        }
    }
}

function itemValue(array $item, array $keys, $default = 'N/A') {
    foreach ($keys as $key) {
        if (isset($item[$key]) && $item[$key] !== '') {
            return $item[$key];
        }
    }
    return $default;
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<div class="container mt-5">
        <h1 class="mb-4">Item Details</h1>

        <?php if ($error): ?>
            <div class="alert alert-warning"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <form action="get_item_by_id.php" method="POST">
            <div class="form-group">
                <label for="item_id">Item ID</label>
                <input type="number" name="item_id" id="item_id" class="form-control" min="1" required>
            </div>
            <button type="submit" class="btn btn-primary mt-3">Get Item</button>
        </form>

        <?php if ($item): ?>
            <table class="table table-bordered mt-4">
                <thead>
                    <tr>
                        <th>Item ID</th>
                        <th>Barcode</th>
                        <th>Item Name</th>
                        <th>Item Details</th>
                        <th>Description</th>
                        <th>Unit Price</th>
                        <th>Created By</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo htmlspecialchars(itemValue($item, ['item_id'])); ?></td>
                        <td><?php echo htmlspecialchars(itemValue($item, ['barcode', 'barcode_data', 'unique_code'])); ?></td>
                        <td><?php echo htmlspecialchars(itemValue($item, ['item_name'])); ?></td>
                        <td><?php echo htmlspecialchars(itemValue($item, ['item_details'])); ?></td>
                        <td><?php echo htmlspecialchars(itemValue($item, ['description'])); ?></td>
                        <td><?php echo htmlspecialchars(number_format((float)itemValue($item, ['unit_price'], 0), 2)); ?></td>
                        <td><?php echo htmlspecialchars(itemValue($item, ['created_by'])); ?></td>
                    </tr>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
