<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<style>
    .pagination-wrapper {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        margin-top: 20px;
    }

    .pagination {
        display: flex;
        flex-wrap: wrap;
        gap: 5px;
    }

    .pagination .page-link {
        padding: 6px 12px;
        border-radius: 6px;
        border: 1px solid #ccc;
        color: #333;
        text-decoration: none;
        background-color: #f9f9f9;
        transition: all 0.2s;
    }

    .pagination .page-link:hover {
        background-color: #e0e0e0;
    }

    .pagination .active .page-link {
        background-color: #007bff;
        color: white;
        border-color: #007bff;
    }

    @media(max-width: 576px) {
        .pagination-wrapper {
            flex-direction: column;
            align-items: flex-start;
        }
    }
</style>

<h4 class="mb-3">Data Item</h4>

<a href="<?= base_url('admin/item/create') ?>" class="btn btn-primary mb-3">
    <i class="fas fa-plus-circle"></i> Tambah Item
</a>

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

<form method="get" class="mb-3 d-flex flex-wrap gap-2">
    <input type="text" name="keyword" class="form-control" placeholder="Cari barcode atau nama..." value="<?= esc($keyword ?? '') ?>" style="max-width: 250px;">
    <button type="submit" class="btn btn-secondary"><i class="fas fa-search"></i> Cari</button>
</form>

<div class="table-responsive">
    <?= view('item/table', ['items' => $items]) ?>
</div>

<div class="pagination-wrapper">
    <div class="text-muted small">
        Total Data: <?= $totalItems ?> | Halaman <?= $currentPage ?> dari <?= $totalPages ?>
    </div>
    <?php if ($totalPages > 1): ?>
    <nav>
        <ul class="pagination justify-content-center flex-wrap">

            <!-- First -->
            <li class="page-item <?= ($currentPage <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?keyword=<?= urlencode($keyword) ?>&page=1">First</a>
            </li>

            <!-- Previous -->
            <li class="page-item <?= ($currentPage <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?keyword=<?= urlencode($keyword) ?>&page=<?= $currentPage - 1 ?>">«</a>
            </li>

            <?php
            $range = 2;
            $start = max(1, $currentPage - $range);
            $end   = min($totalPages, $currentPage + $range);

            for ($i = $start; $i <= $end; $i++): ?>
                <li class="page-item <?= ($i == $currentPage) ? 'active' : '' ?>">
                    <a class="page-link" href="?keyword=<?= urlencode($keyword) ?>&page=<?= $i ?>"><?= $i ?></a>
                </li>
            <?php endfor; ?>

            <!-- Next -->
            <li class="page-item <?= ($currentPage >= $totalPages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?keyword=<?= urlencode($keyword) ?>&page=<?= $currentPage + 1 ?>">»</a>
            </li>

            <!-- Last -->
            <li class="page-item <?= ($currentPage >= $totalPages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?keyword=<?= urlencode($keyword) ?>&page=<?= $totalPages ?>">Last</a>
            </li>
        </ul>
    </nav>
<?php endif; ?>

</div>

<?= $this->endSection() ?>
