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

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

// lista primki
$sql = "SELECT r.id, r.receipt_date, r.doc_no, r.note, s.name AS supplier_name
        FROM receipts r
        JOIN suppliers s ON s.id = r.supplier_id
        ORDER BY r.receipt_date DESC, r.id DESC";
$receipts = $mysqli->query($sql);
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Primke</div>
        <div class="text-muted small">Unos ulaznih dokumenata i stavki</div>
    </div>
    <div class="col-auto">
        <a href="receipt_new.php" class="btn btn-primary btn-soft btn-sm">+ Nova primka</a>
    </div>
</div>

<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 primki</span>
        <span class="pill-counter d-none d-sm-inline">
            <?php echo (int)$receipts->num_rows; ?> primki
        </span>
    </div>
    <div class="card-body card-soft-body">
        <div class="table-responsive d-none d-md-block">
            <table class="table table-sm align-middle">
                <thead class="small text-muted">
                <tr>
                    <th>#</th>
                    <th>Datum</th>
                    <th>Dobavljač</th>
                    <th>Dokument</th>
                    <th>Napomena</th>
                    <th class="text-end">Akcije</th>
                </tr>
                </thead>
                <tbody>
                <?php while ($r = $receipts->fetch_assoc()):
                    $dt = DateTime::createFromFormat('Y-m-d', $r['receipt_date']);
                    $date_hr = $dt ? $dt->format('d.m.Y') : $r['receipt_date'];
                ?>
                    <tr>
                        <td><?php echo $r['id']; ?></td>
                        <td><?php echo $date_hr; ?></td>
                        <td><?php echo htmlspecialchars($r['supplier_name']); ?></td>
                        <td><?php echo htmlspecialchars($r['doc_no']); ?></td>
                        <td><?php echo htmlspecialchars($r['note']); ?></td>
                        <td class="text-end">
                            <a href="receipt_view.php?id=<?php echo $r['id']; ?>" class="btn btn-sm btn-outline-info btn-soft">Pregled</a>
                            <a href="receipt_edit.php?id=<?php echo $r['id']; ?>" class="btn btn-sm btn-outline-primary btn-soft">Uredi</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>

        <div class="d-md-none">
            <?php
            $receipts->data_seek(0);
            if ($receipts->num_rows === 0): ?>
                <div class="text-muted small">Nema primki.</div>
            <?php endif; ?>
            <?php while ($r = $receipts->fetch_assoc()):
                $dt = DateTime::createFromFormat('Y-m-d', $r['receipt_date']);
                $date_hr = $dt ? $dt->format('d.m.Y') : $r['receipt_date'];
            ?>
                <div class="list-card">
                    <div class="list-card-title">
                        <?php echo htmlspecialchars($r['supplier_name']); ?>
                    </div>
                    <div class="list-card-sub">
                        Datum: <strong><?php echo $date_hr; ?></strong>
                        <?php if (!empty($r['doc_no'])): ?>
                            • Br: <?php echo htmlspecialchars($r['doc_no']); ?>
                        <?php endif; ?>
                    </div>
                    <div class="list-card-meta">
                        <?php echo htmlspecialchars($r['note']); ?>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-2">
                        <a href="receipt_view.php?id=<?php echo $r['id']; ?>" class="btn btn-outline-info btn-sm">Pregled</a>
                        <a href="receipt_edit.php?id=<?php echo $r['id']; ?>" class="btn btn-outline-primary btn-sm">Uredi</a>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
</div>

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