@extends('layouts.app')

@section('title', 'Roles')
@section('heading', 'Roles & permissions')

@section('content')
@include('settings.partials.back-to-home')
    <x-card>
        <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
            <div>
                <p class="text-slate-600">System roles sync with <code class="rounded bg-slate-100 px-1 text-xs">php artisan hrms:reset-permissions</code>. Custom roles are fully editable.</p>
            </div>
            @if(auth()->user()->hasPermission('roles_create'))
                <a href="{{ route('tenant-roles.create') }}" class="inline-flex items-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700"><i class="bi bi-plus-lg me-1"></i>Add custom role</a>
            @endif
        </div>
        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Name</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Slug</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Type</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Permissions</th>
                    <th class="px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Actions</th>
                </tr>
            </x-slot>
            @forelse($roles as $r)
                @php
                    $system = in_array($r->name, \App\Services\TenantRoleService::RESERVED_ROLE_NAMES, true);
                @endphp
                <tr>
                    <td class="whitespace-nowrap px-4 py-3 text-sm font-medium text-slate-900">{{ $r->display_name }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $r->name }}</td>
                    <td class="px-4 py-3 text-sm">
                        @if($system)
                            <span class="rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-700">System</span>
                        @else
                            <span class="rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-800">Custom</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $r->permissions_count }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-right text-sm">
                        @if(auth()->user()->hasPermission('roles_edit'))
                            <a href="{{ route('tenant-roles.edit', $r) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        @endif
                        @if(!$system && auth()->user()->hasPermission('roles_delete')) <form action="{{ route('tenant-roles.destroy', $r) }}" method="POST" class="inline" onsubmit="return confirm('Delete this role?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn-icon-action btn-icon-action--delete" title="Delete" aria-label="Delete"><i class="bi bi-trash3"></i></button>
                            </form>
                        @endif
                    </td>
                </tr>
            @empty
                <tr><td colspan="5" class="px-4 py-8 text-center text-sm text-slate-500">No roles.</td></tr>
            @endforelse
        </x-table>
        @if($roles->hasPages())<div class="mt-4">{{ $roles->links() }}</div>@endif
    </x-card>
@endsection
