<?= view('admin/layouts/header', ['title' => 'Laporan Penjualan']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'laporan-penjualan',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?php
$query = array_filter([
    'tanggal_mulai'   => $filters['tanggal_mulai'] ?? '',
    'tanggal_selesai' => $filters['tanggal_selesai'] ?? '',
    'pelanggan_id'    => $filters['pelanggan_id'] ?? '',
], static fn ($value) => $value !== '');
$pdfUrl = site_url('admin/laporan/penjualan/pdf') . ($query ? '?' . http_build_query($query) : '');
$excelUrl = site_url('admin/laporan/penjualan/excel') . ($query ? '?' . http_build_query($query) : '');
?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Laporan Penjualan',
    'pageSubtitle' => 'Rekap detail penjualan barang beserta 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-3">
            <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-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Jumlah Terjual</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_jumlah']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="panel p-3">
                <div class="small text-secondary">Nilai Penjualan</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_penjualan'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Pajak</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_pajak'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Modal</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_modal'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <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/penjualan') ?>" 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-4">
                <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-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/laporan/penjualan') ?>" 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. Transaksi</th>
                        <th>Pelanggan</th>
                        <th>Barang</th>
                        <th class="text-end">Jumlah</th>
                        <th class="text-end">Harga Jual</th>
                        <th class="text-end">Modal</th>
                        <th class="text-end">Subtotal</th>
                        <th class="text-end">Pajak Transaksi</th>
                        <th class="text-end">Laba</th>
                        <th>User</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $shownTaxTransactions = []; ?>
                    <?php foreach ($rows as $row) : ?>
                        <?php
                        $transaksiId = ($row['jenis_transaksi'] ?? 'penjualan') . '-' . (int) ($row['transaksi_id'] ?? 0);
                        $showTax = ! isset($shownTaxTransactions[$transaksiId]);
                        $shownTaxTransactions[$transaksiId] = true;
                        ?>
                        <tr>
                            <td><?= esc(date('d/m/Y', strtotime($row['tanggal_penjualan']))) ?></td>
                            <td class="fw-semibold"><?= esc($row['no_transaksi']) ?><br><span class="badge text-bg-light border"><?= esc($row['sumber_transaksi'] ?? 'Penjualan') ?></span></td>
                            <td>
                                <span class="d-block"><?= esc($row['nama_pelanggan'] ?? '-') ?></span>
                                <span class="small text-secondary"><?= esc($row['no_hp_pelanggan'] ?? '-') ?></span>
                            </td>
                            <td>
                                <span class="fw-semibold d-block small"><?= esc($row['kode_barang']) ?></span>
                                <?= esc($row['nama_barang']) ?>
                            </td>
                            <td class="text-end fw-semibold"><?= esc((string) $row['jumlah']) ?> <?= esc($row['satuan']) ?></td>
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['harga_jual'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end">Rp <?= esc(number_format((float) ($row['harga_beli'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end fw-semibold">Rp <?= esc(number_format((float) ($row['subtotal'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end">
                                <?= $showTax ? 'Rp ' . esc(number_format((float) ($row['pajak_nominal'] ?? 0), 0, ',', '.')) : '<span class="text-secondary">-</span>' ?>
                            </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><?= esc($row['nama_user'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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