@extends('layouts.pmar.theme')
@section("title", env("APP_NAME") . " :: " . __('Legislação Municipal'))
@section('content')
<link rel="stylesheet" href="{{ asset('pmar/css/paginas/licitacoes-publicas.css') }}">
<link rel="stylesheet" href="{{ asset('pmar/css/paginas/legislacao-publica.css') }}">

<div class="lp-hero lp-wrap"><div class="lp-hero-inner">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="{{ route("paginas.home") }}">{{ __('Portal') }}</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">{{ __('Legislação Municipal') }}</li>
            </ol>
        </nav>

        <div class="lp-hero-body">
            <div class="lp-hero-icon">
                <i class="fa-solid fa-scale-balanced text-primary"></i>
            </div>
            <div>
                <h1 class="lp-hero-title text-primary">{{ __('Legislação Municipal') }}</h1>
                <p class="lp-hero-sub text-primary">{{ __('Consulte leis, decretos e normas publicadas pelo município') }}</p>
            </div>
        </div>
    </div>
    <div class="lp-hero-bar"></div>
</div>

<section class="lp-main lp-wrap">
    <div class="container">
        <div class="lp-filter-card">
            <div class="lp-filter-header">
                <div class="lp-filter-header-left">
                    <div class="lp-filter-icon">
                        <i class="fa-solid fa-sliders"></i>
                    </div>
                    <span class="lp-filter-title">{{ __('Filtros') }}</span>
                    <span class="lp-filter-sub">{{ __('Refine sua pesquisa') }}</span>
                </div>
                <a class="btn-lp-clear text-decoration-none" href="{{ route('paginas.legislacao') }}">
                    <i class="fa-solid fa-xmark"></i>{{ __('Limpar filtros') }}
                </a>
            </div>

            <div class="lp-filter-body">
                <form method="get">

                    {{-- BUSCA PRINCIPAL --}}
                    <div class="row g-3 mb-3">
                        <div class="col-12">
                            <label for="texto" class="lp-field-label">
                                {{ __('Texto ou palavra-chave') }}
                            </label>

                            <div class="lp-field-wrap lp-has-icon">
                                <span class="lp-field-icon">
                                    <i class="fa-solid fa-magnifying-glass"></i>
                                </span>

                                <input id="texto"
                                    name="texto"
                                    type="search"
                                    class="form-control"
                                    value="{{ request('texto') }}"
                                    placeholder="{{ __('Ex: saúde, educação, transporte...') }}">
                            </div>
                        </div>
                    </div>

                    {{-- FILTROS --}}
                    <div class="row g-3 align-items-end">

                        {{-- TIPO --}}
                        <div class="col-12 col-md-6 col-lg-3">
                            <label for="tipo" class="lp-field-label">
                                {{ __('Tipo') }}
                            </label>

                            <div class="lp-field-wrap">
                                <select class="form-select" id="tipo" name="tipo">
                                    <option value="">{{ __('Todos') }}</option>

                                    @foreach($tipos as $tipo)
                                        <option value="{{ $tipo->id_tipo_legislacao }}"
                                            @selected((string) request('tipo') === (string) $tipo->id_tipo_legislacao)>
                                            {{ $tipo->ds_tipo_legislacao }}
                                        </option>
                                    @endforeach
                                </select>

                                <span class="lp-select-arrow">
                                    <i class="fa-solid fa-chevron-down"></i>
                                </span>
                            </div>
                        </div>

                        {{-- NÚMERO --}}
                        <div class="col-12 col-md-6 col-lg-2">
                            <label for="numero" class="lp-field-label">
                                {{ __('Número') }}
                            </label>

                            <div class="lp-field-wrap lp-has-icon">
                                <span class="lp-field-icon">
                                    <i class="fa-solid fa-hashtag"></i>
                                </span>

                                <input id="numero"
                                    name="numero"
                                    type="search"
                                    class="form-control"
                                    value="{{ request('numero') }}"
                                    placeholder="{{ __('1234') }}">
                            </div>
                        </div>

                        {{-- ANO --}}
                        <div class="col-12 col-md-6 col-lg-2">
                            <label for="ano" class="lp-field-label">
                                {{ __('Ano') }}
                            </label>

                            <div class="lp-field-wrap">
                                <select class="form-select" id="ano" name="ano">
                                    <option value="">{{ __('Todos') }}</option>

                                    @foreach($anos as $ano)
                                        <option value="{{ $ano }}"
                                            @selected((string) request('ano') === (string) $ano)>
                                            {{ $ano }}
                                        </option>
                                    @endforeach
                                </select>

                                <span class="lp-select-arrow">
                                    <i class="fa-solid fa-chevron-down"></i>
                                </span>
                            </div>
                        </div>

                        {{-- CATEGORIA --}}
                        <div class="col-12 col-md-6 col-lg-3">
                            <label for="categoria" class="lp-field-label">
                                {{ __('Categoria') }}
                            </label>

                            <div class="lp-field-wrap">
                                <select class="form-select" id="categoria" name="categoria">
                                    <option value="">{{ __('Todas') }}</option>

                                    @foreach($categorias as $categoria)
                                        <option value="{{ $categoria->id_categoria }}"
                                            @selected((string) request('categoria') === (string) $categoria->id_categoria)>
                                            {{ $categoria->nm_categoria }}
                                        </option>
                                    @endforeach
                                </select>

                                <span class="lp-select-arrow">
                                    <i class="fa-solid fa-chevron-down"></i>
                                </span>
                            </div>
                        </div>

                        {{-- BOTÃO --}}
                        <div class="col-12 col-lg-2">
                            <button class="btn-lp-apply w-100" type="submit">
                                <i class="fa-solid fa-filter"></i>
                                {{ __('Filtrar') }}
                            </button>
                        </div>

                    </div>

                    <div class="lp-filter-options">
                        <label class="lp-filter-option" for="mostrar-portarias">
                            <input type="checkbox"
                                class="lp-filter-option-input"
                                name="mostrar_portarias"
                                value="1"
                                id="mostrar-portarias"
                                @checked($mostrarPortarias ?? false)>
                            <span class="lp-filter-option-box" aria-hidden="true">
                                <i class="fa-solid fa-check"></i>
                            </span>
                            <span class="lp-filter-option-content">
                                <span class="lp-filter-option-title">
                                    <i class="fa-solid fa-file-signature" aria-hidden="true"></i>
                                    {{ __('Incluir portarias') }}
                                </span>
                                <span class="lp-filter-option-desc">
                                    {{ __('Exibe portarias municipais publicadas junto com as demais normas.') }}
                                </span>
                            </span>
                        </label>
                    </div>

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

        <div class="lp-result-bar-wrap">
            <p class="lp-result-bar mb-0">
                {{ __(':total documento(s) encontrado(s)', ['total' => $legislacoes->total()]) }}
            </p>
            <a href="#" class="lp-download-link" id="btn-download-csv" aria-label="{{ __('Download em CSV') }}">
                <i class="fa-solid fa-download"></i>{{ __('Download em CSV') }}
            </a>
        </div>

        <div class="row g-3">
            @forelse($legislacoes as $documento)
                <div class="col-12">
                @if (($documento['kind'] ?? 'legislacao') === 'portaria')
                    @php $portaria = $documento['data']; @endphp
                    <article class="card border-0 shadow-sm legislacao-card h-100">
                        <div class="card-body p-4">
                            <div class="d-flex flex-column flex-lg-row justify-content-between align-items-start gap-3">
                                <div class="flex-grow-1">
                                    <div class="d-flex flex-wrap gap-2 mb-3">
                                        <span class="badge rounded-pill bg-primary-subtle text-primary fw-semibold px-3 py-2">
                                            <i class="fa-solid fa-file-signature me-1"></i>
                                            {{ __('Portaria') }}
                                        </span>

                                        @if($portaria->dt_publicacao)
                                            <span class="badge rounded-pill bg-light text-dark border px-3 py-2">
                                                <i class="fa-regular fa-calendar me-1"></i>
                                                {{ $portaria->dt_publicacao->format('d/m/Y') }}
                                            </span>
                                        @endif
                                    </div>

                                    <h2 class="h4 mb-3 fw-bold">
                                        @if ($portaria->boletim?->public_bo_pdf_url)
                                            <a href="{{ $portaria->boletim->public_bo_pdf_url }}"
                                                target="_blank"
                                                rel="noopener noreferrer"
                                                class="text-primary text-decoration-none legislacao-link">
                                                {{ __('Portaria Nº :numero', ['numero' => $portaria->nr_portaria]) }}
                                            </a>
                                        @else
                                            <span class="text-primary">
                                                {{ __('Portaria Nº :numero', ['numero' => $portaria->nr_portaria]) }}
                                            </span>
                                        @endif
                                    </h2>

                                    <p class="text-muted mb-4 legislacao-ementa">
                                        {{ $portaria->ds_ementa ?: __('Sem ementa cadastrada.') }}
                                    </p>
                                </div>

                                <div class="d-flex flex-lg-column gap-2 w-auto">
                                    @if ($portaria->boletim?->public_bo_pdf_url)
                                        <a href="{{ $portaria->boletim->public_bo_pdf_url }}"
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            class="btn btn-outline-danger btn-sm px-3"
                                            aria-label="{{ __('Abrir PDF da portaria nº :numero em nova aba', [
                                                'numero' => $portaria->nr_portaria
                                            ]) }}">
                                            <i class="fa-solid fa-file-pdf me-1"></i>
                                            {{ __('Baixar') }}
                                        </a>
                                    @endif
                                </div>
                            </div>

                            <div class="border-top my-3"></div>

                            <div class="d-flex flex-wrap gap-4 small text-muted mb-0">
                                @if($portaria->dt_ato)
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="legislacao-info-icon">
                                            <i class="fa-solid fa-calendar-check"></i>
                                        </div>
                                        <div>
                                            <div class="fw-semibold text-dark">
                                                {{ __('Data do ato') }}
                                            </div>
                                            <div>
                                                {{ $portaria->dt_ato->format('d/m/Y') }}
                                            </div>
                                        </div>
                                    </div>
                                @endif

                                <div class="d-flex align-items-center gap-2">
                                    <div class="legislacao-info-icon">
                                        <i class="fa-solid fa-calendar-days"></i>
                                    </div>
                                    <div>
                                        <div class="fw-semibold text-dark">
                                            {{ __('Publicação') }}
                                        </div>
                                        <div>
                                            {{ $portaria->dt_publicacao
                                                ? $portaria->dt_publicacao->format('d/m/Y')
                                                : __('Não informada') }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </article>
                @else
                    @php $legislacao = $documento['data']; @endphp
                <article class="card border-0 shadow-sm legislacao-card h-100">
    <div class="card-body p-4">

        {{-- TOPO --}}
        <div class="d-flex flex-column flex-lg-row justify-content-between align-items-start gap-3">

            <div class="flex-grow-1">

                {{-- BADGES SUPERIORES --}}
                <div class="d-flex flex-wrap gap-2 mb-3">

                    <span class="badge rounded-pill bg-primary-subtle text-primary fw-semibold px-3 py-2">
                        <i class="fa-solid fa-scale-balanced me-1"></i>
                        {{ $legislacao->tipo->ds_tipo_legislacao ?? __('Sem tipo') }}
                    </span>

                    @if($legislacao->dt_publicacao)
                        <span class="badge rounded-pill bg-light text-dark border px-3 py-2">
                            <i class="fa-regular fa-calendar me-1"></i>
                            {{ \Carbon\Carbon::parse($legislacao->dt_publicacao)->format('d/m/Y') }}
                        </span>
                    @endif

                </div>

                {{-- TÍTULO --}}
                <h2 class="h4 mb-3 fw-bold">
                    <a href="{{ $legislacao->public_pdf_url }}"
                        class="text-primary text-decoration-none legislacao-link">

                        {{ __('Legislação Nº :numero', [
                            'numero' => $legislacao->nr_legislacao
                        ]) }}

                    </a>
                </h2>

                {{-- EMENTA --}}
                <p class="text-muted mb-4 legislacao-ementa">
                    {{ $legislacao->ds_ementa ?: __('Sem ementa cadastrada.') }}
                </p>

            </div>

            {{-- AÇÕES --}}
            <div class="d-flex flex-lg-column gap-2 w-auto">

                <!-- <a href="{{ route('paginas.legislacao.detalhe', $legislacao) }}"
                    class="btn btn-primary btn-sm px-3">

                    <i class="fa-solid fa-eye me-1"></i>
                    {{ __('Detalhes') }}
                </a> -->

                @if ($legislacao->public_pdf_url)
                    <a href="{{ $legislacao->public_pdf_url }}"
                        target="_blank"
                        rel="noopener noreferrer"
                        class="btn btn-outline-danger btn-sm px-3"
                        aria-label="{{ __('Abrir PDF da legislação nº :numero em nova aba', [
                            'numero' => $legislacao->nr_legislacao
                        ]) }}">

                        <i class="fa-solid fa-file-pdf me-1"></i>
                        {{ __('Baixar') }}
                    </a>
                @endif

            </div>
        </div>

        {{-- DIVISÓRIA --}}
        <div class="border-top my-3"></div>

        {{-- INFORMAÇÕES --}}
        <div class="d-flex flex-wrap gap-4 small text-muted mb-3">

            <div class="d-flex align-items-center gap-2">
                <div class="legislacao-info-icon">
                    <i class="fa-solid fa-user"></i>
                </div>

                <div>
                    <div class="fw-semibold text-dark">
                        {{ __('Autor') }}
                    </div>

                    <div>
                        {{ $legislacao->autor->nm_autor ?? __('Não informado') }}
                    </div>
                </div>
            </div>

            <div class="d-flex align-items-center gap-2">
                <div class="legislacao-info-icon">
                    <i class="fa-solid fa-calendar-days"></i>
                </div>

                <div>
                    <div class="fw-semibold text-dark">
                        {{ __('Publicação') }}
                    </div>

                    <div>
                        {{ $legislacao->dt_publicacao
                            ? \Carbon\Carbon::parse($legislacao->dt_publicacao)->format('d/m/Y')
                            : __('Não informada') }}
                    </div>
                </div>
            </div>

        </div>

        {{-- CATEGORIAS --}}
        @if($legislacao->categorias->isNotEmpty())
            <div class="d-flex flex-wrap gap-2">

                @foreach($legislacao->categorias as $categoria)
                    <span class="badge rounded-pill text-bg-light border px-3 py-2">
                        <i class="fa-solid fa-tag me-1"></i>
                        {{ $categoria->nm_categoria }}
                    </span>
                @endforeach

            </div>
        @endif

    </div>
</article>
                @endif

                </div>
            @empty
                <div class="col-12">
                    <div class="lp-empty" role="status">
                        <i class="fa-regular fa-folder-open"></i>
                        <p>{{ __('Nenhuma legislação encontrada para os filtros informados.') }}</p>
                    </div>
                </div>
            @endforelse
        </div>

        <div class="lp-pagination d-flex justify-content-center mt-4">
            {{ $legislacoes->links() }}
        </div>
    </div>
</section>

<div id="legislacao-publica-config"
    class="d-none"
    data-documentos="{{ $legislacaoExportJson }}">
</div>

<script src="{{ asset('pmar/js/paginas/legislacao-publica.js') }}?v={{ @filemtime(public_path('pmar/js/paginas/legislacao-publica.js')) ?: time() }}"></script>
@endsection
