@extends('layouts.app')

@section('title', 'Roles')

@section('content')
    <!-- [ page-header ] start -->
    <div class="page-header">
        <div class="page-header-left d-flex align-items-center">
            <div class="page-header-title">
                <h5 class="m-b-10">Roles</h5>
            </div>
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('index') }}">Home</a></li>
                <li class="breadcrumb-item">Roles</li>
            </ul>
        </div>
        <div class="page-header-right ms-auto">
            <div class="page-header-right-items">
                <div class="d-flex d-md-none">
                    <a href="javascript:void(0)" class="page-header-right-close-toggle">
                        <i class="feather-arrow-left me-2"></i>
                        <span>Back</span>
                    </a>
                </div>
                <div class="d-flex align-items-center gap-2 page-header-right-items-wrapper">
                
                    <div class="dropdown">
                        <a class="btn btn-icon btn-light-brand" data-bs-toggle="dropdown" data-bs-offset="0, 10"
                            data-bs-auto-close="outside">
                            <i class="feather-paperclip"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end">
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-pdf me-3"></i>
                                <span>PDF</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-csv me-3"></i>
                                <span>CSV</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-xml me-3"></i>
                                <span>XML</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-txt me-3"></i>
                                <span>Text</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-exe me-3"></i>
                                <span>Excel</span>
                            </a>
                            <div class="dropdown-divider"></div>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-printer me-3"></i>
                                <span>Print</span>
                            </a>
                        </div>
                    </div>
                    @can('create_role')
                    <a href="{{ route('roles.create') }}" class="btn btn-primary">
                        <i class="feather-plus me-2"></i>
                        <span>Create Role</span>
                    </a>
                    @endcan
                </div>
            </div>
            <div class="d-md-none d-flex align-items-center">
                <a href="javascript:void(0)" class="page-header-right-open-toggle">
                    <i class="feather-align-right fs-20"></i>
                </a>
            </div>
        </div>
    </div>
    <div class="main-content">
        <div class="row">
            <div class="col-lg-12">
                <div class="card stretch stretch-full">
                    <div class="card-body p-0">
                        <x-server-list-toolbar form-id="roleFilters" apply-label="Apply Filters" reset-label="Reset Filters"
                            actions-class="w-100 justify-content-center" placeholder="Search roles by name" :filters="[
                                'assignment'=>['label'=>'User assignment','options'=>['assigned'=>'Assigned to users','unassigned'=>'Not assigned']],
                                'permission_id'=>['label'=>'Permission','options'=>$permissions->pluck('name','id')->all()],
                                'guard_name'=>['label'=>'Guard','options'=>$guards->mapWithKeys(fn($guard)=>[$guard=>$guard])->all()],
                            ]" />
                        <div class="table-responsive">
                            <table class="table table-hover" id="roleServerList">
                                <thead>
                                    <tr>
                                        <th class="wd-30">
                                            <div class="btn-group mb-1">
                                                <div class="custom-control custom-checkbox ms-1">
                                                    <input type="checkbox" class="custom-control-input"
                                                        id="checkAllCustomer">
                                                    <label class="custom-control-label" for="checkAllCustomer"></label>
                                                </div>
                                            </div>
                                        </th>
                                        <th>Role</th>
                                        <!-- <th>Permission</th> -->
                                        <th>User Count</th>
                                        <th>Permission Count</th>
                                        <th class="text-end">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($roles as $role)
                                    <tr>
                                        <td>{{ $role->id }}</td>
                                        <td>{{ $role->name }}</td>
                                        <!-- <td>
                                            @foreach($role->permissions as $permission)
                                                <span class="badge bg-secondary me-1">{{ $permission->name }}</span>
                                            @endforeach
                                        </td> -->
                                        <td>{{ $role->users_count }}</td>
                                        <td>{{ $role->permissions_count }}</td>
                                        <td>
                                            <div class="hstack gap-2 justify-content-end role-row-actions">
                                                <a href="{{ route('roles.show', $role) }}"
                                                    class="avatar-text avatar-md bg-soft-primary text-primary"
                                                    title="View Role" aria-label="View Role">
                                                    <i class="feather feather-eye"></i>
                                                </a>
                                                @can('update_role')
                                                    <a href="{{ route('roles.edit', $role) }}" class="avatar-text avatar-md bg-soft-warning text-warning"
                                                        title="Edit Role" aria-label="Edit Role"><i class="feather feather-edit-3"></i></a>
                                                @endcan
                                                @can('delete_role')
                                                    @if($role->users_count === 0)
                                                    <form action="{{ route('roles.destroy', $role) }}" method="POST" class="d-inline-flex m-0"
                                                        onsubmit="return confirm('Are you sure you want to delete this Role?');">
                                                        @csrf @method('DELETE')
                                                        <button type="submit" class="avatar-text avatar-md border-0 bg-soft-danger text-danger"
                                                            title="Delete Role" aria-label="Delete Role"><i class="feather feather-trash-2"></i></button>
                                                    </form>
                                                    @endif
                                                @endcan
                                            </div>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>

                        </div>
                    </div>
                    <div class="card-footer d-flex flex-wrap align-items-center justify-content-between gap-2">
                        <small class="text-muted">Showing {{ $roles->firstItem() ?? 0 }}–{{ $roles->lastItem() ?? 0 }} of {{ $roles->total() }} roles</small>
                        @if($roles->hasPages())
                            {{ $roles->links() }}
                        @else
                            <nav aria-label="Role pagination"><ul class="pagination pagination-sm mb-0">
                                <li class="page-item disabled"><span class="page-link">‹</span></li>
                                <li class="page-item active"><span class="page-link">1</span></li>
                                <li class="page-item disabled"><span class="page-link">›</span></li>
                            </ul></nav>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- [ Main Content ] end -->
@endsection
