<?= view('admin/layouts/header', ['title' => 'Penjualan']) ?>
<?php $isAdministrator = ($userRole ?? '') === 'administrator'; ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'penjualan',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Penjualan',
    'pageSubtitle' => 'Kelola transaksi penjualan barang ke pelanggan.',
    'topbarAction' => '<a href="' . site_url('admin/penjualan/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Penjualan</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/penjualan') ?>" 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="piutang" <?= ($statusPembayaran ?? '') === 'piutang' ? 'selected' : '' ?>>Piutang</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/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>No. Transaksi</th>
                        <th>Pelanggan</th>
                        <th class="text-center">Total Penjualan</th>
                        <th class="text-center">Dibayar</th>
                        <th class="text-center">Piutang</th>
                        <th class="text-center">Status</th>
                        <th class="text-end" width="1%">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transaksi as $item) : ?>
                        <tr>
                            <td>
                                <?= esc(date('d/m/Y', strtotime($item['tanggal_penjualan']))) ?><br>
                                <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_pelanggan'] ?? '-') ?></td>
                            <td class="text-center">Rp <?= esc(number_format((float) ($item['total_penjualan'] ?? 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_penjualan'] ?? 0) - (float) ($item['total_dibayar'] ?? 0)), 0, ',', '.')) ?>
                            </td>
                            <td class="text-center">
                                <?php $isLunas = ($item['status_pembayaran'] ?? 'piutang') === 'lunas'; ?>
                                <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Piutang' ?></span>
                            </td>
                            <td class="text-end text-nowrap">
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-primary dropdown-toggle"
                                        type="button"
                                        data-bs-toggle="dropdown"
                                        aria-expanded="false">
                                        <i class="bi bi-three-dots"></i> Aksi
                                    </button>

                                    <ul class="dropdown-menu dropdown-menu-end">
                                        <li>
                                            <a class="dropdown-item"
                                                href="<?= site_url('admin/penjualan/detail/' . $item['id']) ?>">
                                                <i class="bi bi-eye me-2"></i>Detail
                                            </a>
                                        </li>

                                        <li>
                                            <a class="dropdown-item"
                                                href="<?= site_url('admin/penjualan/cicilan/' . $item['id']) ?>">
                                                <i class="bi bi-cash-coin me-2"></i>Pembayaran
                                            </a>
                                        </li>

                                        <li>
                                            <a class="dropdown-item"
                                                href="<?= site_url('admin/penjualan/nota/' . $item['id']) ?>"
                                                target="_blank">
                                                <i class="bi bi-printer me-2"></i>Cetak Nota
                                            </a>
                                        </li>

                                        <?php if ($isAdministrator) : ?>
                                            <li>
                                                <hr class="dropdown-divider">
                                            </li>

                                            <li>
                                                <a class="dropdown-item"
                                                    href="<?= site_url('admin/penjualan/edit/' . $item['id']) ?>">
                                                    <i class="bi bi-pencil-square me-2"></i>Edit
                                                </a>
                                            </li>

                                            <li>
                                                <form action="<?= site_url('admin/penjualan/delete/' . $item['id']) ?>"
                                                    method="post"
                                                    data-confirm="Hapus transaksi penjualan ini? Stok barang akan dikembalikan.">

                                                    <?= csrf_field() ?>

                                                    <button type="submit"
                                                        class="dropdown-item text-danger">
                                                        <i class="bi bi-trash3 me-2"></i>Hapus
                                                    </button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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