<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Laporan Penyesuaian Stok</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
    $jenisLabel = [
        '' => 'Semua jenis',
        'penambahan' => 'Penambahan',
        'pengurangan' => 'Pengurangan',
        'sesuai' => 'Sesuai',
    ][$filters['jenis'] ?? ''] ?? 'Semua jenis';
    $statusLabel = [
        '' => 'Semua status',
        'aktif' => 'Aktif',
        'dibatalkan' => 'Dibatalkan',
    ][$filters['status'] ?? ''] ?? 'Semua status';
    ?>

    <div class="header">
        <div class="title">LAPORAN PENYESUAIAN STOK</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 Penyesuaian</div>
                <div class="summary-value"><?= esc((string) $summary['total_penyesuaian']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Penambahan</div>
                <div class="summary-value"><?= esc((string) $summary['total_penambahan']) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Pengurangan</div>
                <div class="summary-value"><?= esc((string) $summary['total_pengurangan']) ?></div>
            </td>
            <td>
                <div class="summary-label">Dibatalkan</div>
                <div class="summary-value"><?= esc((string) $summary['dibatalkan']) ?></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>
        |
        Jenis: <strong><?= esc($jenisLabel) ?></strong>
        |
        Status: <strong><?= esc($statusLabel) ?></strong>
        |
        Pencarian: <strong><?= ! empty($filters['keyword']) ? esc($filters['keyword']) : '-' ?></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: 100px;">No. Penyesuaian</th>
                <th>Barang</th>
                <th style="width: 52px;" class="text-right">Sistem</th>
                <th style="width: 52px;" class="text-right">Fisik</th>
                <th style="width: 50px;" class="text-right">Selisih</th>
                <th style="width: 70px;">Jenis</th>
                <th style="width: 90px;">Alasan</th>
                <th style="width: 64px;">Status</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_penyesuaian']))) ?></td>
                    <td><?= esc($row['no_penyesuaian']) ?></td>
                    <td><?= esc($row['kode_barang']) ?> - <?= esc($row['nama_barang']) ?></td>
                    <td class="text-right"><?= esc((string) $row['stok_sistem']) ?></td>
                    <td class="text-right"><?= esc((string) $row['stok_fisik']) ?></td>
                    <td class="text-right"><?= esc((string) $row['selisih']) ?></td>
                    <td><?= esc(ucfirst((string) $row['jenis'])) ?></td>
                    <td><?= esc($row['alasan']) ?></td>
                    <td><?= esc(ucfirst((string) $row['status'])) ?></td>
                    <td><?= esc($row['nama_user'] ?? '-') ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)) : ?>
                <tr>
                    <td colspan="11" class="text-center">Tidak ada data penyesuaian stok.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
