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

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

$stmt = $mysqli->prepare('SELECT name, oib, address, city, phone, email, note FROM suppliers WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($name, $oib, $address, $city, $phone, $email, $note);
if (!$stmt->fetch()) {
    $stmt->close();
    echo '<div class="alert alert-danger m-3">Dobavljač nije pronađen.</div>';
    require_once 'footer.php';
    exit;
}
$stmt->close();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name'] ?? '');
    $oib     = trim($_POST['oib'] ?? '');
    $address = trim($_POST['address'] ?? '');
    $city    = trim($_POST['city'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $note    = trim($_POST['note'] ?? '');

    if ($name === '') {
        echo '<div class="alert alert-danger m-3">Naziv je obavezan.</div>';
    } else {
        $stmtU = $mysqli->prepare('UPDATE suppliers SET name=?, oib=?, address=?, city=?, phone=?, email=?, note=? WHERE id=?');
        $stmtU->bind_param('sssssssi', $name, $oib, $address, $city, $phone, $email, $note, $id);
        $stmtU->execute();
        $stmtU->close();
        header('Location: suppliers.php?msg=' . urlencode('Dobavljač ažuriran.'));
        exit;
    }
}
?>
<div class="row g-3 mb-3">
    <div class="col">
        <div class="section-title">Uredi dobavljača</div>
        <div class="text-muted small">Ažuriranje podataka dobavljača</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">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">OIB</label>
                <input type="text" name="oib" class="form-control"
                       value="<?php echo htmlspecialchars($oib); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Adresa</label>
                <input type="text" name="address" class="form-control"
                       value="<?php echo htmlspecialchars($address); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Grad</label>
                <input type="text" name="city" class="form-control"
                       value="<?php echo htmlspecialchars($city); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Telefon</label>
                <input type="text" name="phone" class="form-control"
                       value="<?php echo htmlspecialchars($phone); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Email</label>
                <input type="email" name="email" class="form-control"
                       value="<?php echo htmlspecialchars($email); ?>">
            </div>
            <div class="mb-2">
                <label class="form-label">Napomena</label>
                <input type="text" name="note" class="form-control"
                       value="<?php echo htmlspecialchars($note); ?>">
            </div>

            <button class="btn btn-primary btn-soft" type="submit">Spremi</button>
            <a href="suppliers.php" class="btn btn-outline-secondary btn-soft">Natrag</a>
        </form>
    </div>
</div>

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