<?= view('admin/layouts/header', ['title' => 'Transaksi Service']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'service',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Transaksi Service',
    'pageSubtitle' => 'Kelola pekerjaan service, part yang diganti, dan biaya jasa.',
    'topbarAction' => '<a href="' . site_url('admin/service/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Service</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
    <?php endif; ?>

    <div class="panel p-4 mb-4">
        <form action="<?= site_url('admin/service') ?>" method="get" class="row g-3 align-items-end">
            <div class="col-12 col-lg-4">
                <label for="keyword" class="form-label fw-semibold">Pencarian</label>
                <input type="search" class="form-control" id="keyword" name="keyword" value="<?= esc($filters['keyword'] ?? '', 'attr') ?>" placeholder="No transaksi, pelanggan, teknisi, objek">
            </div>
            <div class="col-12 col-md-6 col-lg-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 service</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-6 col-lg-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 pembayaran</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-lg-2">
                <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/service') ?>" class="btn btn-light">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <?php if (empty($transaksi)) : ?>
        <div class="panel p-4 text-center text-secondary">
            Belum ada transaksi service<?= ! empty($filters['keyword']) || ! empty($filters['status_service']) || ! empty($filters['status_pembayaran']) ? ' sesuai pencarian/filter.' : '.' ?>
        </div>
    <?php else : ?>
        <div class="row g-3">
            <?php foreach ($transaksi as $item) : ?>
                <?php
                $status = (string) ($item['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';
                $isLunas = ($item['status_pembayaran'] ?? 'piutang') === 'lunas';
                $subtotalBarang = (float) ($item['subtotal_barang'] ?? 0);
                $subtotalJasa = (float) ($item['subtotal_jasa'] ?? 0);
                $totalService = (float) ($item['total_service'] ?? 0);
                $totalDibayar = (float) ($item['total_dibayar'] ?? 0);
                $sisa = max(0, $totalService - $totalDibayar);
                ?>
                <div class="col-12 col-md-6 col-xl-4">
                    <div class="border rounded-2 bg-white p-3 h-100 shadow-sm">
                        <div class="d-flex align-items-start justify-content-between gap-3 mb-3">
                            <div>
                                <div class="small text-secondary"><?= esc(date('d/m/Y', strtotime($item['tanggal_service']))) ?></div>
                                <div class="fw-bold"><?= esc($item['no_transaksi']) ?></div>
                            </div>
                            <div class="text-end">
                                <span class="badge <?= esc($badgeClass, 'attr') ?> mb-1"><?= esc($statusLabels[$status] ?? $status) ?></span><br>
                                <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Piutang' ?></span>
                            </div>
                        </div>

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

                        <div class="row g-2 mb-3">
                            <div class="col-6">
                                <div class="small text-secondary">Pelanggan</div>
                                <div class="fw-semibold text-truncate"><?= esc($item['nama_pelanggan'] ?? '-') ?></div>
                            </div>
                            <div class="col-6">
                                <div class="small text-secondary">Teknisi</div>
                                <div class="fw-semibold text-truncate"><?= esc($item['nama_teknisi'] ?? '-') ?></div>
                            </div>
                        </div>

                        <div class="d-flex gap-2 mb-3">
                            <span class="badge text-bg-light border">
                                <i class="bi bi-cpu me-1"></i><?= esc((string) ((int) ($item['total_part'] ?? 0))) ?> part
                            </span>
                            <span class="badge text-bg-light border">
                                <i class="bi bi-tools me-1"></i><?= esc((string) ((int) ($item['total_jasa'] ?? 0))) ?> jasa
                            </span>
                        </div>

                        <table class="table table-sm align-middle mb-3">
                            <tbody>
                                <tr>
                                    <th>Part</th>
                                    <td class="text-end">Rp <?= esc(number_format($subtotalBarang, 0, ',', '.')) ?></td>
                                </tr>
                                <tr>
                                    <th>Jasa</th>
                                    <td class="text-end">Rp <?= esc(number_format($subtotalJasa, 0, ',', '.')) ?></td>
                                </tr>
                                <tr>
                                    <th>Total</th>
                                    <td class="text-end fw-bold">Rp <?= esc(number_format($totalService, 0, ',', '.')) ?></td>
                                </tr>
                                <tr>
                                    <th>Dibayar</th>
                                    <td class="text-end">Rp <?= esc(number_format($totalDibayar, 0, ',', '.')) ?></td>
                                </tr>
                                <tr>
                                    <th>Sisa</th>
                                    <td class="text-end fw-bold <?= $sisa > 0 ? 'text-danger' : 'text-success' ?>">Rp <?= esc(number_format($sisa, 0, ',', '.')) ?></td>
                                </tr>
                            </tbody>
                        </table>

                        <div class="d-flex align-items-center justify-content-between gap-2">
                            <div class="small text-secondary text-truncate">
                                <i class="bi bi-person small"></i> <?= esc($item['nama_user'] ?? '-') ?>
                            </div>
                            <div class="text-nowrap">
                                <a href="<?= site_url('admin/service/cicilan/' . $item['id']) ?>" class="btn btn-sm btn-success btn-icon" title="Cicilan service" aria-label="Cicilan service">
                                    <i class="bi bi-cash-coin"></i>
                                </a>
                                <a href="<?= site_url('admin/service/nota/' . $item['id']) ?>" target="_blank" class="btn btn-sm btn-secondary btn-icon" title="Cetak nota service" aria-label="Cetak nota service">
                                    <i class="bi bi-printer"></i>
                                </a>
                                <a href="<?= site_url('admin/service/detail/' . $item['id']) ?>" class="btn btn-sm btn-info btn-icon" title="Detail service" aria-label="Detail service">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <?php if (($userRole ?? '') === 'administrator') : ?>
                                    <a href="<?= site_url('admin/service/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit service" aria-label="Edit service">
                                        <i class="bi bi-pencil-square"></i>
                                    </a>
                                    <form action="<?= site_url('admin/service/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus transaksi service ini? Stok part akan dikembalikan.">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus service" aria-label="Hapus service">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php if (! empty($pager)) : ?>
            <div class="d-flex justify-content-center mt-4">
                <?= $pager->links('service', 'bootstrap_full') ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</section>

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