<?php
require_once 'header.php';
require_permission('items');

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) {
    header('Location: items.php');
    exit;
}

$stmt = $mysqli->prepare('SELECT code, name, unit, item_group_id FROM items WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($code, $name, $unit, $group_id);
if (!$stmt->fetch()) {
    $stmt->close();
    echo '<div class="alert alert-danger m-3">Artikl nije pronađen.</div>';
    require_once 'footer.php';
    exit;
}
$stmt->close();

$groups = $mysqli->query('SELECT id, name FROM item_groups ORDER BY name');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $unit = trim($_POST['unit'] ?? '');
    $group_id = (int)($_POST['item_group_id'] ?? 0);

    if ($name === '' || $unit === '') {
        echo '<div class="alert alert-danger m-3">Naziv i JM su obavezni.</div>';
    } else {
        if ($group_id <= 0) {
            $stmtU = $mysqli->prepare('UPDATE items SET name=?, unit=?, item_group_id=NULL WHERE id=?');
            $stmtU->bind_param('ssi', $name, $unit, $id);
        } else {
            $stmtU = $mysqli->prepare('UPDATE items SET name=?, unit=?, item_group_id=? WHERE id=?');
            $stmtU->bind_param('ssii', $name, $unit, $group_id, $id);
        }
        $stmtU->execute();
        $stmtU->close();
        header('Location: items.php?msg=' . urlencode('Artikl ažuriran.'));
        exit;
    }
}
?>
<div class="row g-3 mb-3">
    <div class="col">
        <div class="section-title">Uredi artikl</div>
    </div>
</div>
<div class="card card-soft">
    <div class="card-body card-soft-body">
        <form method="post">
            <div class="mb-2">
                <label class="form-label">Šifra</label>
                <input type="text" class="form-control" value="<?php echo htmlspecialchars($code); ?>" disabled>
            </div>
            <div class="mb-2">
                <label class="form-label">Grupa</label>
                <select name="item_group_id" class="form-select form-select-sm">
                    <option value="0">-- bez grupe --</option>
                    <?php
                    $groups->data_seek(0);
                    while ($g = $groups->fetch_assoc()):
                        $sel = ($group_id == (int)$g['id']) ? 'selected' : '';
                    ?>
                        <option value="<?php echo $g['id']; ?>" <?php echo $sel; ?>>
                            <?php echo htmlspecialchars($g['name']); ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="mb-2">
                <label class="form-label">Naziv</label>
                <input type="text" name="name" class="form-control" required
                       value="<?php echo htmlspecialchars($name); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Jedinica mjere</label>
                <input type="text" name="unit" class="form-control" required
                       value="<?php echo htmlspecialchars($unit); ?>">
            </div>
            <button class="btn btn-primary btn-soft" type="submit">Spremi</button>
            <a href="items.php" class="btn btn-outline-secondary btn-soft">Natrag</a>
        </form>
    </div>
</div>
<?php require_once 'footer.php'; ?>
