<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Laporan Stok Barang</title>
    <style>
        @page { margin: 12mm; }

        body {
            margin: 0;
            color: #111827;
            font-family: DejaVu Sans, Arial, sans-serif;
            font-size: 10px;
            line-height: 1.35;
        }

        .header {
            padding-bottom: 8px;
            border-bottom: 2px solid #111827;
            text-align: center;
        }

        .title {
            font-size: 17px;
            font-weight: 700;
            letter-spacing: .4px;
        }

        .subtitle {
            margin-top: 3px;
            color: #4b5563;
        }

        .summary {
            width: 100%;
            margin-top: 12px;
            border-collapse: collapse;
        }

        .summary td {
            width: 25%;
            padding: 7px 8px;
            border: 1px solid #d1d5db;
        }

        .summary-label {
            color: #6b7280;
            font-size: 9px;
        }

        .summary-value {
            font-size: 14px;
            font-weight: 700;
        }

        .filter {
            margin-top: 10px;
            color: #4b5563;
        }

        table.items {
            width: 100%;
            margin-top: 12px;
            border-collapse: collapse;
        }

        .items th,
        .items td {
            padding: 5px 6px;
            border: 1px solid #d1d5db;
        }

        .items th {
            background: #f3f4f6;
            font-weight: 700;
            text-align: left;
        }

        .text-right { text-align: right; }
        .text-center { text-align: center; }
    </style>
</head>
<body>
    <?php
    $kategoriLabel = 'Semua kategori';
    foreach ($kategori as $item) {
        if (($filters['kategori_id'] ?? '') === (string) $item['id']) {
            $kategoriLabel = $item['nama_kategori'];
            break;
        }
    }

    $statusMap = [
        ''              => 'Semua status',
        'aman'          => 'Aman',
        'hampir-habis'  => 'Hampir habis',
        'kosong'        => 'Kosong',
    ];
    ?>

    <div class="header">
        <div class="title">LAPORAN STOK BARANG</div>
        <div class="subtitle">Dicetak pada <?= esc(date('d/m/Y H:i')) ?></div>
    </div>

    <table class="summary">
        <tr>
            <td>
                <div class="summary-label">Total Barang</div>
                <div class="summary-value"><?= esc((string) $summary['total_barang']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Stok</div>
                <div class="summary-value"><?= esc((string) $summary['total_stok']) ?></div>
            </td>
            <td>
                <div class="summary-label">Hampir Habis</div>
                <div class="summary-value"><?= esc((string) $summary['hampir_habis']) ?></div>
            </td>
            <td>
                <div class="summary-label">Stok Kosong</div>
                <div class="summary-value"><?= esc((string) $summary['kosong']) ?></div>
            </td>
        </tr>
    </table>

    <div class="filter">
        Kategori: <strong><?= esc($kategoriLabel) ?></strong> |
        Status: <strong><?= esc($statusMap[$filters['status'] ?? ''] ?? 'Semua status') ?></strong>
    </div>

    <table class="items">
        <thead>
            <tr>
                <th style="text-align:center; width: 28px;" class="text-center">No</th>
                <th style="text-align:center; width: 84px;">Kode</th>
                <th>Nama Barang</th>
                <th style="text-align:center; width: 130px;">Kategori</th>
                <th style="text-align:center; width: 140px;">Lokasi</th>                            
                <th style="text-align:center; width: 56px;" class="text-right">Stok</th>                
                <th style="text-align:center; width: 72px;">Status</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($barang as $index => $item) : ?>
                <?php
                $stok = (int) ($item['stok'] ?? 0);
                $stokMinimum = (int) ($item['stok_minimum'] ?? 0);
                $statusLabel = $stok <= 0 ? 'Kosong' : ($stok <= $stokMinimum ? 'Hampir habis' : 'Aman');
                ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc($item['kode_barang']) ?></td>
                    <td><?= esc($item['nama_barang']) ?></td>
                    <td><?= esc($item['nama_kategori'] ?? '-') ?></td>
                    <td><?= ! empty($item['lokasi_barang']) ? esc($item['lokasi_barang']) : '-' ?></td>                        
                    <td class="text-right"><?= esc((string) $stok) . ' ' . esc($item['satuan']) ?></td>                    
                    <td><?= esc($statusLabel) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($barang)) : ?>
                <tr>
                    <td colspan="10" class="text-center">Tidak ada data stok barang.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
