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

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

        .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: 16.66%;
            padding: 7px 8px;
            border: 1px solid #d1d5db;
        }

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

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

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

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

        .items th,
        .items td {
            padding: 4px;
            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
    $pelangganLabel = 'Semua pelanggan';
    foreach ($pelanggan as $item) {
        if (($filters['pelanggan_id'] ?? '') === (string) $item['id']) {
            $pelangganLabel = $item['nama'];
            break;
        }
    }
    ?>

    <div class="header">
        <div class="title">LAPORAN PENJUALAN</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">Jumlah Terjual</div>
                <div class="summary-value"><?= esc((string) $summary['total_jumlah']) ?></div>
            </td>
            <td>
                <div class="summary-label">Nilai Penjualan</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_penjualan'] ?? 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>
            <td>
                <div class="summary-label">Total Modal</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_modal'] ?? 0), 0, ',', '.')) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Laba</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_laba'] ?? 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>
        |
        Pelanggan: <strong><?= esc($pelangganLabel) ?></strong>
    </div>

    <table class="items">
        <thead>
            <tr>
                <th style="width: 24px;" class="text-center">No</th>
                <th style="width: 52px;">Tanggal</th>
                <th style="width: 46px;">Sumber</th>
                <th style="width: 84px;">No. Transaksi</th>
                <th style="width: 94px;">Pelanggan</th>
                <th>Nama Barang</th>
                <th style="width: 42px;" class="text-right">Jumlah</th>
                <th style="width: 64px;" class="text-right">Jual</th>
                <th style="width: 64px;" class="text-right">Modal</th>
                <th style="width: 70px;" class="text-right">Subtotal</th>
                <th style="width: 62px;" class="text-right">Pajak</th>
                <th style="width: 62px;" class="text-right">Laba</th>
                <th style="width: 70px;">User</th>
            </tr>
        </thead>
        <tbody>
            <?php $shownTaxTransactions = []; ?>
            <?php foreach ($rows as $index => $row) : ?>
                <?php
                $transaksiId = ($row['jenis_transaksi'] ?? 'penjualan') . '-' . (int) ($row['transaksi_id'] ?? 0);
                $showTax = ! isset($shownTaxTransactions[$transaksiId]);
                $shownTaxTransactions[$transaksiId] = true;
                ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc(date('d/m/Y', strtotime($row['tanggal_penjualan']))) ?></td>
                    <td><?= esc($row['sumber_transaksi'] ?? 'Penjualan') ?></td>
                    <td><?= esc($row['no_transaksi']) ?></td>
                    <td><?= esc($row['nama_pelanggan'] ?? '-') ?></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_jual'] ?? 0), 0, ',', '.')) ?></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 class="text-right"><?= $showTax ? 'Rp ' . esc(number_format((float) ($row['pajak_nominal'] ?? 0), 0, ',', '.')) : '-' ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['laba'] ?? 0), 0, ',', '.')) ?></td>
                    <td><?= esc($row['nama_user'] ?? '-') ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)) : ?>
                <tr>
                    <td colspan="13" class="text-center">Tidak ada data penjualan.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
