@csrf
@if(isset($registro))
    @method('put')
@endif

@foreach(['q', 'ano', 'con_modalidade', 'con_status', 'page'] as $filterKey)
    @if(request()->filled($filterKey))
        <input type="hidden" name="{{ $filterKey }}" value="{{ request($filterKey) }}">
    @endif
@endforeach

<div class="card border-0 shadow rounded-4">
    <div class="card-body p-4">
        <h5 class="mb-4 fw-semibold">
            <i class="fa-solid fa-file-contract me-2"></i>
            {{ __('Dados do Contrato') }}
        </h5>

        <div class="row g-4">
            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Número') }}</label>
                <input type="text" name="con_numero" maxlength="50" class="form-control shadow-sm" value="{{ old('con_numero', $registro->con_numero ?? '') }}">
            </div>

            <div class="col-md-2">
                <label class="form-label fw-semibold text-secondary">{{ __('Ano') }}</label>
                <input type="text" name="con_ano" maxlength="4" class="form-control shadow-sm" value="{{ old('con_ano', $registro->con_ano ?? '') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Modalidade') }}</label>
                <select class="form-select shadow-sm" name="con_modalidade">
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach(\App\Models\Contratos\Contrato::MODALIDADES as $key => $label)
                        <option value="{{ $key }}" @selected((string) old('con_modalidade', $registro->con_modalidade ?? '') === (string) $key)>{{ __($label) }}</option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label fw-semibold text-secondary">{{ __('Status') }}</label>
                <select class="form-select shadow-sm" name="con_status">
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach(\App\Models\Contratos\Contrato::STATUS as $key => $label)
                        <option value="{{ $key }}" @selected((string) old('con_status', $registro->con_status ?? '') === (string) $key)>{{ __($label) }}</option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-8">
                <label class="form-label fw-semibold text-secondary" for="con_titulo">{{ __('Título') }}</label>
                <textarea name="con_titulo" id="con_titulo" rows="3" maxlength="1024" class="form-control shadow-sm" required aria-describedby="con_titulo_counter">{{ old('con_titulo', $registro->con_titulo ?? '') }}</textarea>
                <div class="d-flex justify-content-end mt-1">
                    <small id="con_titulo_counter" class="text-muted">
                        <span id="con_titulo_count">0</span>/1024
                    </small>
                </div>
            </div>

            <div class="col-md-4">
                <label class="form-label fw-semibold text-secondary">{{ __('Processo') }}</label>
                <input type="text" name="con_processo" maxlength="50" class="form-control shadow-sm" value="{{ old('con_processo', $registro->con_processo ?? '') }}">
            </div>

            <div class="col-md-8 position-relative">
                <label class="form-label fw-semibold text-secondary">{{ __('Licitação vinculada') }}</label>
                <input type="hidden"
                       name="licitacao_procedimento_id"
                       id="licitacao_procedimento_id"
                       value="{{ old('licitacao_procedimento_id', $registro->licitacao_procedimento_id ?? '') }}">
                <input type="text"
                       class="form-control shadow-sm"
                       id="licitacao_search"
                       placeholder="{{ __('Nº do processo, nº da licitação ou trecho do objeto (mín. 2 caracteres)') }}"
                       autocomplete="off"
                       data-search-url="{{ route('admin.contratos.searchLicitacoes') }}"
                       value="{{ $licitacaoSelecionada['text'] ?? '' }}">
                <div id="licitacao_search_results" class="list-group position-absolute w-100 mt-1 d-none"></div>
                <small class="text-muted d-block mt-2">
                    <i class="fa-solid fa-link me-1"></i>
                    {{ __('Vincule este contrato a uma licitação para acesso direto no portal.') }}
                </small>
            </div>

            <div class="col-md-4">
                <label class="form-label fw-semibold text-secondary">{{ __('CNPJ do Favorecido') }}</label>
                <input type="text" name="con_favorecido" maxlength="14" class="form-control shadow-sm" value="{{ old('con_favorecido', $registro->con_favorecido ?? '') }}">
            </div>

            <div class="col-md-8">
                <label class="form-label fw-semibold text-secondary">{{ __('Nome do Favorecido') }}</label>
                <input type="text" name="fav_nome" maxlength="200" class="form-control shadow-sm" value="{{ old('fav_nome', $registro->favorecido->fav_nome ?? '') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Data de Assinatura') }}</label>
                <input type="text" name="con_dataassinatura" maxlength="10" class="form-control shadow-sm" data-mask-date value="{{ old('con_dataassinatura', $registro->con_dataassinatura ?? '') }}" placeholder="{{ __('dd/mm/aaaa') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Data Inicial') }}</label>
                <input type="text" name="con_dataini" maxlength="10" class="form-control shadow-sm" data-mask-date value="{{ old('con_dataini', $registro->con_dataini ?? '') }}" placeholder="{{ __('dd/mm/aaaa') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Data Final') }}</label>
                <input type="text" name="con_datafim" maxlength="10" class="form-control shadow-sm" data-mask-date value="{{ old('con_datafim', $registro->con_datafim ?? '') }}" placeholder="{{ __('dd/mm/aaaa') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Data Publicação') }}</label>
                <input type="text" name="con_datapub" maxlength="10" class="form-control shadow-sm" data-mask-date value="{{ old('con_datapub', $registro->con_datapub ?? '') }}" placeholder="{{ __('dd/mm/aaaa') }}">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary">{{ __('Valor') }}</label>
                <input type="text" name="con_valor" maxlength="20" class="form-control shadow-sm" value="{{ old('con_valor', $registro->con_valor ?? '') }}">
            </div>

            <div class="col-md-6">
                <label class="form-label fw-semibold text-secondary">{{ __('Secretaria') }}</label>
                <select class="form-select shadow-sm" name="con_secretaria">
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach($secretarias as $secretaria)
                        <option value="{{ $secretaria->id }}" @selected((string) old('con_secretaria', $registro->con_secretaria ?? '') === (string) $secretaria->id)>{{ $secretaria->name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-6">
                <label class="form-label fw-semibold text-secondary">{{ __('Secretaria Gestora') }}</label>
                <select class="form-select shadow-sm" name="con_secretaria_gestora">
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach($secretariasGestoras as $secretaria)
                        <option value="{{ $secretaria->id }}" @selected((string) old('con_secretaria_gestora', $registro->con_secretaria_gestora ?? '') === (string) $secretaria->id)>{{ $secretaria->name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-2">
                <label class="form-label fw-semibold text-secondary">{{ __('Covid') }}</label>
                <select class="form-select shadow-sm" name="con_covid">
                    <option value="">{{ __('Selecione') }}</option>
                    <option value="S" @selected(old('con_covid', $registro->con_covid ?? '') === 'S')>{{ __('Sim') }}</option>
                    <option value="N" @selected(old('con_covid', $registro->con_covid ?? '') === 'N')>{{ __('Não') }}</option>
                </select>
            </div>

            <div class="col-md-10">
                <label class="form-label fw-semibold text-secondary">{{ __('Fiscais') }}</label>
                <textarea name="con_fiscais" rows="2" class="form-control shadow-sm">{{ old('con_fiscais', $registro->con_fiscais ?? '') }}</textarea>
            </div>
        </div>

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

@push('scripts')
<link rel="stylesheet" href="{{ asset('pmar/css/admin/contratos/form.css') }}">
<script src="{{ asset('pmar/js/admin/contratos/form.js') }}"></script>
@endpush
