<?= view('admin/layouts/header', ['title' => 'Laporan Service']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'laporan-service',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?php
$query = array_filter([
    'tanggal_mulai'     => $filters['tanggal_mulai'] ?? '',
    'tanggal_selesai'   => $filters['tanggal_selesai'] ?? '',
    'pelanggan_id'      => $filters['pelanggan_id'] ?? '',
    'teknisi_id'        => $filters['teknisi_id'] ?? '',
    'status_service'    => $filters['status_service'] ?? '',
    'status_pembayaran' => $filters['status_pembayaran'] ?? '',
], static fn($value) => $value !== '');
$pdfUrl = site_url('admin/laporan/service/pdf') . ($query ? '?' . http_build_query($query) : '');
$excelUrl = site_url('admin/laporan/service/excel') . ($query ? '?' . http_build_query($query) : '');
$statusLabels = [
    'menunggu'           => 'Menunggu',
    'dalam_pengerjaan'   => 'Dalam Pengerjaan',
    'selesai_siap_ambil' => 'Selesai dan Siap Ambil',
    'selesai_diambil'    => 'Selesai Diambil',
];
?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Laporan Service',
    'pageSubtitle' => 'Rekap transaksi service, pendapatan jasa, part, modal, dan laba.',
    'topbarAction' => '<a href="' . $excelUrl . '" class="btn btn-success"><i class="bi bi-file-earmark-excel me-1"></i>Export Excel</a>'
        . '<a href="' . $pdfUrl . '" target="_blank" class="btn btn-dark"><i class="bi bi-file-earmark-pdf me-1"></i>Export PDF</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-3 mb-4">
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Total Transaksi</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_transaksi']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Pendapatan Jasa</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_jasa'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Penjualan Part</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_part'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Total Service</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_service'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Total Laba</div>
                <div class="h4 fw-bold mb-0 <?= (float) ($summary['total_laba'] ?? 0) < 0 ? 'text-danger' : 'text-success' ?>">
                    Rp <?= esc(number_format((float) ($summary['total_laba'] ?? 0), 0, ',', '.')) ?>
                </div>
            </div>
        </div>
    </div>

    <div class="panel p-4 mb-4">
        <form action="<?= site_url('admin/laporan/service') ?>" method="get" class="row g-3 align-items-end">
            <div class="col-12 col-md-3">
                <label for="tanggal_mulai" class="form-label fw-semibold">Tanggal Mulai</label>
                <input type="date" class="form-control" id="tanggal_mulai" name="tanggal_mulai" value="<?= esc($filters['tanggal_mulai'] ?? '', 'attr') ?>">
            </div>
            <div class="col-12 col-md-3">
                <label for="tanggal_selesai" class="form-label fw-semibold">Tanggal Selesai</label>
                <input type="date" class="form-control" id="tanggal_selesai" name="tanggal_selesai" value="<?= esc($filters['tanggal_selesai'] ?? '', 'attr') ?>">
            </div>
            <div class="col-12 col-md-3">
                <label for="pelanggan_id" class="form-label fw-semibold">Pelanggan</label>
                <select class="form-select select2" id="pelanggan_id" name="pelanggan_id" data-placeholder="Semua pelanggan">
                    <option value="">Semua pelanggan</option>
                    <?php foreach ($pelanggan as $item) : ?>
                        <option value="<?= esc($item['id'], 'attr') ?>" <?= ($filters['pelanggan_id'] ?? '') === (string) $item['id'] ? 'selected' : '' ?>>
                            <?= esc($item['nama']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <label for="teknisi_id" class="form-label fw-semibold">Teknisi</label>
                <select class="form-select select2" id="teknisi_id" name="teknisi_id" data-placeholder="Semua teknisi">
                    <option value="">Semua teknisi</option>
                    <?php foreach ($teknisi as $item) : ?>
                        <option value="<?= esc($item['id'], 'attr') ?>" <?= ($filters['teknisi_id'] ?? '') === (string) $item['id'] ? 'selected' : '' ?>>
                            <?= esc($item['nama']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <label for="status_service" class="form-label fw-semibold">Status Service</label>
                <select class="form-select" id="status_service" name="status_service">
                    <option value="">Semua Status</option>
                    <?php foreach ($statusLabels as $statusKey => $statusLabel) : ?>
                        <option value="<?= esc($statusKey, 'attr') ?>" <?= ($filters['status_service'] ?? '') === $statusKey ? 'selected' : '' ?>>
                            <?= esc($statusLabel) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <label for="status_pembayaran" class="form-label fw-semibold">Status Pembayaran</label>
                <select class="form-select" id="status_pembayaran" name="status_pembayaran">
                    <option value="">Semua Status</option>
                    <option value="lunas" <?= ($filters['status_pembayaran'] ?? '') === 'lunas' ? 'selected' : '' ?>>Lunas</option>
                    <option value="piutang" <?= ($filters['status_pembayaran'] ?? '') === 'piutang' ? 'selected' : '' ?>>Piutang</option>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-dark flex-fill">
                        <i class="bi bi-funnel me-1"></i>Filter
                    </button>
                    <a href="<?= site_url('admin/laporan/service') ?>" class="btn btn-light">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <div class="panel p-4">
        <div class="table-responsive">
            <table class="table table-hover align-middle datatable">
                <thead>
                    <tr>
                        <th>Tanggal</th>
                        <th>No. Service</th>
                        <th>Pelanggan</th>                        
                        <th class="text-end">Part</th>
                        <th class="text-end">Jasa</th>
                        <th class="text-end">Pajak</th>
                        <th class="text-end">Total</th>
                        <th class="text-end">Modal</th>
                        <th class="text-end">Laba</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $row) : ?>
                        <?php $isLunas = ($row['status_pembayaran'] ?? 'piutang') === 'lunas'; ?>
                        <tr>
                            <td><?= esc(date('d/m/Y', strtotime($row['tanggal_service']))) ?></td>
                            <td>
                                <span  class="fw-semibold d-block"><?= esc($row['no_transaksi']) ?></span>
                                <span class="small">Teknisi : <?= esc($row['nama_teknisi'] ?? '-') ?></span>
                            </td>
                            <td>
                                <span class="d-block"><?= esc($row['nama_pelanggan'] ?? '-') ?></span>
                                <span class="small text-secondary d-block"><?= esc($row['no_hp_pelanggan'] ?? '-') ?></span>

                                <div>
                                    <span class="d-block"><?= ! empty($row['merk_tipe']) ? esc($row['merk_tipe']) : '-' ?></span>
                                    <span class="small text-secondary"><?= ! empty($row['nomor_objek']) ? esc($row['nomor_objek']) : '-' ?></span>
                                </div>
                            </td>                            
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['subtotal_barang'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['subtotal_jasa'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['pajak_nominal'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end fw-semibold">Rp <?= esc(number_format((float) ($row['total_service'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['total_modal_part'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end fw-semibold <?= (float) ($row['laba'] ?? 0) < 0 ? 'text-danger' : 'text-success' ?>">
                                Rp <?= esc(number_format((float) ($row['laba'] ?? 0), 0, ',', '.')) ?>
                            </td>
                            <td>
                                <span class="badge text-bg-light border mb-1"><?= esc($statusLabels[$row['status_service']] ?? ($row['status_service'] ?? '-')) ?></span>
                                <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Piutang' ?></span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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