@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Editar Unidade Organizacional'))

@section('content')
    @include('errors.messageFlash')

    <div class="card shadow-sm">

        @php
            $headLine = "<i class='fa-solid fa-sitemap'></i> " . __('Editar Unidade Organizacional');
            $filterParams      = request()->only(['q', 'structure_id', 'status', 'page']);
            $filterQueryString = http_build_query(array_filter($filterParams));
            $backUrl           = route('admin.unidadesOrganizacionais.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 method="post"
                  action="{{ route('admin.unidadesOrganizacionais.update', $unidade->id) }}">
                @csrf
                @method('patch')

                @foreach($filterParams as $key => $value)
                    @if($value)
                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                    @endif
                @endforeach

                <div class="row">
                    <div class="col-sm-12 col-md-6">
                        @include('components.inputs._selectType', [
                            'for'         => 'structure_id',
                            'label'       => __('Secretaria'),
                            'name'        => 'structure_id',
                            'id'          => 'structureSelect',
                            'required'    => false,
                            'readonly'    => false,
                            'placeholder' => __('Selecione a secretaria (opcional)...'),
                            'options'     => $secretarias->mapWithKeys(
                                fn($s) => [$s->id => "{$s->abbreviation} — {$s->name}"]
                            )->toArray(),
                            'selected'    => old('structure_id', $unidade->structure_id),
                            'small'       => __('Opcional. Secretaria à qual esta unidade pertence.'),
                        ])
                    </div>
                    <div class="col-sm-12 col-md-6">
                        @include('components.inputs._inputType', [
                            'for'      => null,
                            'label'    => __('Sigla'),
                            'name'     => 'abbreviation',
                            'id'       => null,
                            'required' => true,
                            'type'     => null,
                            'old'      => old('abbreviation', $unidade->abbreviation),
                            'oninput'  => "javascript:this.value = this.value.toUpperCase().replace(/[^A-Z0-9\/\-]/g, '')",
                            'readonly' => false,
                            'small'    => __('Ex.: CTSIS, DTIC, SADT. Será convertida para maiúsculas automaticamente.'),
                        ])
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12 col-md-8">
                        @include('components.inputs._inputType', [
                            'for'      => null,
                            'label'    => __('Nome da Unidade'),
                            'name'     => 'name',
                            'id'       => null,
                            'required' => true,
                            'type'     => null,
                            'old'      => old('name', $unidade->name),
                            'oninput'  => null,
                            'readonly' => false,
                            'small'    => __('Ex.: Coordenação Técnica de Sistemas Informatizados.'),
                        ])
                    </div>
                    <div class="col-sm-12 col-md-4">
                        @include('components.inputs._selectType', [
                            'for'         => 'status',
                            'label'       => __('Status'),
                            'name'        => 'status',
                            'required'    => true,
                            'readonly'    => false,
                            'placeholder' => __('Selecione o status...'),
                            'options'     => collect(App\Enums\StatusEnum::cases())
                                ->mapWithKeys(fn($s) => [$s->name => $s->value])
                                ->toArray(),
                            'selected'    => old('status', $unidade->status),
                            'small'       => null,
                        ])
                    </div>
                </div>

                <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>
@endsection
