<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Laporan Service</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, .filter { 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; }
        table.items { width: 100%; margin-top: 12px; border-collapse: collapse; }
        .items th, .items td { padding: 4px; border: 1px solid #d1d5db; vertical-align: top; }
        .items th { background: #f3f4f6; font-weight: 700; text-align: left; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
    </style>
</head>
<body>
    <?php
    $statusLabels = [
        'menunggu'           => 'Menunggu',
        'dalam_pengerjaan'   => 'Dalam Pengerjaan',
        'selesai_siap_ambil' => 'Selesai dan Siap Ambil',
        'selesai_diambil'    => 'Selesai Diambil',
    ];
    $pelangganLabel = 'Semua pelanggan';
    foreach ($pelanggan as $item) {
        if (($filters['pelanggan_id'] ?? '') === (string) $item['id']) {
            $pelangganLabel = $item['nama'];
            break;
        }
    }
    $teknisiLabel = 'Semua teknisi';
    foreach ($teknisi as $item) {
        if (($filters['teknisi_id'] ?? '') === (string) $item['id']) {
            $teknisiLabel = $item['nama'];
            break;
        }
    }
    ?>

    <div class="header">
        <div class="title">LAPORAN SERVICE</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">Pendapatan Jasa</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_jasa'] ?? 0), 0, ',', '.')) ?></div>
            </td>
            <td>
                <div class="summary-label">Penjualan Part</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_part'] ?? 0), 0, ',', '.')) ?></div>
            </td>
            <td>
                <div class="summary-label">Total Service</div>
                <div class="summary-value">Rp <?= esc(number_format((float) ($summary['total_service'] ?? 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>
        |
        Teknisi: <strong><?= esc($teknisiLabel) ?></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: 82px;">No. Service</th>
                <th style="width: 85px;">Pelanggan</th>
                <th style="width: 70px;">Teknisi</th>
                <th>Objek / Jasa</th>
                <th style="width: 58px;" class="text-right">Part</th>
                <th style="width: 58px;" class="text-right">Jasa</th>
                <th style="width: 58px;" class="text-right">Pajak</th>
                <th style="width: 62px;" class="text-right">Total</th>
                <th style="width: 58px;" class="text-right">Modal</th>
                <th style="width: 58px;" class="text-right">Laba</th>
                <th style="width: 62px;">Status</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_service']))) ?></td>
                    <td><?= esc($row['no_transaksi']) ?></td>
                    <td><?= esc($row['nama_pelanggan'] ?? '-') ?></td>
                    <td><?= esc($row['nama_teknisi'] ?? '-') ?></td>
                    <td>
                        <?= ! empty($row['merk_tipe']) ? esc($row['merk_tipe']) : '-' ?>
                        <?= ! empty($row['nomor_objek']) ? ' / ' . esc($row['nomor_objek']) : '' ?><br>
                        <?= ! empty($row['daftar_jasa']) ? esc($row['daftar_jasa']) : '-' ?>
                    </td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['subtotal_barang'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['subtotal_jasa'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['pajak_nominal'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['total_service'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['total_modal_part'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format((float) ($row['laba'] ?? 0), 0, ',', '.')) ?></td>
                    <td><?= esc($statusLabels[$row['status_service']] ?? ($row['status_service'] ?? '-')) ?><br><?= (($row['status_pembayaran'] ?? 'piutang') === 'lunas') ? 'Lunas' : 'Piutang' ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)) : ?>
                <tr>
                    <td colspan="13" class="text-center">Tidak ada data service.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>
