<div class="card border-0 shadow-lg rounded-4">
    <div class="card-body p-4 p-md-5">

        <h5 class="mb-4 fw-bold text-dark">
            <i class="fa-solid fa-file-signature me-2 text-success"></i>
            {{ isset($ata) ? __('Editar Ata') : __('Nova Ata') }}
        </h5>

        <div class="row g-4">

            <div class="col-md-3">
                <label class="form-label">{{ __('Número:') }}</label>
                <input type="text" name="nr_ata" maxlength="50" class="form-control"
                       value="{{ old('nr_ata', $ata->nr_ata ?? '') }}" required>
            </div>

            <div class="col-md-3">
                <label class="form-label">{{ __('Processo N°:') }}</label>
                <input type="text" name="nr_processo" maxlength="50" class="form-control"
                       value="{{ old('nr_processo', $ata->nr_processo ?? '') }}" required>
            </div>

            <div class="col-md-3">
                <label class="form-label">{{ __('Data:') }}</label>
                <input type="date" name="dt_data_ata" class="form-control"
                       value="{{ old('dt_data_ata', $ata->dt_data_ata ?? '') }}" required>
            </div>

            <div class="col-md-3">
                <label class="form-label">{{ __('Vigência:') }}</label>
                <input type="date" name="dt_vigencia_ata" class="form-control"
                       value="{{ old('dt_vigencia_ata', $ata->dt_vigencia_ata ?? '') }}" required>
            </div>

            <div class="col-md-12 position-relative">
                <label class="form-label">{{ __('Licitação vinculada:') }}</label>
                <input type="hidden"
                       name="licitacao_procedimento_id"
                       id="licitacao_procedimento_id"
                       value="{{ old('licitacao_procedimento_id', $ata->licitacao_procedimento_id ?? '') }}">
                <input type="text"
                       class="form-control"
                       id="licitacao_search"
                       data-search-url="{{ route('admin.atas.searchLicitacoes') }}"
                       data-empty-message="{{ __('Nenhum resultado encontrado.') }}"
                       placeholder="{{ __('Nº do processo, nº da licitação ou trecho do objeto (mín. 2 caracteres)') }}"
                       autocomplete="off"
                       value="{{ $licitacaoSelecionada['text'] ?? '' }}">
                <div id="licitacao_search_results" class="list-group position-absolute w-100 mt-1 d-none z-3"></div>
                <small class="text-muted d-block mt-2">
                    <i class="fa-solid fa-link me-1"></i>
                    {{ __('Vincule esta ata a uma licitação para criar acesso direto na página pública.') }}
                </small>
            </div>

            <div class="col-12">
                <label class="form-label">{{ __('Objeto:') }}</label>
                <textarea name="ds_objeto" rows="3" maxlength="100" class="form-control"
                          required>{{ old('ds_objeto', $ata->ds_objeto ?? '') }}</textarea>
            </div>

            <div class="col-md-4">
                <label class="form-label">{{ __('BO N°:') }}</label>
                <select name="id_bo" id="id_bo" class="form-select" required>
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach($boletins as $boletim)
                        <option value="{{ $boletim->id }}"
                                data-date="{{ !empty($boletim->publicationDate) ? \Carbon\Carbon::parse($boletim->publicationDate)->format('Y-m-d') : '' }}"
                                data-link="{{ $boletim->url }}"
                                @selected(old('id_bo', $ata->id_bo ?? '') == $boletim->id)>
                            {{ $boletim->number }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">{{ __('BO Data:') }}</label>
                <input type="text" name="dt_bo" id="dt_bo" class="form-control bg-light"
                       value="{{ old('dt_bo', $ata->dt_bo ?? '') }}" readonly>
            </div>

            <div class="col-md-4">
                <label class="form-label">{{ __('BO Link:') }}</label>
                <input type="text" name="ds_link_bo" id="ds_link_bo" class="form-control bg-light"
                       value="{{ old('ds_link_bo', $ata->ds_link_bo ?? '') }}" readonly>
            </div>

            <div class="col-md-6">
                <label class="form-label">{{ __('Secretaria:') }}</label>
                @php
                    $secretariasSelecionadas = collect($secretariasSelecionadas ?? [])
                        ->filter(fn ($value) => !empty($value))
                        ->map(fn ($value) => (string) $value)
                        ->values();
                    if ($secretariasSelecionadas->isEmpty()) {
                        $secretariasSelecionadas = collect(['']);
                    }
                @endphp

                <div id="secretarias_container" class="d-flex flex-column gap-2">
                    @foreach($secretariasSelecionadas as $index => $secretariaSelecionada)
                        <div class="input-group secretaria-item">
                            <select name="id_secretaria_ata[]" class="form-select secretaria-select" @if($index === 0) required @endif>
                                <option value="">{{ __('Selecione') }}</option>
                                @foreach($secretarias as $secretaria)
                                    <option value="{{ $secretaria->id }}"
                                            data-sigla="{{ $secretaria->abbreviation }}"
                                            @selected((string) $secretariaSelecionada === (string) $secretaria->id)>
                                        {{ $secretaria->name }}
                                    </option>
                                @endforeach
                            </select>
                            @if($index > 0)
                                <button type="button" class="btn btn-outline-danger remove-secretaria">
                                    <i class="fa-solid fa-trash"></i>
                                </button>
                            @endif
                        </div>
                    @endforeach
                </div>

                <button type="button" id="add_secretaria_btn" class="btn btn-outline-primary btn-sm mt-2">
                    <i class="fa-solid fa-plus me-1"></i>{{ __('Adicionar secretaria') }}
                </button>
            </div>

            <div class="col-md-6">
                <label class="form-label">{{ __('Sigla:') }}</label>
                <input type="text" name="ds_sigla" id="ds_sigla" class="form-control bg-light"
                       value="{{ old('ds_sigla', $ata->ds_sigla ?? '') }}" readonly>
            </div>

            <div class="col-md-12">
                <label class="form-label">{{ __('Ata (PDF):') }}</label>
                <input type="file" name="nm_arquivo_ata" class="form-control"
                       accept="application/pdf" @if(!isset($ata)) required @endif>

                <small class="text-muted d-block mt-2">
                    <i class="fa-solid fa-circle-info me-1"></i>
                    {{ __('Somente PDF (máx. 10 MB)') }}
                </small>

                @if(!empty($ata?->nm_arquivo_ata))
                    <a href="{{ $ata->public_arquivo_ata_url }}"
                       target="_blank"
                       rel="noopener noreferrer"
                       class="btn btn-outline-primary btn-sm mt-3">
                        <i class="fa-solid fa-file-pdf me-1"></i> {{ __('Ver arquivo atual') }}
                    </a>
                @endif
            </div>

        </div>

        <div class="d-flex justify-content-end mt-5">
            <button class="btn btn-success px-5 py-2">
                <i class="fa-solid fa-check me-2"></i>{{ __('Salvar') }}
            </button>
        </div>

    </div>
</div>

@push('scripts')
<script src="{{ asset('pmar/js/admin/atas-form.js') }}?v={{ @filemtime(public_path('pmar/js/admin/atas-form.js')) ?: time() }}"></script>
@endpush
