@php
    $leg = $legislacao ?? null;
@endphp

<div class="row g-3">

    {{-- DADOS PRINCIPAIS --}}
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold text-dark">
                    <i class="fa-solid fa-scale-balanced me-2 text-primary"></i>
                    {{ __('Dados da legislação') }}
                </h5>
            </div>

            <div class="card-body pt-0">
                <div class="row g-3">

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Tipo de legislação') }}
                        </label>

                        <select name="id_tipo_legislacao" class="form-select rounded-3" required>
                            <option value="">{{ __('Selecione') }}</option>

                            @foreach ($tipos as $tipo)
                                <option value="{{ $tipo->id_tipo_legislacao }}"
                                    @selected((int) old('id_tipo_legislacao', $leg->id_tipo_legislacao ?? 0) === (int) $tipo->id_tipo_legislacao)>
                                    {{ $tipo->ds_tipo_legislacao }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Nº da legislação') }}
                        </label>

                        <input
                            type="text"
                            name="nr_legislacao"
                            class="form-control rounded-3"
                            required
                            maxlength="60"
                            value="{{ old('nr_legislacao', $leg->nr_legislacao ?? '') }}">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Data de assinatura') }}
                        </label>

                        <input
                            type="date"
                            name="dt_assinatura"
                            class="form-control rounded-3"
                            value="{{ old('dt_assinatura', $leg && $leg->dt_assinatura ? $leg->dt_assinatura->format('Y-m-d') : '') }}">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Data de publicação') }}
                        </label>

                        <input
                            type="date"
                            name="dt_publicacao"
                            id="legislacao_dt_publicacao"
                            class="form-control rounded-3"
                            value="{{ old('dt_publicacao', $leg && $leg->dt_publicacao ? $leg->dt_publicacao->format('Y-m-d') : '') }}">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Autor da lei / cargo') }}
                        </label>

                        <select name="id_autor" class="form-select rounded-3">
                            <option value="">{{ __('Selecione') }}</option>

                            @foreach ($autores as $autor)
                                <option value="{{ $autor->id_autor }}"
                                    @selected((int) old('id_autor', $leg->id_autor ?? 0) === (int) $autor->id_autor)>
                                    {{ $autor->autor_com_cargo }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Sancionado por (prefeito/prefeito em exercício)') }}
                        </label>

                        <select name="id_autor_sancao" class="form-select rounded-3">
                            <option value="">{{ __('Selecione') }}</option>

                            @foreach ($autoresSancao as $autor)
                                <option value="{{ $autor->id_autor }}"
                                    @selected((int) old('id_autor_sancao', $leg->id_autor_sancao ?? 0) === (int) $autor->id_autor)>
                                    {{ $autor->autor_com_cargo }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-12">
                        <label class="form-label fw-semibold">
                            {{ __('Ementa / objeto') }}
                        </label>

                        <textarea
                            name="ds_ementa"
                            class="form-control rounded-3"
                            rows="4"
                            required>{{ old('ds_ementa', $leg->ds_ementa ?? '') }}</textarea>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Nº do BO de publicação') }}
                        </label>

                        @php
                            $boSelId = old('id_boletim_oficial', $leg->id_boletim_oficial ?? '');
                            $boSelLabel = $leg && $leg->boletim ? $leg->boletim->nr_bo : '';
                        @endphp

                        <select
                            name="id_boletim_oficial"
                            id="legislacao_id_boletim"
                            class="form-select rounded-3 js-legislacao-boletim-api"
                            data-api-url="{{ route('api.boletinsOficiais') }}"
                            data-placeholder="{{ __('Selecione') }}"
                            data-selected-id="{{ $boSelId }}"
                            data-selected-label="{{ e($boSelLabel) }}">

                            <option value="">{{ __('Selecione') }}</option>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label fw-semibold">
                            {{ __('Data do BO') }}
                        </label>

                        <input
                            type="text"
                            id="legislacao_dt_bo_display"
                            class="form-control rounded-3 bg-light"
                            readonly
                            value="{{ $leg && $leg->boletim && $leg->boletim->dt_publicacao ? $leg->boletim->dt_publicacao->format('d/m/Y') : '' }}">
                    </div>

                    <div class="col-md-3 d-flex align-items-end">
                        <a
                            href="{{ $leg && $leg->boletim?->public_bo_pdf_url ? $leg->boletim->public_bo_pdf_url : '#' }}"
                            target="_blank"
                            rel="noopener noreferrer"
                            id="legislacao_link_bo"
                            class="btn btn-outline-primary rounded-3 w-100 @if (!($leg && $leg->boletim?->public_bo_pdf_url)) disabled @endif">

                            <i class="fa-solid fa-file-pdf me-2"></i>
                            {{ __('Link do BO') }}
                        </a>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Status') }}
                        </label>

                        <select name="cd_status" class="form-select rounded-3" required>
                            <option value="1" @selected((int) old('cd_status', $leg->cd_status ?? 1) === 1)>
                                {{ __('Aguardando revisão') }}
                            </option>

                            <option value="2" @selected((int) old('cd_status', $leg->cd_status ?? 1) === 2)>
                                {{ __('Pendente') }}
                            </option>

                            <option value="3" @selected((int) old('cd_status', $leg->cd_status ?? 1) === 3)>
                                {{ __('Publicado') }}
                            </option>
                        </select>
                    </div>

                    @if ($leg && (int) $leg->cd_status === 3)
                        <div class="col-md-6 d-flex align-items-end">
                            <a
                                href="{{ route('paginas.legislacao.detalhe', $leg->id_legislacao) }}"
                                target="_blank"
                                rel="noopener noreferrer"
                                class="btn btn-outline-dark rounded-3 w-100">

                                <i class="fa-solid fa-globe me-2"></i>
                                {{ __('Visualizar no portal') }}
                            </a>
                        </div>
                    @endif

                </div>
            </div>
        </div>
    </div>

    {{-- PDF --}}
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-file-pdf me-2 text-danger"></i>
                    {{ __('Documento em PDF') }}
                </h5>
            </div>

            <div class="card-body pt-0">
                <div class="row g-3 align-items-end">

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Enviar novo PDF') }}
                        </label>

                        <input
                            type="file"
                            name="pdf_arquivo"
                            class="form-control rounded-3"
                            accept=".pdf,application/pdf">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            {{ __('Arquivo atual') }}
                        </label>

                        <input
                            type="text"
                            name="nm_arquivo_anexo"
                            class="form-control rounded-3 bg-light"
                            readonly
                            value="{{ old('nm_arquivo_anexo', $leg->nm_arquivo_anexo ?? '') }}">
                    </div>

                    @if ($leg && $leg->public_pdf_url)
                        <div class="col-12">
                            <a
                                href="{{ $leg->public_pdf_url }}"
                                target="_blank"
                                rel="noopener noreferrer"
                                class="btn btn-outline-danger rounded-3">

                                <i class="fa-solid fa-file-pdf me-2"></i>
                                {{ __('Abrir PDF cadastrado') }}
                            </a>
                        </div>
                    @endif

                </div>
            </div>
        </div>
    </div>

    {{-- CATEGORIAS --}}
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm rounded-3 h-100">
            <div class="card-header bg-white border-0 py-3 d-flex align-items-center justify-content-between">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-tags me-2 text-info"></i>
                    {{ __('Categorias relacionadas') }}
                </h5>
                @php
                    $selectedCategorias = old('categorias', isset($leg) ? $leg->categorias->pluck('id_categoria')->toArray() : []);
                    $totalSelecionadas = count(array_filter(array_map('intval', $selectedCategorias)));
                @endphp
                @if ($totalSelecionadas > 0)
                    <span class="badge bg-info text-white fs-6 rounded-pill" id="legislacao_categorias_badge">
                        {{ $totalSelecionadas }}
                    </span>
                @else
                    <span class="badge bg-secondary text-white fs-6 rounded-pill" id="legislacao_categorias_badge">0</span>
                @endif
            </div>

            <div class="card-body pt-2">
                <div class="legislacao-categorias-ts-host">
                    <select
                        id="legislacao_categorias_select"
                        name="categorias[]"
                        multiple
                        autocomplete="off"
                        class="form-select"
                        data-placeholder="{{ __('Buscar categoria…') }}"
                        aria-describedby="legislacao_categorias_ajuda">

                        @foreach ($categorias as $categoria)
                            @php $cid = (int) $categoria->id_categoria; @endphp
                            <option value="{{ $cid }}" @selected(in_array($cid, array_map('intval', $selectedCategorias), true))>
                                {{ $categoria->nm_categoria }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div id="legislacao_categorias_ajuda" class="form-text mt-2">
                    <i class="fa-solid fa-circle-info me-1 text-info"></i>
                    {{ __('Clique para abrir a lista. Selecione quantas categorias precisar.') }}
                </div>
            </div>
        </div>
    </div>

    {{-- TEXTO --}}
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm rounded-3 h-100">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-align-left me-2 text-success"></i>
                    {{ __('Texto compilado') }}
                </h5>
            </div>

            <div class="card-body pt-0">
                <textarea
                    name="ds_legislacao_sem_html"
                    class="form-control rounded-3 h-100"
                    rows="10"
                    required>{{ old('ds_legislacao_sem_html', $leg->ds_legislacao_sem_html ?? '') }}</textarea>
            </div>
        </div>
    </div>

    {{-- RELACIONAMENTOS --}}
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-link me-2 text-info"></i>
                    {{ __('Relacionamentos de legislações') }}
                </h5>
            </div>

            <div class="card-body pt-0">

                <div class="row g-3">

                    <div class="col-md-3">
                        <label class="form-label fw-semibold">
                            {{ __('Ação') }}
                        </label>

                        <select id="legislacao_sel_acao" class="form-select rounded-3">
                            <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 fw-semibold">
                            {{ __('Legislação') }}
                        </label>

                        <select id="legislacao_sel_alvo" class="form-select rounded-3">
                            <option value="">{{ __('Selecione') }}</option>

                            @foreach ($legislacaoVinculos as $opt)
                                @if (!$leg || (int) $opt->id_legislacao !== (int) $leg->id_legislacao)
                                    <option value="{{ $opt->id_legislacao }}"
                                        data-mes="{{ $opt->dt_publicacao ? $opt->dt_publicacao->translatedFormat('d \d\e F \d\e Y') : '' }}">

                                        {{ ($opt->tipo->ds_tipo_legislacao ?? '') . ' ' . $opt->nr_legislacao }}
                                    </option>
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-3 d-flex align-items-end">
                        <button
                            type="button"
                            class="btn btn-primary rounded-3 w-100"
                            id="legislacao_btn_vincular">

                            <i class="fa-solid fa-plus me-2"></i>
                            {{ __('Vincular') }}
                        </button>
                    </div>

                </div>

                <div id="legislacao_acoes_list" class="mt-4"></div>

                <input type="hidden" name="acoes_json" id="legislacao_acoes_json" value="">

                <table class="d-none" aria-hidden="true">
                    <tbody id="legislacao_acoes_semente">
                        @foreach (($leg?->acoes ?? collect()) as $acao)
                            <tr
                                data-cd="{{ $acao->cd_acao }}"
                                data-alvo="{{ $acao->id_legislacao_sofre_acao }}"
                                class="acao-semente"></tr>
                        @endforeach
                    </tbody>
                </table>

            </div>
        </div>
    </div>

    {{-- BOTÃO --}}
    <div class="col-12">
        <div class="d-flex justify-content-end">
            <button class="btn btn-success px-5 rounded-3 shadow-sm" type="submit">
                <i class="fa-solid fa-floppy-disk me-2"></i>
                {{ __('Salvar legislação') }}
            </button>
        </div>
    </div>

</div>