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

// poruka
if (!empty($_GET['msg'])) {
    echo '<div class="alert alert-success mb-3">'.htmlspecialchars($_GET['msg']).'</div>';
}

// dodavanje
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_supplier'])) {
    $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 mb-3">Naziv je obavezan.</div>';
    } else {
        $stmt = $mysqli->prepare('INSERT INTO suppliers (name, oib, address, city, phone, email, note) VALUES (?,?,?,?,?,?,?)');
        $stmt->bind_param('sssssss', $name, $oib, $address, $city, $phone, $email, $note);
        $stmt->execute();
        $stmt->close();
        echo '<div class="alert alert-success mb-3">Dobavljač dodan.</div>';
    }
}

// brisanje
if (isset($_GET['delete_id'])) {
    $id = (int)$_GET['delete_id'];
    if ($id > 0) {
        try {
            $stmt = $mysqli->prepare('DELETE FROM suppliers WHERE id = ?');
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->close();
            echo '<div class="alert alert-success mb-3">Dobavljač obrisan.</div>';
        } catch (mysqli_sql_exception $ex) {
            echo '<div class="alert alert-danger mb-3">Dobavljača nije moguće obrisati (postoje primke vezane uz njega).</div>';
        }
    }
}

// pretraga
$search = trim($_GET['q'] ?? '');
$where = '';
if ($search !== '') {
    $esc = $mysqli->real_escape_string($search);
    $where = "WHERE name LIKE '%{$esc}%' OR city LIKE '%{$esc}%'";
}

$sql = "SELECT * FROM suppliers {$where} ORDER BY name";
$suppliers = $mysqli->query($sql);
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Dobavljači</div>
        <div class="text-muted small">Unos i pregled dobavljača</div>
    </div>
</div>

<div class="row g-3">
    <div class="col-12 col-lg-4">
        <div class="card card-soft">
            <div class="card-header card-soft-header">
                <span class="fw-semibold">Novi dobavljač</span>
            </div>
            <div class="card-body card-soft-body">
                <form method="post">
                    <input type="hidden" name="create_supplier" value="1">
                    <div class="mb-2">
                        <label class="form-label">Naziv</label>
                        <input type="text" name="name" class="form-control" required>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">OIB</label>
                        <input type="text" name="oib" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Adresa</label>
                        <input type="text" name="address" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Grad</label>
                        <input type="text" name="city" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Telefon</label>
                        <input type="text" name="phone" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Napomena</label>
                        <input type="text" name="note" class="form-control">
                    </div>
                    <button class="btn btn-primary btn-soft w-100" type="submit">Spremi dobavljača</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12 col-lg-8">
        <div class="card card-soft">
            <div class="card-header card-soft-header d-flex justify-content-between align-items-center">
                <span class="fw-semibold">Popis dobavljača</span>
                <span class="pill-counter d-none d-sm-inline">
                    <?php echo (int)$suppliers->num_rows; ?> dobavljača
                </span>
            </div>
            <div class="card-body card-soft-body">

                <div class="mb-3">
                    <form method="get" class="row g-2 align-items-end">
                        <div class="col-12 col-md-6">
                            <label class="form-label">Pretraga dobavljača</label>
                            <input type="text" name="q"
                                   class="form-control form-control-sm"
                                   placeholder="naziv ili grad..."
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <div class="col-auto">
                            <button class="btn btn-outline-primary btn-soft btn-sm" type="submit">
                                Primijeni
                            </button>
                        </div>
                        <?php if ($search !== ''): ?>
                        <div class="col-auto">
                            <a href="suppliers.php" class="btn btn-outline-secondary btn-soft btn-sm">Reset</a>
                        </div>
                        <?php endif; ?>
                    </form>
                </div>

                <div class="table-responsive d-none d-md-block">
                    <table class="table table-sm align-middle">
                        <thead class="small text-muted">
                        <tr>
                            <th>Naziv</th>
                            <th>Grad</th>
                            <th>Telefon</th>
                            <th>Email</th>
                            <th class="text-end">Akcije</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php while ($s = $suppliers->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($s['name']); ?></td>
                                <td><?php echo htmlspecialchars($s['city']); ?></td>
                                <td><?php echo htmlspecialchars($s['phone']); ?></td>
                                <td><?php echo htmlspecialchars($s['email']); ?></td>
                                <td class="text-end">
                                    <a href="supplier_edit.php?id=<?php echo $s['id']; ?>"
                                       class="btn btn-sm btn-outline-primary btn-soft">Uredi</a>
                                    <a href="suppliers.php?delete_id=<?php echo $s['id']; ?>"
                                       class="btn btn-sm btn-outline-danger btn-soft"
                                       onclick="return confirm('Obrisati dobavljača?');">Obriši</a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>

                <div class="d-md-none">
                    <?php
                    $suppliers->data_seek(0);
                    if ($suppliers->num_rows === 0): ?>
                        <div class="text-muted small">Nema dobavljača.</div>
                    <?php endif; ?>
                    <?php while ($s = $suppliers->fetch_assoc()): ?>
                        <div class="list-card">
                            <div class="list-card-title">
                                <?php echo htmlspecialchars($s['name']); ?>
                            </div>
                            <div class="list-card-sub">
                                <?php echo htmlspecialchars($s['city']); ?>
                            </div>
                            <div class="list-card-meta">
                                Tel: <?php echo htmlspecialchars($s['phone']); ?>
                                <?php if (!empty($s['email'])): ?>
                                    • <?php echo htmlspecialchars($s['email']); ?>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex justify-content-end mt-1">
                                <a href="supplier_edit.php?id=<?php echo $s['id']; ?>"
                                   class="btn btn-outline-primary btn-sm">Uredi</a>
                                <a href="suppliers.php?delete_id=<?php echo $s['id']; ?>"
                                   class="btn btn-outline-danger btn-sm"
                                   onclick="return confirm('Obrisati dobavljača?');">X</a>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>

            </div>
        </div>
    </div>
</div>

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