<?= view('admin/layouts/header', ['title' => 'Laporan Pembelian']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'laporan-pembelian',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?php
$query = array_filter([
    'tanggal_mulai'   => $filters['tanggal_mulai'] ?? '',
    'tanggal_selesai' => $filters['tanggal_selesai'] ?? '',
    'suplier_id'      => $filters['suplier_id'] ?? '',
], static fn ($value) => $value !== '');
$pdfUrl = site_url('admin/laporan/pembelian/pdf') . ($query ? '?' . http_build_query($query) : '');
$excelUrl = site_url('admin/laporan/pembelian/excel') . ($query ? '?' . http_build_query($query) : '');
?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Laporan Pembelian',
    'pageSubtitle' => 'Rekap detail pembelian barang berdasarkan tanggal dan supplier.',
    '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">Total Item Barang</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_item']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl">
            <div class="panel p-3">
                <div class="small text-secondary">Total Jumlah Pembelian</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">
            <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">
            <div class="panel p-3">
                <div class="small text-secondary">Total Nilai Pembelian</div>
                <div class="h4 fw-bold mb-0">Rp <?= esc(number_format((float) ($summary['total_harga'] ?? 0), 0, ',', '.')) ?></div>
            </div>
        </div>
    </div>

    <div class="panel p-4 mb-4">
        <form action="<?= site_url('admin/laporan/pembelian') ?>" 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="suplier_id" class="form-label fw-semibold">Supplier</label>
                <select class="form-select select2" id="suplier_id" name="suplier_id" data-placeholder="Semua supplier">
                    <option value="">Semua supplier</option>
                    <?php foreach ($suplier as $item) : ?>
                        <option value="<?= esc($item['id'], 'attr') ?>" <?= ($filters['suplier_id'] ?? '') === (string) $item['id'] ? 'selected' : '' ?>>
                            <?= esc($item['nama']) ?> - <?= esc($item['perusahaan']) ?>
                        </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/pembelian') ?>" 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>Supplier</th>                        
                        <th>Barang</th>                        
                        <th class="text-end">Jumlah</th>
                        <th class="text-end">Harga Beli</th>
                        <th class="text-end">Subtotal</th>
                        <th>Dokumen</th>
                        <th>User</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $row) : ?>
                        <tr>
                            <td><?= esc(date('d/m/Y', strtotime($row['tanggal_pembelian']))) ?></td>
                            <td class="fw-semibold"><?= esc($row['no_transaksi']) ?></td>
                            <td>
                                <span class="d-block"><?= esc($row['nama_suplier'] ?? '-') ?></span>
                                <span class="small text-secondary"><?= esc($row['perusahaan_suplier'] ?? '-') ?></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_beli'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-end fw-semibold">Rp <?= esc(number_format((float) ($row['subtotal'] ?? 0), 0, ',', '.')) ?></td>
                            <td><?= ! empty($row['nomor_dokumen']) ? esc($row['nomor_dokumen']) : '<span class="text-secondary">-</span>' ?></td>
                            <td><?= esc($row['nama_user'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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