@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: " . __("Atualizar Usuário"))
@section('content')

    <div class="container mt-5">
        <div class="row">
            <div class="col-12">
                <div class="card shadow">

                    @php
                        $headLine = "<i class='fa-solid fa-user-gear'></i> Atualizar Usuário";
                        $filterQueryString = http_build_query(array_filter(
                            \App\Http\Resources\Accounts\AccountResource::listFilterQueryParams(request())
                        ));
                        $backUrl = route('admin.contas.index') . ($filterQueryString ? '?' . $filterQueryString : '');
                    @endphp

                    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                        <h3 class="my-2 fw-bolder">{!! __($headLine) !!}</h3>
                        <div class="d-flex">
                            <button class="btn btn-light btn-sm me-2" type="button"
                                    onclick="window.location.href ='{{ $backUrl }}'">
                                <i class="fa-solid fa-arrow-left"></i> {{ __("Voltar") }}
                            </button>
                        </div>
                    </div>

                    <div class="card-body">

                        <form action="{{ route('admin.contas.update', $user->id) }}" method="post" enctype="multipart/form-data">
                            @include("errors.messageFlash")
                            @csrf
                            @method('patch')

                            {{-- Preservar filtros e parâmetros de paginação --}}
                            @include('components.contas._accountListFilterHiddenFields')


                            <div class="row">
                                <div class="col-sm-12 col-md-6 d-flex align-items-center justify-content-center">
                                    <div class="mb-3">
                                        @include('components.tables._openPhoto', [
                                            'asset' => $user->photo,
                                            'alt' => $user->name,
                                            'title' => $user->name,
                                            'text' => 'Visualizar foto',
                                            'index' => 1,
                                            'style' => "width: 200px;",
                                        ])
                                    </div>
                                </div>
                                <div class="col-sm-12 col-md-6 d-flex align-items-end">
                                    <div style="width: 100%">
                                        <!-- Campo Foto -->
                                        @include('components.inputs._inputType',[
                                            "for" => null,
                                            "label" => "Foto",
                                            "name" => "photo",
                                            "id" => null,
                                            "required" => false,
                                            "type" => 'file',
                                            "old" => null,
                                            "oninput" => null,
                                            "readonly" => true,
                                            "small" => null,
                                            "accept" => 'image/jpeg,image/jpg,image/png,image/gif',
                                        ])
                                    </div>
                                </div>
                            </div>
                            <hr/>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <!-- Campo Nome completo -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Nome completo",
                                        "name" => "fullName",
                                        "id" => null,
                                        "required" => true,
                                        "type" => null,
                                        "old" => old('fullName', $user->name),
                                        "oninput" => "javascript:this.value = this.value.toLocaleUpperCase('pt-BR').replace(/[^\p{L}\s]+/gu, '').replace(/\s{2,}/g,' ').trimStart()",
                                        "readonly" => false,
                                        "small" => null,
                                    ])
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <!-- Campo Email -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Email",
                                        "name" => "email",
                                        "id" => null,
                                        "required" => false,
                                        "type" => 'email',
                                        "old" => old('email', $user->email),
                                        "oninput" => "javascript:this.value = this.value.toLowerCase()",
                                        "readonly" => false,
                                        "small" => null,
                                        "placeholder" => "nome@email.com",
                                    ])
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-4">
                                    <!-- Campo CPF -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "CPF",
                                        "name" => "cpf",
                                        "id" => null,
                                        "required" => true,
                                        "type" => null,
                                        "old" => old('cpf', $user->cpf),
                                        "oninput" => "javascript:aplicarMascaraCPF(this)",
                                        "readonly" => false,
                                        "small" => null,
                                    ])
                                </div>
                                <div class="col-sm-12 col-md-4">
                                    <!-- Campo Matrícula -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Matrícula",
                                        "name" => "matriculation",
                                        "id" => null,
                                        "required" => false,
                                        "type" => null,
                                        "old" => old('matriculation', $user->matriculation),
                                        "oninput" => "javascript:this.value = this.value.replace(/\D/g, '')",
                                        "readonly" => false,
                                        "small" => null,
                                    ])
                                </div>
                                <div class="col-sm-12 col-md-4">
                                    <!-- Campo Telefone -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Telefone",
                                        "name" => "phoneNumber",
                                        "id" => null,
                                        "required" => true,
                                        "type" => null,
                                        "old" => old('phoneNumber', $user->phoneNumber),
                                        "oninput" => "javascript:aplicarMascaraTelefone(this)",
                                        "readonly" => false,
                                        "small" => null,
                                    ])
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <!-- Campo Secretaria -->
                                    @include('components.inputs._selectType', [
                                        "for" => "structure_id",
                                        "label" => "Pertence a",
                                        "name" => "structure_id",
                                        "id" => "structureID",
                                        "required" => false,
                                        "readonly" => false,
                                        "placeholder" => "Selecione uma secretaria...",
                                        "options" => $secretarias->mapWithKeys(fn($s) => [
                                            $s->id => "{$s->name} ({$s->abbreviation})"
                                        ])->toArray(),
                                        "selected" => old('structure_id', $user->structure_id ?? ''),
                                        "small" => "Opcional. Selecione a secretaria à qual o usuário pertence.",
                                    ])
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    @include('components.inputs._selectType', [
                                        "for" => "status",
                                        "label" => "Status",
                                        "name" => "status",
                                        "required" => true,
                                        "readonly" => false,
                                        "options" => collect(App\Enums\StatusEnum::cases())
                                            ->mapWithKeys(fn($status) => [$status->name => $status->value])
                                            ->toArray(),
                                        "selected" => old('status', $user->status ?? ''),
                                        "placeholder" => "Selecione o status...",
                                        "small" => "Informe o status atual do usuário.",
                                        "onchange" => null
                                    ])
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    @include('components.inputs._unidadesSelector', [
                                        'unidades'         => $unidades,
                                        'selectedUnidades' => old('unidades', $userUnidades ?? []),
                                    ])
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    @include('components.inputs._empresasSelector', [
                                        'empresas'         => $empresas,
                                        'selectedEmpresas' => old('empresas', $userEmpresas ?? []),
                                    ])
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    <div class="form-check mb-3">
                                        <input class="form-check-input" type="checkbox" name="resetPasswordCheckbox" onclick="javascript:resetPasswordUser(this)">
                                        <label class="form-check-label" for="resetPasswordCheckbox">{{ __("Deseja redefinir a senha deste usuário para a senha padrão?") }}</label>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="d-none" id="resetPasswordCheckbox">
                                    <!-- Campo Senha -->
                                        @include('components.inputs._inputType',[
                                            "for" => null,
                                            "label" => "Senha",
                                            "name" => "password",
                                            "id" => null,
                                            "required" => true,
                                            "type" => 'password',
                                            "old" => old('password', 'mudar123'),
                                            "oninput" => null,
                                            "readonly" => true,
                                            "small" => '* A senha padrão é: <strong>mudar123</strong><br>',
                                            "inputGroup" => false,
                                        ])
                                    </div>
                                </div>
                            </div>

                            <div class="md-3">
                                <div class="list-group mb-3">
                                    <a class="list-group-item list-group-item-action active" aria-current="true">
                                        <h5 class="mb-1">{{ __("* Permissão") }}</h5>
                                        <small onclick="javascript:selectAllCheckboxCategoriasNoticias(this)" class="text-warning" style="cursor: pointer">{{ __('* selecionar todas as categorias') }}</small>
                                    </a>
                                    <div class="list-group-item list-group-item-action">
                                        @include('components.contas._permissionsMatrix', [
                                            'mode' => 'edit',
                                            'permissoesDisponiveis' => \App\Enums\StatusPermission::cases(),
                                            'permissoesUsuario' => $permissoes,
                                        ])
                                    </div>
                                </div>
                            </div>
                            @php
                                $filterQueryString = http_build_query(array_filter(
                                    \App\Http\Resources\Accounts\AccountResource::listFilterQueryParams(request())
                                ));
                                $backUrl = route('admin.contas.index') . ($filterQueryString ? '?' . $filterQueryString : '');
                            @endphp
                            <div class="d-flex justify-content-between mt-4">
                                <a href="{{ $backUrl }}" class="btn btn-lg btn-secondary">
                                    <i class="fa-solid fa-arrow-left me-1"></i> {{ __('Voltar') }}
                                </a>
                                <button type="submit" class="btn btn-lg btn-dark">
                                    {{ __('Atualizar') }}
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

