<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title><?= esc($title) ?></title>
    <style>
        @page {
            margin: 10mm;
        }

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

        .heading {
            margin-bottom: 8mm;
            padding-bottom: 3mm;
            border-bottom: 1px solid #d1d5db;
        }

        .heading-title {
            font-size: 16px;
            font-weight: 700;
        }

        .heading-subtitle {
            margin-top: 2px;
            color: #6b7280;
            font-size: 10px;
        }

        table.labels {
            width: 100%;
            border-collapse: collapse;
        }

        table.labels td {
            width: 33.333%;
            padding: 2mm;
            vertical-align: top;
        }

        .label {
            height: <?= $type === 'barcode' ? '36mm' : '37mm' ?>;
            padding: 2mm;
            overflow: hidden;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            text-align: center;
        }

        .label-image {
            display: block;
            max-width: 100%;
            height: <?= $type === 'barcode' ? '19mm' : '24mm' ?>;
            margin: 0 auto 1mm;
            object-fit: contain;
        }

        .label-code {
            font-size: 9px;
            font-weight: 700;
            line-height: 1.1;
        }

        .label-name {
            margin-top: .5mm;
            font-size: 9px;
            line-height: 1.15;
        }

        .empty {
            padding: 14mm;
            border: 1px solid #d1d5db;
            text-align: center;
            color: #6b7280;
        }
    </style>
</head>
<body>
    <div class="heading">
        <div class="heading-title"><?= esc($title) ?></div>
        <div class="heading-subtitle">Berisi kode barang dan nama barang untuk ditempel pada barang (opsional).</div>
    </div>

    <?php if (empty($labels)) : ?>
        <div class="empty">Belum ada data barang yang dapat dicetak.</div>
    <?php else : ?>
        <?php $columns = 3; ?>
        <table class="labels">
            <?php foreach (array_chunk($labels, $columns) as $row) : ?>
                <tr>
                    <?php foreach ($row as $label) : ?>
                        <td>
                            <div class="label">
                                <img src="<?= esc($label['image'], 'attr') ?>" class="label-image" alt="<?= esc($label['kode_barang'], 'attr') ?>">
                                <div class="label-code"><?= esc($label['kode_barang']) ?></div>
                                <div class="label-name"><?= esc($label['nama_barang']) ?></div>
                            </div>
                        </td>
                    <?php endforeach; ?>

                    <?php for ($i = count($row); $i < $columns; $i++) : ?>
                        <td></td>
                    <?php endfor; ?>
                </tr>
            <?php endforeach; ?>
        </table>
    <?php endif; ?>
</body>
</html>
