<?= view('admin/layouts/header', ['title' => 'Pembelian']) ?>
<?php $isAdministrator = ($userRole ?? '') === 'administrator'; ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'pembelian',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Pembelian',
    'pageSubtitle' => 'Kelola transaksi pembelian barang dari supplier.',
    'topbarAction' => '<a href="' . site_url('admin/pembelian/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Pembelian</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/pembelian') ?>" method="get" class="row g-3 align-items-end">
            <div class="col-12 col-md-4 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 Status</option>
                    <option value="lunas" <?= ($statusPembayaran ?? '') === 'lunas' ? 'selected' : '' ?>>Lunas</option>
                    <option value="hutang" <?= ($statusPembayaran ?? '') === 'hutang' ? 'selected' : '' ?>>Hutang</option>
                </select>
            </div>
            <div class="col-12 col-md-auto">
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-dark">
                        <i class="bi bi-funnel me-1"></i>Filter
                    </button>
                    <a href="<?= site_url('admin/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>No. Transaksi</th>                        
                        <th>Supplier</th>
                        <th class="text-center">Total Pembelian</th>
                        <th class="text-center">Terbayar</th>
                        <th class="text-center">Hutang</th>
                        <th width="1%">Status</th>
                        <th class="text-end" width="13%">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transaksi as $item) : ?>
                        <tr>                            
                            <td>
                                <span class="text-muted small"><?= esc(date('d/m/Y', strtotime($item['tanggal_pembelian']))) ?><br></span>
                                <span class="fw-semibold d-block"><?= esc($item['no_transaksi']) ?></span>
                                <span><i class="bi bi-person small"></i> <?= esc($item['nama_user'] ?? '-') ?></span>
                            </td>
                            <td><?= esc($item['nama_suplier'] ?? '-') ?></td>                           
                            <td class="text-center">Rp <?= esc(number_format((float) ($item['total_harga'] ?? 0), 0, ',', '.')) ?></td>
                            <td class="text-center">
                                Rp <?= esc(number_format((float) ($item['total_dibayar'] ?? 0), 0, ',', '.')) ?>                                
                            </td>
                            <td class="text-center">                                
                                Rp <?= esc(number_format(max(0, (float) ($item['total_harga'] ?? 0) - (float) ($item['total_dibayar'] ?? 0)), 0, ',', '.')) ?>
                            </td>
                            <td>
                                <?php $isLunas = ($item['status_pembayaran'] ?? 'hutang') === 'lunas'; ?>
                                <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Hutang' ?></span>
                            </td>                            
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/pembelian/pdf/' . $item['id']) ?>" target="_blank" class="btn btn-sm btn-outline-dark btn-icon" title="Export PDF pembelian" aria-label="Export PDF pembelian">
                                    <i class="bi bi-file-earmark-pdf"></i>
                                </a>
                                <a href="<?= site_url('admin/pembelian/detail/' . $item['id']) ?>" class="btn btn-sm btn-info btn-icon" title="Detail pembelian" aria-label="Detail pembelian">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="<?= site_url('admin/pembelian/cicilan/' . $item['id']) ?>" class="btn btn-sm btn-success btn-icon" title="Cicilan pembelian" aria-label="Cicilan pembelian">
                                    <i class="bi bi-cash-coin"></i>
                                </a>
                                <?php if ($isAdministrator) : ?>
                                    <a href="<?= site_url('admin/pembelian/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit pembelian" aria-label="Edit pembelian">
                                        <i class="bi bi-pencil-square"></i>
                                    </a>
                                    <form action="<?= site_url('admin/pembelian/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus transaksi pembelian ini? Stok dan harga beli moving average akan dihitung ulang.">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus pembelian" aria-label="Hapus pembelian">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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