@model dynamic
@{
    int current = Model.CurrentPage;
    int total = Model.TotalPages;

    int start = Math.Max(1, current - 2);
    int end = Math.Min(total, current + 2);
}
<div class="mt-8">

    <nav>
        <ul class="pagination flex gap-1">

            @* Previous *@
            @if (Model.HasPrevious)
            {
                <li class="p-3 border bg-white">
                    <a href="?page=@(current - 1)">
                        Previous
                    </a>
                </li>
            }

            @* Halaman pertama *@
            @if (start > 1)
            {
                <li class="p-3 border bg-white">
                    <a href="?page=1">1</a>
                </li>

                @if (start > 2)
                {
                    <li class="p-3 border bg-white">
                        <span>...</span>
                    </li>
                }
            }


            @* Halaman sekitar aktif *@
            @for (int i = start; i <= end; i++)
            {
                <li class="page-item p-3 border rounded @(i == current ? "active bg-primary text-white" : "bg-white")">

                    <a href="?page=@i">
                        @i
                    </a>

                </li>
            }


            @* Halaman terakhir *@
            @if (end < total)
            {
                @if (end < total - 1)
                {
                    <li class="p-3 border bg-white">
                        <span>...</span>
                    </li>
                }

                <li class="p-3 border bg-white">
                    <a href="?page=@total">
                        @total
                    </a>
                </li>
            }


            @* Next *@
            @if (Model.HasNext)
            {
                <li class="p-3 border bg-white">
                    <a href="?page=@(current + 1)">
                        Next
                    </a>
                </li>
            }

        </ul>
    </nav>
</div>