<div class="role-edit">
    <x-title>
        Editing Role: {{ $role->name }}

        <x-slot:buttons>
            <a wire:navigate href="{{ route('admin.roles.show', $role) }}"
               class="btn btn-outline-secondary waves-effect">
                <i class="mdi mdi-eye me-2"></i>
                View Role
            </a>
        </x-slot:buttons>
    </x-title>

    @if(session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    <div class="row">
        {{-- MAIN --}}
        <div class="col-12 col-md-9">
            <div class="card">
                <div class="card-body">

                    {{-- ROLE NAME --}}
                    <div class="mb-4">
                        <label class="form-label">
                            Role Name
                        </label>

                        <input type="text"
                               class="form-control"
                               wire:model.defer="name"
                                @disabled($role->is_system ?? false)>

                        @if($role->is_system ?? false)
                            <div class="form-text text-warning">
                                System roles cannot be renamed.
                            </div>
                        @endif

                        @error('name')
                        <div class="text-danger small">{{ $message }}</div>
                        @enderror
                    </div>

                    {{-- PERMISSIONS --}}
                    <div class="d-flex align-items-center justify-content-between mb-3">
                        <h4 class="mb-0">Permissions</h4>
                        <div class="d-flex align-items-center gap-2">
                            <select class="form-select form-select-sm w-auto" wire:model.live="filterGroup">
                                <option value="">All Groups</option>
                                @foreach($allGroups as $group)
                                    <option value="{{ $group }}">{{ $group }}</option>
                                @endforeach
                            </select>
                            @if($filterGroup)
                                <button type="button" class="btn btn-sm btn-outline-secondary" wire:click="$set('filterGroup', '')">
                                    Clear
                                </button>
                            @endif
                        </div>
                    </div>

                    @foreach($permissionGroups as $group => $permissions)
                        <div class="card mb-3 border">
                            <div class="card-header bg-light fw-semibold">
                                {{ $group }}
                            </div>

                            <div class="card-body">
                                @foreach($permissions as $permission)
                                    <div class="form-check mb-2">
                                        <input class="form-check-input"
                                               type="checkbox"
                                               wire:model.defer="permissions"
                                               value="{{ $permission->name }}"
                                               id="perm_{{ $permission->id }}">

                                        <label class="form-check-label"
                                               for="perm_{{ $permission->id }}">
                                            <strong>{{ $permission->label }}</strong>

                                            <div class="text-muted small">
                                                {{ $permission->description }}
                                            </div>

                                            <code class="small text-muted">
                                                {{ $permission->name }}
                                            </code>
                                        </label>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    @endforeach

                    {{-- ACTIONS --}}
                    <div class="d-flex justify-content-end mt-4">
                        <button class="btn btn-primary"
                                wire:click="save">
                            Save Changes
                        </button>
                    </div>

                </div>
            </div>
        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">
                        Editing Role
                    </h4>

                    <hr>

                    <p class="text-muted small mb-2">
                        You are editing:
                    </p>

                    <p class="fw-semibold mb-3">
                        {{ $role->name }}
                    </p>

                    @if($role->is_system ?? false)
                        <div class="alert alert-warning small mb-0">
                            This is a system role. Renaming is disabled and
                            permission changes should be reviewed carefully.
                        </div>
                    @else
                        <p class="text-muted small mb-0">
                            Changes apply immediately to all users with this role.
                        </p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
