@php $p = $portaria ?? null; @endphp

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-light"><span class="fw-semibold">{{ __('Dados da portaria') }}</span></div>
    <div class="card-body">
        <div class="row g-3">
            <div class="col-md-4">
                <label class="form-label">{{ __('Nº da portaria') }}</label>
                <input type="text" name="nr_portaria" class="form-control" required maxlength="60"
                    value="{{ old('nr_portaria', $p->nr_portaria ?? '') }}">
            </div>
            <div class="col-md-4">
                <label class="form-label">{{ __('Data do ato') }}</label>
                <input type="date" name="dt_ato" class="form-control"
                    value="{{ old('dt_ato', $p && $p->dt_ato ? $p->dt_ato->format('Y-m-d') : '') }}">
            </div>
            <div class="col-md-4">
                <label class="form-label">{{ __('Data de publicação') }}</label>
                <input type="date" name="dt_publicacao" id="portaria_dt_publicacao" class="form-control"
                    value="{{ old('dt_publicacao', $p && $p->dt_publicacao ? $p->dt_publicacao->format('Y-m-d') : '') }}">
            </div>
            <div class="col-12">
                <label class="form-label">{{ __('Ementa') }}</label>
                <textarea name="ds_ementa" class="form-control" rows="3">{{ old('ds_ementa', $p->ds_ementa ?? '') }}</textarea>
            </div>
            <div class="col-md-6">
                <label class="form-label">{{ __('Boletim oficial') }}</label>
                @php
                    $pBoSelId = old('id_boletim_oficial', $p->id_boletim_oficial ?? '');
                    $pBoSelLabel = $p && $p->boletim ? $p->boletim->nr_bo : '';
                @endphp
                <select name="id_boletim_oficial" id="portaria_id_boletim" class="form-select js-legislacao-boletim-api" required
                    data-api-url="{{ route('api.boletinsOficiais') }}"
                    data-placeholder="{{ __('Selecione') }}"
                    data-selected-id="{{ $pBoSelId }}"
                    data-selected-label="{{ e($pBoSelLabel) }}">
                    <option value="">{{ __('Selecione') }}</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">{{ __('Data do BO') }}</label>
                <input type="text" id="portaria_dt_bo_display" class="form-control" readonly
                    value="{{ $p && $p->boletim && $p->boletim->dt_publicacao ? $p->boletim->dt_publicacao->format('d/m/Y') : '' }}">
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <a href="{{ $p && $p->boletim?->public_bo_pdf_url ? $p->boletim->public_bo_pdf_url : '#' }}"
                    id="portaria_link_bo" target="_blank" rel="noopener noreferrer"
                    class="btn btn-outline-primary w-100 @if (!($p && $p->boletim?->public_bo_pdf_url)) disabled @endif">
                    <i class="fa-solid fa-file-pdf me-1"></i>{{ __('Link do BO') }}
                </a>
            </div>
            <div class="col-md-6">
                <label class="form-label">{{ __('Status') }}</label>
                <select name="cd_status" class="form-select" required>
                    <option value="1" @selected((int) old('cd_status', $p->cd_status ?? 1) === 1)>{{ __('Pendente') }}</option>
                    <option value="2" @selected((int) old('cd_status', $p->cd_status ?? 1) === 2)>{{ __('Publicado') }}</option>
                </select>
            </div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-light">{{ __('Relacionamentos de portarias') }}</div>
    <div class="card-body">
        <div class="row g-3">
            <div class="col-md-3">
                <label class="form-label">{{ __('Ação') }}</label>
                <select id="portaria_sel_acao" class="form-select">
                    <option value="">{{ __('Selecione') }}</option>
                    <option value="1">{{ __('Altera') }}</option>
                    <option value="2">{{ __('Revoga') }}</option>
                    <option value="3">{{ __('Anula') }}</option>
                    <option value="4">{{ __('Promulga') }}</option>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label">{{ __('Portaria') }}</label>
                <select id="portaria_sel_alvo" class="form-select">
                    <option value="">{{ __('Selecione') }}</option>
                    @foreach ($portariasVinculo as $opt)
                        @if (!$p || (int) $opt->id_portaria !== (int) $p->id_portaria)
                            <option value="{{ $opt->id_portaria }}"
                                data-mes="{{ $opt->dt_publicacao ? $opt->dt_publicacao->translatedFormat('d \d\e F \d\e Y') : '' }}">
                                {{ $opt->nr_portaria }}
                                @if ($opt->dt_ato)
                                    ({{ $opt->dt_ato->format('Y') }})
                                @endif
                            </option>
                        @endif
                    @endforeach
                </select>
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <button type="button" class="btn btn-primary w-100" id="portaria_btn_vincular">
                    <i class="fa-solid fa-link me-1"></i>{{ __('Vincular') }}
                </button>
            </div>
        </div>
        <div id="portaria_acoes_list" class="mt-3" role="list"></div>
        <input type="hidden" name="acoes_json" id="portaria_acoes_json" value="">
        <table class="d-none" aria-hidden="true">
            <tbody id="portaria_acoes_semente">
                @foreach (($p?->acoes ?? collect()) as $acao)
                    <tr data-cd="{{ $acao->cd_acao }}" data-alvo="{{ $acao->id_portaria_sofre_acao }}" class="acao-semente"></tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>

<div class="d-flex justify-content-end mt-3">
    <button type="submit" class="btn btn-success px-4"><i class="fa-solid fa-floppy-disk me-1" aria-hidden="true"></i>{{ __('Salvar') }}</button>
</div>
