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

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

$stmt = $mysqli->prepare("
    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
    WHERE r.id = ?
");
$stmt->bind_param('i', $id);
$stmt->execute();
$rec = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$rec) {
    echo '<div class="alert alert-danger m-3">Primka nije pronađena.</div>';
    require_once 'footer.php'; exit;
}

$dt = DateTime::createFromFormat('Y-m-d', $rec['receipt_date']);
$date_hr = $dt ? $dt->format('d.m.Y') : $rec['receipt_date'];

$items = $mysqli->query("
    SELECT ri.item_id, i.code, i.name, ri.quantity, ri.unit
    FROM receipt_items ri
    JOIN items i ON i.id = ri.item_id
    WHERE ri.receipt_id = ".(int)$id."
    ORDER BY i.name
");
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Pregled primke</div>
        <div class="text-muted small">Primka #<?php echo $rec['id']; ?></div>
    </div>
    <div class="col-auto d-flex flex-wrap gap-2">
        <a href="receipts.php" class="btn btn-outline-secondary btn-soft btn-sm">← Natrag</a>
        <a href="receipt_edit.php?id=<?php echo $rec['id']; ?>" class="btn btn-outline-primary btn-soft btn-sm">Uredi</a>
    </div>
</div>

<div class="card card-soft mb-3">
    <div class="card-body card-soft-body">
        <div class="row g-3">
            <div class="col-md-6">
                <div class="text-muted small">Dobavljač</div>
                <div class="fw-semibold"><?php echo htmlspecialchars($rec['supplier_name']); ?></div>
            </div>
            <div class="col-md-3">
                <div class="text-muted small">Datum</div>
                <div class="fw-semibold"><?php echo $date_hr; ?></div>
            </div>
            <div class="col-md-3">
                <div class="text-muted small">Broj dokumenta</div>
                <div class="fw-semibold"><?php echo htmlspecialchars($rec['doc_no'] ?? ''); ?></div>
            </div>
            <div class="col-12">
                <div class="text-muted small">Napomena</div>
                <div><?php echo htmlspecialchars($rec['note'] ?? ''); ?></div>
            </div>
        </div>
    </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">Stavke</span>
        <span class="pill-counter d-none d-sm-inline"><?php echo (int)$items->num_rows; ?> stavki</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>Šifra</th>
                    <th>Naziv</th>
                    <th class="text-end">Količina</th>
                    <th>JM</th>
                </tr>
                </thead>
                <tbody>
                <?php while ($it = $items->fetch_assoc()): ?>
                    <tr>
                        <td class="fw-semibold"><?php echo htmlspecialchars($it['code']); ?></td>
                        <td><?php echo htmlspecialchars($it['name']); ?></td>
                        <td class="text-end"><?php echo rtrim(rtrim(number_format((float)$it['quantity'], 3, ',', '.'), '0'), ','); ?></td>
                        <td><?php echo htmlspecialchars($it['unit']); ?></td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>

        <div class="d-md-none">
            <?php
            $items->data_seek(0);
            if ($items->num_rows === 0): ?>
                <div class="text-muted small">Nema stavki.</div>
            <?php endif; ?>
            <?php while ($it = $items->fetch_assoc()): ?>
                <div class="list-card">
                    <div class="list-card-title"><?php echo htmlspecialchars($it['name']); ?></div>
                    <div class="list-card-sub">Šifra: <?php echo htmlspecialchars($it['code']); ?></div>
                    <div class="list-card-meta">
                        Količina: <strong><?php echo rtrim(rtrim(number_format((float)$it['quantity'], 3, ',', '.'), '0'), ','); ?></strong>
                        <?php echo htmlspecialchars($it['unit']); ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
</div>

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