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

        body {
            margin: 0;
            color: #111827;
            font-family: DejaVu Sans, Arial, sans-serif;
            font-size: 9px;
            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;
            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
    $suplierLabel = 'Semua supplier';
    foreach ($suplier as $item) {
        if (($filters['suplier_id'] ?? '') === (string) $item['id']) {
            $suplierLabel = $item['nama'] . ' - ' . $item['perusahaan'];
            break;
        }
    }
    ?>

    <div class="header">
        <div class="title">LAPORAN PEMBELIAN</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 Transaksi</div>
                <div class="summary-value"><?= esc((string) $summary['total_transaksi']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Item Barang</div>
                <div class="summary-value"><?= esc((string) $summary['total_item']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Jumlah Pembelian</div>
                <div class="summary-value"><?= esc((string) $summary['total_jumlah']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Nilai Pembelian</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_harga'] ?? 0), 0, ',', '.')) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Pajak</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_pajak'] ?? 0), 0, ',', '.')) ?></div>
            </td>
        </tr>
    </table>

    <div class="filter">
        Periode:
        <strong><?= ! empty($filters['tanggal_mulai']) ? esc(date('d/m/Y', strtotime($filters['tanggal_mulai']))) : 'Awal' ?></strong>
        s/d
        <strong><?= ! empty($filters['tanggal_selesai']) ? esc(date('d/m/Y', strtotime($filters['tanggal_selesai']))) : 'Akhir' ?></strong>
        |
        Supplier: <strong><?= esc($suplierLabel) ?></strong>
    </div>

    <table class="items">
        <thead>
            <tr>
                <th style="width: 26px;" class="text-center">No</th>
                <th style="width: 58px;">Tanggal</th>
                <th style="width: 94px;">No. Transaksi</th>
                <th style="width: 118px;">Supplier</th>                
                <th>Nama Barang</th>                
                <th style="width: 48px;" class="text-right">Jumlah</th>
                <th style="width: 70px;" class="text-right">Harga</th>
                <th style="width: 76px;" class="text-right">Subtotal</th>
                <th style="width: 100px;">Dokumen</th>
                <th style="width: 78px;">User</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $index => $row) : ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc(date('d/m/Y', strtotime($row['tanggal_pembelian']))) ?></td>
                    <td><?= esc($row['no_transaksi']) ?></td>
                    <td><?= esc($row['nama_suplier'] ?? '-') ?></td>
                    <td><?= esc($row['kode_barang']) ?> - <?= esc($row['nama_barang']) ?></td>                    
                    <td class="text-right"><?= esc((string) $row['jumlah']) ?> <?= esc($row['satuan']) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['harga_beli'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['subtotal'] ?? 0), 0, ',', '.')) ?></td>
                    <td><?= ! empty($row['nomor_dokumen']) ? esc($row['nomor_dokumen']) : '-' ?></td>
                    <td><?= esc($row['nama_user'] ?? '-') ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)) : ?>
                <tr>
                    <td colspan="10" class="text-center">Tidak ada data pembelian.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
