{{-- Aba Projetos: filtros, tabela e paginação (sem card de prioridades) --}}
<div class="row g-2 mb-3">
    <div class="col-md-12">
        <label class="form-label small">{{ __("Buscar") }}</label>
        <div class="input-group input-group-sm">
            <input type="text" name="search" id="searchInput" class="form-control"
                placeholder="{{ __('Buscar por denominação, descrição, bairro, endereço...') }}"
                value="{{ request('search') }}">
            <button class="btn btn-outline-secondary" type="button" id="clearSearch">
                <i class="fa fa-times"></i>
            </button>
        </div>
    </div>
</div>

<form method="get" id="filterForm" class="row g-2 mb-3">
    <input type="hidden" name="search" id="searchHidden" value="{{ request('search') }}">
    <input type="hidden" name="tab" value="projetos">
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_gestor">{{ __("Secretaria") }}</label>
        <select name="gestor" id="tab_proj_f_gestor" class="form-select form-select-sm">
            <option value="">{{ __("Todos") }}</option>
            @foreach($gestores as $g)
                <option value="{{ $g->id }}" {{ request('gestor') == $g->id ? 'selected' : '' }}>
                    {{ $g->abbreviation ? $g->abbreviation . ' - ' . $g->name : $g->name }}
                </option>
            @endforeach
        </select>
    </div>
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_prioritario">{{ __("Prioritário") }}</label>
        <select name="prioritario" id="tab_proj_f_prioritario" class="form-select form-select-sm">
            <option value="">{{ __("Todos") }}</option>
            <option value="1" {{ request('prioritario') === '1' ? 'selected' : '' }}>{{ __('Sim') }}</option>
            <option value="0" {{ request('prioritario') === '0' ? 'selected' : '' }}>{{ __('Não') }}</option>
        </select>
    </div>
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_orc_cor">{{ __('Situação do orçamento (MAPÃO)') }}</label>
        <select name="orcamento_status_cor" id="tab_proj_f_orc_cor" class="form-select form-select-sm">
            <option value="">{{ __('Todas') }}</option>
            @foreach($mapaoCoresLegenda ?? [] as $valor => $legenda)
                <option value="{{ $valor }}" {{ request('orcamento_status_cor') === $valor ? 'selected' : '' }}>{{ $legenda }}</option>
            @endforeach
            <option value="_vazio" {{ request('orcamento_status_cor') === '_vazio' ? 'selected' : '' }}>{{ __('Sem situação cadastrada') }}</option>
        </select>
    </div>
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_status_reg">{{ __('Situação do cadastro') }}</label>
        <select name="status_registro" id="tab_proj_f_status_reg" class="form-select form-select-sm">
            <option value="">{{ __('Todas') }}</option>
            @foreach($statusRegistroFiltroOpcoes ?? [] as $valor => $rotulo)
                <option value="{{ $valor }}" {{ request('status_registro') === $valor ? 'selected' : '' }}>{{ $rotulo }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_bairro">{{ __('Bairro') }}</label>
        <select name="bairro" id="tab_proj_f_bairro" class="form-select form-select-sm">
            <option value="">{{ __('Todos') }}</option>
            @foreach($bairrosFiltroOpcoes ?? [] as $bNome)
                <option value="{{ $bNome }}" {{ request('bairro') === $bNome || request('bairo') === $bNome ? 'selected' : '' }}>
                    {{ $bNome }}
                </option>
            @endforeach
        </select>
    </div>
    <div class="col-6 col-md-4 col-lg-2">
        <label class="form-label small" for="tab_proj_f_per_page">{{ __("Por página") }}</label>
        <select name="per_page" id="tab_proj_f_per_page" class="form-select form-select-sm" onchange="this.form.submit()">
            @foreach($perPageOptions as $option)
                <option value="{{ $option }}" {{ $currentPerPage == $option ? 'selected' : '' }}>{{ $option }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-12 col-md-8 col-lg-4 d-flex align-items-end gap-2 flex-wrap">
        <button class="btn btn-success btn-sm" type="submit">
            <i class="fa fa-filter me-1"></i> {{ __("Filtrar") }}
        </button>
        <a href="{{ route('admin.projetosProgramas.index', ['tab' => 'projetos']) }}" class="btn btn-secondary btn-sm">{{ __("Limpar") }}</a>
    </div>
</form>

<div class="d-flex flex-wrap justify-content-between align-items-center mb-2">
    <div class="small text-muted">
        <strong>{{ __("Total") }}:</strong> {{ $projects->total() }} {{ __("registro(s)") }}
        @if($projects->total())
            <span class="ms-2">| {{ __("Exibindo") }} {{ $projects->firstItem() }}-{{ $projects->lastItem() }}</span>
        @endif
    </div>
    <div class="d-flex align-items-center gap-2">
        <div class="form-check form-check-inline mb-0">
            <input class="form-check-input" type="checkbox" id="printShowMetas" checked>
            <label class="form-check-label small" for="printShowMetas">
                {{ __('Metas / Desdobramentos') }}
            </label>
        </div>
        <div class="form-check form-check-inline mb-0">
            <input class="form-check-input" type="checkbox" id="printShowHistorico">
            <label class="form-check-label small" for="printShowHistorico">
                {{ __('Histórico') }}
            </label>
        </div>
        <a href="{{ route('admin.projetosProgramas.imprimirLista', request()->query()) }}" target="_blank"
           class="btn btn-outline-secondary btn-sm"
           data-print-list>
            <i class="fa fa-print me-1"></i> {{ __("Imprimir") }}
        </a>
    </div>
</div>

<div class="table-responsive">
    <table class="table table-sm table-striped table-hover table-bordered mb-2" id="projetosTable">
        <thead class="table-light">
            <tr>
                <th style="width:4%" class="text-center text-nowrap" scope="col"
                    title="{{ __('Numeração igual ao MAPÃO impresso') }}">{{ __('Nº') }}</th>
                <th style="width:5%" class="text-center">
                    <a href="?{{ http_build_query(array_merge(request()->except(['sort_by', 'sort_dir']), ['sort_by' => 'gestor_structure_id', 'sort_dir' => request('sort_by') == 'gestor_structure_id' && request('sort_dir') == 'asc' ? 'desc' : 'asc', 'tab' => 'projetos'])) }}"
                        class="text-decoration-none text-dark">
                        {{ __("Gestor") }}
                        @if(request('sort_by') == 'gestor_structure_id')
                            <i class="fa fa-sort-{{ request('sort_dir') == 'asc' ? 'up' : 'down' }}"></i>
                        @else
                            <i class="fa fa-sort text-muted"></i>
                        @endif
                    </a>
                </th>
                <th class="text-center">
                    <a href="?{{ http_build_query(array_merge(request()->except(['sort_by', 'sort_dir']), ['sort_by' => 'denominacao', 'sort_dir' => request('sort_by') == 'denominacao' && request('sort_dir') == 'asc' ? 'desc' : 'asc', 'tab' => 'projetos'])) }}"
                        class="text-decoration-none text-dark">
                        {{ __("Denominação") }}
                        @if(request('sort_by') == 'denominacao')
                            <i class="fa fa-sort-{{ request('sort_dir') == 'asc' ? 'up' : 'down' }}"></i>
                        @else
                            <i class="fa fa-sort text-muted"></i>
                        @endif
                    </a>
                </th>
                <th style="width:8%" class="text-center">{{ __("Tipo") }}</th>
                <th style="width:8%" class="text-center">{{ __("Classif.") }}</th>
                <th style="width:10%" class="text-center">{{ __("Etapa") }}</th>
                <th style="width:8%" class="text-center">{{ __("Situação") }}</th>
                <th style="width:18%" class="text-center">{{ __("Ações") }}</th>
            </tr>
        </thead>
        <tbody>
            @forelse($projects as $proj)
                <tr>
                    <td class="small font-monospace text-center text-nowrap">{{ ($mapaoIdentificadores ?? [])[$proj->id] ?? '—' }}</td>
                    <td class="small text-center">
                        @if($proj->gestor)
                            {{ $proj->gestor->abbreviation ? $proj->gestor->abbreviation . ' - ' . $proj->gestor->name : $proj->gestor->name }}
                        @endif
                    </td>
                    <td class="small text-center">{{ \Illuminate\Support\Str::limit($proj->denominacao, 50) }}</td>
                    <td class="small text-center">{{ optional($proj->tipo)->nome }}</td>
                    <td class="small text-center">{{ optional($proj->classificacao)->nome }}</td>
                    <td class="small text-center">{{ optional($proj->etapa)->nome }}</td>
                    <td class="text-center">
                        <span
                            class="badge bg-{{ $proj->status_registro_badge_class }} small">{{ __($proj->status_registro_label) }}</span>
                    </td>
                    <td class="text-center">
                        <div class="btn-group btn-group-sm" role="group">
                            <a href="{{ route('admin.projetosProgramas.identificacao', $proj->id) }}"
                                class="btn btn-outline-primary btn-sm" title="{{ __('Editar') }}">
                                <i class="fa fa-edit"></i>
                            </a>
                            @if($canManagePriorities)
                                <form action="{{ route('admin.projetosProgramas.prioridade.toggle', $proj->id) }}"
                                      method="post"
                                      class="d-inline">
                                    @csrf
                                    @foreach(($filterParams ?? []) as $key => $value)
                                        @if($value !== null && $value !== '')
                                            <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                        @endif
                                    @endforeach
                                    <input type="hidden" name="origem" value="SUPOP">
                                    <button type="submit"
                                            class="btn btn-sm {{ $proj->prioritario ? 'btn-warning text-dark' : 'btn-outline-dark' }}"
                                            title="{{ $proj->prioritario ? __('Remover dos prioritários') : __('Marcar como prioritário') }}">
                                        <i class="{{ $proj->prioritario ? 'fa-solid fa-star' : 'fa-regular fa-star' }}"
                                            aria-hidden="true"></i>
                                        <span class="visually-hidden">{{ $proj->prioritario ? __('Remover prioridade') : __('Priorizar') }}</span>
                                    </button>
                                </form>
                            @endif
                            <button type="button" class="btn btn-outline-secondary btn-sm" data-open-attachments
                                data-fetch="{{ route('admin.projetosProgramas.anexos', $proj->id) }}"
                                data-upload="{{ route('admin.projetosProgramas.upload', $proj->id) }}"
                                data-title="{{ $proj->denominacao }}" title="{{ __('Anexos') }}">
                                <i class="fa fa-paperclip"></i>
                            </button>
                            @if($canDelete ?? false)
                                <form action="{{ route('admin.projetosProgramas.destroy', $proj->id) }}"
                                    method="post" class="d-inline"
                                    onsubmit="return confirm('{{ __('Tem certeza que deseja excluir este registro? Esta ação não pode ser desfeita.') }}');">
                                    @csrf @method('DELETE')
                                    <button class="btn btn-outline-danger btn-sm" title="{{ __('Excluir') }}" type="submit">
                                        <i class="fa fa-trash"></i>
                                    </button>
                                </form>
                            @endif
                        </div>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" class="text-center text-muted py-3">{{ __("Nenhum registro encontrado") }}</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>
<div class="d-flex justify-content-center">
    {{ $projects->appends(array_merge(request()->query(), ['tab' => 'projetos']))->links('pagination::bootstrap-4') }}
</div>
