@extends('layouts.app')
@section('title', 'Create Role')
@section('content')
<x-ui.form-shell
    title="Create Custom Role"
    description="Create a permission bundle for a specific responsibility. System roles remain protected and are managed by the application."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Roles & Permissions','url'=>route('admin.roles.index')],['label'=>'Create Role']]"
>
    <form method="POST" action="{{ route('admin.roles.store') }}" class="mf-form">
        @csrf

        <x-ui.form-section title="Role identity" description="The role identifier is permanent after creation; the display name can be changed later.">
            <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') }}" 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 name="name" value="{{ old('name') }}" class="mf-input" placeholder="operations-reviewer" required>
                    <p class="mf-field-help">Lowercase letters and numbers separated by dots or hyphens.</p>
                    @error('name')<p class="mf-field-error">{{ $message }}</p>@enderror
                </div>
                <div class="mf-col-4">
                    <label class="mf-field-label">Description</label>
                    <input name="description" value="{{ old('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="Grant only the capabilities required by this responsibility.">
                <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', [])))>
                            <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">Create role</button>
        </x-ui.form-actions>
    </form>
</x-ui.form-shell>
@endsection
