@extends('layouts.metor.admin')
@section('content')
@section('title','Roles')

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex align-items-center justify-content-between">
                <div>
                    <h4 class="card-title mb-0">Roles</h4>
                </div>
                <div>
                    @can('roles-create')
                    <a href="{{ route('roles.create') }}" class='btn btn-primary'>
                        <i class="ri-add-line me-1"></i> Add Roles
                    </a>
                    @endcan
                </div>
            </div>
            <div class="card-body">
                <!-- Search Form -->
                <form method="GET" action="{{ route('roles.index') }}" class="mb-3">
                    <div class="row g-2">
                        <div class="col-md-4">
                            <input type="text" 
                                   name="search" 
                                   class="form-control" 
                                   placeholder="Search by name or guard name..." 
                                   value="{{ request('search') }}">
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="ri-search-line"></i> Search
                            </button>
                        </div>
                        @if(request('search'))
                            <div class="col-md-2">
                                <a href="{{ route('roles.index') }}" class="btn btn-secondary w-100">
                                    <i class="ri-close-line"></i> Clear
                                </a>
                            </div>
                        @endif
                    </div>
                    <!-- Preserve sort parameters -->
                    @if(request('sort_by'))
                        <input type="hidden" name="sort_by" value="{{ request('sort_by') }}">
                    @endif
                    @if(request('sort_order'))
                        <input type="hidden" name="sort_order" value="{{ request('sort_order') }}">
                    @endif
                </form>

                <div class="js-index-cards d-none"></div>

                <div class="table-responsive js-index-table d-none d-md-block">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th style='width:5%'>
                                    <a href="{{ route('roles.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'id', 'sort_order' => request('sort_by') == 'id' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
                                       class="text-decoration-none text-dark">
                                        Sr.
                                        @if(request('sort_by') == 'id')
                                            @if(request('sort_order') == 'asc')
                                                <i class="ri-arrow-up-line"></i>
                                            @else
                                                <i class="ri-arrow-down-line"></i>
                                            @endif
                                        @endif
                                    </a>
                                </th>
                                <th>
                                    <a href="{{ route('roles.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'name', 'sort_order' => request('sort_by') == 'name' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
                                       class="text-decoration-none text-dark">
                                        Name
                                        @if(request('sort_by') == 'name' || !request('sort_by'))
                                            @if(request('sort_order') == 'desc' || !request('sort_order'))
                                                <i class="ri-arrow-down-line"></i>
                                            @else
                                                <i class="ri-arrow-up-line"></i>
                                            @endif
                                        @endif
                                    </a>
                                </th>
                                <th>
                                    <a href="{{ route('roles.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'guard_name', 'sort_order' => request('sort_by') == 'guard_name' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
                                       class="text-decoration-none text-dark">
                                        Guard Name
                                        @if(request('sort_by') == 'guard_name')
                                            @if(request('sort_order') == 'asc')
                                                <i class="ri-arrow-up-line"></i>
                                            @else
                                                <i class="ri-arrow-down-line"></i>
                                            @endif
                                        @endif
                                    </a>
                                </th>
                                <th class='text-end' style='width:20%'>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($roles as $role)
                                <tr id="row_{{ $role->id }}">
                                    <td>{{ $roles->firstItem() + $loop->index }}</td>
                                    <td>{{ $role->name }}</td>
                                    <td>{{ $role->guard_name }}</td>
                                    <td class="text-end">
                                        <div id="action_{{ $role->id }}" class="d-flex gap-1 justify-content-end">
                                            @can('roles-edit')
                                                <a href="{{ route('roles.edit', $role->id) }}" 
                                                   class="btn btn-sm btn-primary" 
                                                   title="Edit">
                                                    <i class="ri-pencil-line"></i>
                                                </a>
                                            @endcan

                                            @can('roles-delete')
                                                <a id="trash_{{ $role->id }}"
                                                   class="btn btn-sm btn-danger"
                                                   title="Move to Trash"
                                                   onclick="suSoftDelete('{{ $role->id }}', 'roles')">
                                                    <i class="ri-delete-bin-line"></i>
                                                </a>

                                                <a id="restore_{{ $role->id }}"
                                                   class="btn btn-sm btn-success"
                                                   title="Restore"
                                                   onclick="suRestore('{{ $role->id }}', 'roles')"
                                                   style="display:none;">
                                                    <i class="ri-restart-line"></i>
                                                </a>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="text-center">No roles found.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                @if($roles->hasPages())
                <div class="mt-4 d-flex justify-content-between align-items-center flex-wrap">
                    <div class="mb-2">
                        <p class="text-muted mb-0">
                            Showing {{ $roles->firstItem() ?? 0 }} to {{ $roles->lastItem() ?? 0 }} of {{ $roles->total() }} entries
                        </p>
                    </div>
                    <nav aria-label="Page navigation">
                        <ul class="pagination pagination-sm mb-0">
                            {{-- Previous Page Link --}}
                            @if ($roles->onFirstPage())
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </span>
                                </li>
                            @else
                                <li class="page-item">
                                    <a class="page-link" href="{{ $roles->appends(request()->except('page'))->previousPageUrl() }}" rel="prev">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </a>
                                </li>
                            @endif

                            {{-- Pagination Elements --}}
                            @php
                                $currentPage = $roles->currentPage();
                                $lastPage = $roles->lastPage();
                                $startPage = max(1, $currentPage - 2);
                                $endPage = min($lastPage, $currentPage + 2);
                            @endphp

                            @if($startPage > 1)
                                <li class="page-item">
                                    <a class="page-link" href="{{ $roles->appends(request()->except('page'))->url(1) }}">1</a>
                                </li>
                                @if($startPage > 2)
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                @endif
                            @endif

                            @for($page = $startPage; $page <= $endPage; $page++)
                                @if ($page == $currentPage)
                                    <li class="page-item active">
                                        <span class="page-link">{{ $page }}</span>
                                    </li>
                                @else
                                    <li class="page-item">
                                        <a class="page-link" href="{{ $roles->appends(request()->except('page'))->url($page) }}">{{ $page }}</a>
                                    </li>
                                @endif
                            @endfor

                            @if($endPage < $lastPage)
                                @if($endPage < $lastPage - 1)
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                @endif
                                <li class="page-item">
                                    <a class="page-link" href="{{ $roles->appends(request()->except('page'))->url($lastPage) }}">{{ $lastPage }}</a>
                                </li>
                            @endif

                            {{-- Next Page Link --}}
                            @if ($roles->hasMorePages())
                                <li class="page-item">
                                    <a class="page-link" href="{{ $roles->appends(request()->except('page'))->nextPageUrl() }}" rel="next">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </a>
                                </li>
                            @else
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </span>
                                </li>
                            @endif
                        </ul>
                    </nav>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>

@endsection
