<?= view('admin/layouts/header', ['title' => 'Detail Service']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'service',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Detail Service',
    'pageSubtitle' => $transaksi['no_transaksi'] ?? '-',
    'topbarAction' => '<div class="d-flex gap-2"><a href="' . site_url('admin/service') . '" class="btn btn-outline-dark"><i class="bi bi-arrow-left me-1"></i>Kembali</a></div>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-4">
        <div class="col-12 col-xl-4">
            <div class="panel p-4 h-100">
                <div class="mb-3">
                    <div class="small text-secondary">Tanggal Service</div>
                    <div class="fw-semibold"><?= esc(date('d/m/Y', strtotime($transaksi['tanggal_service']))) ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Pelanggan</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_pelanggan'] ?? '-') ?></div>
                    <?php if (! empty($transaksi['no_hp_pelanggan'])) : ?>
                        <div class="small text-secondary"><?= esc($transaksi['no_hp_pelanggan']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Teknisi</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_teknisi'] ?? '-') ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Objek Service</div>
                    <div class="fw-semibold"><?= ! empty($transaksi['merk_tipe']) ? esc($transaksi['merk_tipe']) : '-' ?></div>
                    <div class="small text-secondary"><?= ! empty($transaksi['nomor_objek']) ? esc($transaksi['nomor_objek']) : '-' ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Status Service</div>
                    <?php
                    $status = (string) ($transaksi['status_service'] ?? 'menunggu');
                    $badgeClasses = [
                        'dalam_pengerjaan' => 'text-bg-primary',
                        'selesai_siap_ambil' => 'text-bg-info',
                        'selesai_diambil' => 'text-bg-success',
                    ];
                    $badgeClass = $badgeClasses[$status] ?? 'text-bg-warning';
                    ?>
                    <span class="badge <?= esc($badgeClass, 'attr') ?>"><?= esc($statusLabels[$status] ?? $status) ?></span>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Status Pembayaran</div>
                    <?php $isLunas = ($transaksi['status_pembayaran'] ?? 'piutang') === 'lunas'; ?>
                    <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Piutang' ?></span>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">User Input</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_user'] ?? '-') ?></div>
                </div>

                <div>
                    <div class="small text-secondary">Keterangan/Keluhan</div>
                    <div><?= ! empty($transaksi['keterangan']) ? nl2br(esc($transaksi['keterangan'])) : '-' ?></div>
                </div>
            </div>
        </div>

        <div class="col-12 col-xl-8">
            <div class="panel p-4 mb-4">
                <div class="d-flex align-items-center justify-content-between gap-3 mb-3">
                    <div class="fw-bold">Part/Barang Diganti</div>
                    <div class="badge text-bg-light border"><?= esc((string) count($barangItems)) ?> item</div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th>Kode</th>
                                <th>Nama Barang</th>
                                <th class="text-end">Qty</th>
                                <th class="text-end">Harga</th>
                                <th class="text-end">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($barangItems as $item) : ?>
                                <tr>
                                    <td class="fw-semibold"><?= esc($item['kode_barang']) ?></td>
                                    <td><?= esc($item['nama_barang']) ?> <span class="badge text-bg-light border"><?= esc($item['satuan']) ?></span></td>
                                    <td class="text-end"><?= esc((string) $item['jumlah']) ?></td>
                                    <td class="text-end">Rp <?= esc(number_format((float) ($item['harga_jual'] ?? 0), 0, ',', '.')) ?></td>
                                    <td class="text-end">Rp <?= esc(number_format((float) ($item['subtotal'] ?? 0), 0, ',', '.')) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($barangItems)) : ?>
                                <tr>
                                    <td colspan="5" class="text-center text-secondary py-4">Belum ada part/barang.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="panel p-4">
                <div class="d-flex align-items-center justify-content-between gap-3 mb-3">
                    <div class="fw-bold">Jasa Service</div>
                    <div class="badge text-bg-light border"><?= esc((string) count($jasaItems)) ?> jasa</div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th>Nama Jasa</th>
                                <th class="text-end">Biaya</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($jasaItems as $item) : ?>
                                <tr>
                                    <td class="fw-semibold"><?= esc($item['nama_jasa']) ?></td>
                                    <td class="text-end">Rp <?= esc(number_format((float) ($item['biaya'] ?? 0), 0, ',', '.')) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($jasaItems)) : ?>
                                <tr>
                                    <td colspan="2" class="text-center text-secondary py-4">Belum ada jasa.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th class="text-end">Subtotal Part</th>
                                <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['subtotal_barang'] ?? 0), 0, ',', '.')) ?></th>
                            </tr>
                            <tr>
                                <th class="text-end">Subtotal Jasa</th>
                                <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['subtotal_jasa'] ?? 0), 0, ',', '.')) ?></th>
                            </tr>
                            <tr>
                                <th class="text-end">Pajak (<?= esc((string) ($transaksi['pajak_persen'] ?? 0)) ?>%)</th>
                                <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['pajak_nominal'] ?? 0), 0, ',', '.')) ?></th>
                            </tr>
                            <tr>
                                <th class="text-end">Total Service</th>
                                <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['total_service'] ?? 0), 0, ',', '.')) ?></th>
                            </tr>
                            <tr>
                                <th class="text-end">Total Dibayar</th>
                                <th class="text-end">Rp <?= esc(number_format((float) ($totalDibayar ?? 0), 0, ',', '.')) ?></th>
                            </tr>
                            <tr>
                                <th class="text-end">Sisa Piutang</th>
                                <th class="text-end">Rp <?= esc(number_format(max(0, (float) ($transaksi['total_service'] ?? 0) - (float) ($totalDibayar ?? 0)), 0, ',', '.')) ?></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
