@extends('layouts.app')
@section('title', 'Edit Role')
@section('content')
<x-ui.form-shell
    :title="$role->display_name"
    description="Maintain this custom role within your own delegation ceiling. System roles cannot be edited here."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Roles & Permissions','url'=>route('admin.roles.index')],['label'=>$role->display_name]]"
>
    <form method="POST" action="{{ route('admin.roles.update',$role) }}" class="mf-form">
        @csrf
        @method('PUT')

        <x-ui.form-section title="Role identity" description="The permanent role identifier is shown for reference. Update only the human-facing name or description.">
            <x-ui.form-grid>
                <div class="mf-col-4">
                    <label class="mf-field-label">Display name</label>
                    <input name="display_name" value="{{ old('display_name',$role->display_name) }}" class="mf-input" required>
                    @error('display_name')<p class="mf-field-error">{{ $message }}</p>@enderror
                </div>
                <div class="mf-col-4">
                    <label class="mf-field-label">Role identifier</label>
                    <input value="{{ $role->name }}" class="mf-input" disabled>
                    <p class="mf-field-help">Identifiers are immutable after role creation.</p>
                </div>
                <div class="mf-col-4">
                    <label class="mf-field-label">Description</label>
                    <input name="description" value="{{ old('description',$role->description) }}" class="mf-input">
                    @error('description')<p class="mf-field-error">{{ $message }}</p>@enderror
                </div>
            </x-ui.form-grid>
        </x-ui.form-section>

        @foreach($permissionGroups as $group=>$permissions)
            <x-ui.form-section :title="ucwords(str_replace(['_','-'],' ',$group))" description="Only permissions within your own delegation ceiling are available.">
                <div class="mf-option-grid">
                    @foreach($permissions as $permission)
                        <label class="mf-option">
                            <input type="checkbox" name="permission_ids[]" value="{{ $permission->id }}" @checked(in_array($permission->id, old('permission_ids',$role->permissions->pluck('id')->all())))>
                            <span>
                                <span class="block font-medium">{{ $permission->display_name }}</span>
                                <span class="block text-xs text-muted-foreground">{{ $permission->name }}</span>
                            </span>
                        </label>
                    @endforeach
                </div>
            </x-ui.form-section>
        @endforeach

        <x-ui.form-actions>
            <a class="mf-btn" href="{{ route('admin.roles.index') }}">Cancel</a>
            <button class="mf-btn mf-btn-primary">Save role</button>
        </x-ui.form-actions>
    </form>
</x-ui.form-shell>
@endsection
