<div class="user-edit">
    <x-title>
        Editing User: {{ $user->name_first }} {{ $user->name_last }}

        <x-slot:buttons>
            <a href="{{ route('admin.users.show', $user) }}"
               class="btn btn-outline-secondary">
                View Profile
            </a>
        </x-slot:buttons>
    </x-title>

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

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

                    <div class="row mb-3">
                        <div class="col-6">
                            <label class="form-label">First Name</label>
                            <input class="form-control" wire:model.defer="name_first">
                        </div>

                        <div class="col-6">
                            <label class="form-label">Last Name</label>
                            <input class="form-control" wire:model.defer="name_last">
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <label class="form-label">Username</label>
                            <input class="form-control" wire:model.defer="username">
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input class="form-control" wire:model.defer="email">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">
                            New Password
                            <span class="text-muted small">(optional)</span>
                        </label>
                        <input type="password"
                               class="form-control"
                               wire:model.defer="password">
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Roles</label>
                        <div class="d-flex flex-wrap gap-3">
                            @foreach($allRoles as $role)
                                <div class="form-check">
                                    <input class="form-check-input"
                                           type="checkbox"
                                           wire:model.defer="roles"
                                           value="{{ $role->name }}">
                                    <label class="form-check-label">
                                        {{ $role->name }}
                                    </label>
                                </div>
                            @endforeach
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Workspace Access</label>

                        <div class="form-check mb-2">
                            <input class="form-check-input"
                                   type="checkbox"
                                   id="allWorkspaces"
                                   wire:model.live="allWorkspaces">
                            <label class="form-check-label" for="allWorkspaces">
                                All workspaces (no restriction)
                            </label>
                        </div>

                        <div class="d-flex flex-wrap gap-3 ps-3" wire:ignore.self>
                            @foreach(\App\Components\Admin\Users\UserEdit::WORKSPACES as $ws)
                                <div class="form-check">
                                    <input class="form-check-input"
                                           type="checkbox"
                                           wire:model.defer="allowedWorkspaces"
                                           value="{{ $ws }}"
                                           @if($allWorkspaces) disabled @endif>
                                    <label class="form-check-label">
                                        {{ strtoupper($ws) }}
                                    </label>
                                </div>
                            @endforeach
                        </div>

                        <p class="text-muted small mt-2 mb-0">
                            When restricted, the user can only switch to and access the ticked workspaces.
                            The <code>workspaces.override</code> permission bypasses this whitelist entirely.
                        </p>
                    </div>

                    <div class="d-flex justify-content-end">
                        <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</h4>
                    <hr>

                    <p class="text-muted small mb-0">
                        Role changes apply immediately and may
                        affect system access.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
