@extends('layouts.app')
@section('title', $user->exists ? 'Editar utilizador' : 'Novo utilizador')
@section('content')
    <div class="page-heading"><div><span class="eyebrow">ADMINISTRAÇÃO · UTILIZADORES</span><h1>{{ $user->exists ? 'Editar '.$user->name : 'Criar utilizador' }}</h1><p>O acesso efectivo resulta da combinação entre unidade, departamento e perfis.</p></div><a class="secondary-link" href="{{ route('admin.users.index') }}">← Voltar</a></div>
    @if($errors->any())<div class="alert danger">{{ $errors->first() }}</div>@endif
    <form class="panel management-form" method="POST" action="{{ $user->exists ? route('admin.users.update', $user) : route('admin.users.store') }}">@csrf @if($user->exists)@method('PUT')@endif
        <div class="form-section"><div><span class="eyebrow">IDENTIDADE</span><h2>Conta institucional</h2><p>Dados utilizados para identificar o colaborador.</p></div><div class="form-grid">
            <label class="field span-2"><span>Nome completo</span><input name="name" value="{{ old('name', $user->name) }}" required></label><label class="field"><span>Nome de utilizador</span><input name="username" value="{{ old('username', $user->username) }}" required placeholder="nome.apelido"></label><label class="field"><span>Email</span><input type="email" name="email" value="{{ old('email', $user->email) }}" required></label>
            <label class="field"><span>{{ $user->exists ? 'Nova palavra-passe (opcional)' : 'Palavra-passe temporária' }}</span><input type="password" name="password" {{ $user->exists ? '' : 'required' }} autocomplete="new-password"></label><label class="field"><span>Confirmar palavra-passe</span><input type="password" name="password_confirmation" {{ $user->exists ? '' : 'required' }} autocomplete="new-password"></label>
        </div></div>
        <div class="form-section"><div><span class="eyebrow">AFECTAÇÃO</span><h2>Contexto de trabalho</h2><p>“Todas as filiais” concede acesso global conforme as permissões.</p></div><div class="form-grid">
            <label class="field"><span>Organização</span><select name="organization_id" required>@foreach($organizations as $organization)<option value="{{ $organization->id }}" @selected(old('organization_id', $assignment?->organization_id ?: $organizations->first()?->id) == $organization->id)>{{ $organization->name }}</option>@endforeach</select></label>
            <label class="field"><span>Filial</span><select name="branch_id"><option value="">Todas as filiais / acesso global</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected(old('branch_id', $assignment?->branch_id) == $branch->id)>{{ $branch->name }}</option>@endforeach</select></label>
            <label class="field span-2"><span>Departamento/unidade</span><select name="department_id"><option value="">Sem departamento específico</option>@foreach($departments as $department)<option value="{{ $department->id }}" @selected(old('department_id', $assignment?->department_id) == $department->id)>{{ $department->name }}</option>@endforeach</select></label>
        </div></div>
        <div class="form-section"><div><span class="eyebrow">AUTORIZAÇÃO</span><h2>Perfis atribuídos</h2><p>Pode combinar um perfil operacional com um administrativo.</p></div><div class="choice-grid">@foreach($roles as $role)<label class="choice-card"><input type="checkbox" name="role_ids[]" value="{{ $role->id }}" @checked(in_array($role->id, old('role_ids', $selectedRoleIds)))><span class="role-mark {{ $role->is_privileged ? 'privileged' : '' }}">{{ $role->is_privileged ? '◆' : '◇' }}</span><div><strong>{{ $role->name }}</strong><small>{{ $role->is_privileged ? 'Disponível no Modo de Administração' : 'Perfil operacional' }}</small></div></label>@endforeach</div></div>
        <div class="form-options"><label><input type="checkbox" name="is_active" value="1" @checked(old('is_active', $user->exists ? $user->is_active : true))> Conta activa</label><span>A reposição da palavra-passe obrigará nova alteração no próximo acesso.</span></div><div class="form-actions"><a class="secondary-link" href="{{ route('admin.users.index') }}">Cancelar</a><button class="primary-button compact" type="submit">Guardar utilizador</button></div>
    </form>
@endsection
