@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('settings.title')],
        ['label' => __('rbac.users.title'), 'url' => route('settings.users.index')],
        ['label' => $targetUser->name],
    ];
@endphp

<x-ui.layout.page
    :title="$targetUser->name"
    :subtitle="__('rbac.users.edit_subtitle')"
    page-icon="fe fe-shield"
    :breadcrumbs="$breadcrumbs"
>
    @error('user')
        <div class="alert alert-danger">{{ $message }}</div>
    @enderror

    <div class="card border-0 shadow-sm mb-3">
        <div class="card-body p-4">
            <form method="POST" action="{{ route('settings.users.update', $targetUser) }}" novalidate>
                @csrf
                @method('PUT')
                <div class="row g-3">
                    <div class="col-md-6">
                        <x-ui.form.input name="name" :label="__('rbac.users.columns.name')"
                            :value="old('name', $targetUser->name)" required />
                    </div>
                    <div class="col-md-6">
                        <x-ui.form.input name="email" type="email" :label="__('rbac.users.columns.email')"
                            :value="old('email', $targetUser->email)" required />
                    </div>
                    <div class="col-md-6">
                        <x-ui.form.search-select
                            name="employee_id"
                            lookup="employees"
                            :label="__('rbac.users.columns.employee')"
                            :options="$employeeOptions"
                            :selected="old('employee_id', $targetUser->employee_id)"
                            :placeholder="__('rbac.users.placeholders.employee')"
                            optional
                        />
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">{{ __('rbac.users.status') }}</label>
                        <div>
                            <x-ui.operational.status-badge
                                :label="$targetUser->is_active ? __('common.active') : __('common.inactive')"
                                :variant="$targetUser->is_active ? 'success' : 'neutral'"
                            />
                        </div>
                    </div>
                </div>

                <div class="mt-4 mb-4">
                    <label class="form-label">{{ __('rbac.users.columns.roles') }}</label>
                    <div class="d-flex flex-column gap-2">
                        @foreach ($roles as $role)
                            @if ($targetUser->isTenantOwner())
                                @if (in_array($role->id, $assignedRoleIds, true))
                                    <input type="hidden" name="role_ids[]" value="{{ $role->id }}">
                                    <span><x-ui.operational.status-badge :label="$role->name" variant="neutral" /></span>
                                @endif
                            @else
                                <label class="d-flex align-items-center gap-2 mb-0">
                                    <input type="checkbox" name="role_ids[]" value="{{ $role->id }}"
                                        class="form-check-input @error('role_ids') is-invalid @enderror"
                                        @checked(in_array($role->id, old('role_ids', $assignedRoleIds), true))>
                                    <span>{{ $role->name }}</span>
                                </label>
                            @endif
                        @endforeach
                    </div>
                    @error('role_ids')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                    @enderror
                </div>
                <div class="d-flex justify-content-end gap-2">
                    <x-ui.buttons.secondary :href="route('settings.users.index')">{{ __('common.cancel') }}</x-ui.buttons.secondary>
                    <x-ui.buttons.primary type="submit" icon="fe fe-save">{{ __('common.save') }}</x-ui.buttons.primary>
                </div>
            </form>
        </div>
    </div>

    @if (! $targetUser->isTenantOwner() && auth()->id() !== $targetUser->id)
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-header bg-white"><strong>{{ __('rbac.users.reset_password') }}</strong></div>
            <div class="card-body">
                <form method="POST" action="{{ route('settings.users.password.update', $targetUser) }}" novalidate>
                    @csrf
                    @method('PUT')
                    <div class="row g-3 align-items-end">
                        <div class="col-md-5">
                            <x-ui.form.input name="password" type="password"
                                :label="__('rbac.users.password')" required />
                        </div>
                        <div class="col-md-5">
                            <x-ui.form.input name="password_confirmation" type="password"
                                :label="__('rbac.users.password_confirm')" required />
                        </div>
                        <div class="col-md-2 text-end">
                            <x-ui.buttons.primary type="submit">{{ __('rbac.users.reset_password') }}</x-ui.buttons.primary>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    @endif

    @if (! $targetUser->isTenantOwner() && auth()->id() !== $targetUser->id)
        <div class="text-end">
            <form method="POST" class="d-inline"
                action="{{ $targetUser->is_active
                    ? route('settings.users.deactivate', $targetUser)
                    : route('settings.users.activate', $targetUser) }}">
                @csrf
                @if ($targetUser->is_active)
                    <x-ui.buttons.danger type="submit" icon="fe fe-user-x">{{ __('rbac.users.deactivate') }}</x-ui.buttons.danger>
                @else
                    <x-ui.buttons.primary type="submit" icon="fe fe-user-check">{{ __('rbac.users.activate') }}</x-ui.buttons.primary>
                @endif
            </form>
        </div>
    @endif
</x-ui.layout.page>
