@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Portarias'))

@push('scripts')
    <link rel="stylesheet" href="{{ asset('css/admin/legislacao/index.css') }}">
@endpush

@section('content')
    @include('errors.messageFlash')

    <div class="card shadow-sm">
        @php $headLine = "<i class='fa-solid fa-file-signature'></i> " . __('Portarias'); @endphp
        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' => __($headLine),
            'routeBack' => 'admin.legislacao.index',
            'routeBackParams' => request()->only(['q', 'page', 'ano', 'tipo', 'status']),
            'nameBack' => __('Legislações'),
            'routeAdd' => 'admin.legislacao.portarias.create',
            'routeAddParams' => $filterParams,
            'nameAdd' => __('Nova portaria'),
            'permission' => 'GERENCIADOR_DE_LEGISLACAO',
        ])

        <div class="card-body">
            <form method="get" action="{{ route('admin.legislacao.portarias.index') }}" class="leg-filter-panel mb-4">
                @if (request()->filled('sort_by'))
                    <input type="hidden" name="sort_by" value="{{ request('sort_by') }}">
                @endif
                @if (request()->filled('sort_dir'))
                    <input type="hidden" name="sort_dir" value="{{ request('sort_dir') }}">
                @endif
                <div class="d-flex justify-content-end mb-3">
                    <span class="badge bg-primary leg-results-badge">
                        <i class="fa-solid fa-file-signature me-1" aria-hidden="true"></i>
                        {{ __('Registros encontrados') }}:
                        <strong>{{ number_format($portarias->total(), 0, ',', '.') }}</strong>
                    </span>
                </div>
                <div class="row g-3 align-items-end">
                    <div class="col-12 col-sm-6 col-lg-2">
                        <label for="port-filtro-ano" class="form-label">
                            <i class="fa-solid fa-calendar-days me-1 text-muted" aria-hidden="true"></i>{{ __('Ano de publicação') }}
                        </label>
                        <select id="port-filtro-ano" name="ano" class="form-select">
                            <option value="">{{ __('Todos') }}</option>
                            @foreach ($anos as $ano)
                                <option value="{{ $ano }}" @selected((string) $anoSelecionado === (string) $ano)>{{ $ano }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-12 col-sm-6 col-lg-3">
                        <label for="port-filtro-status" class="form-label">
                            <i class="fa-solid fa-circle-check me-1 text-muted" aria-hidden="true"></i>{{ __('Status') }}
                        </label>
                        <select id="port-filtro-status" name="status" class="form-select">
                            <option value="">{{ __('Todos') }}</option>
                            @foreach ($statusOpcoes as $valor => $rotulo)
                                <option value="{{ $valor }}"
                                    @selected((string) ($filterParams['status'] ?? '') === (string) $valor)>
                                    {{ $rotulo }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-12 col-lg-7">
                        <label for="port-filtro-q" class="form-label">
                            <i class="fa-solid fa-magnifying-glass me-1 text-muted" aria-hidden="true"></i>{{ __('Buscar') }}
                        </label>
                        <input id="port-filtro-q" type="text" class="form-control" name="q"
                            value="{{ $filterParams['q'] ?? '' }}"
                            placeholder="{{ __('Número ou ementa…') }}"
                            maxlength="255"
                            autocomplete="off">
                    </div>
                </div>
                <div class="leg-filter-actions d-flex flex-wrap justify-content-end align-items-center gap-2 mt-3">
                    <button class="btn btn-outline-success" type="submit">
                        <i class="fa-solid fa-filter me-1" aria-hidden="true"></i>{{ __('Filtrar') }}
                    </button>
                    <a href="{{ route('admin.legislacao.portarias.index', ['ano' => '', 'status' => '', 'q' => '']) }}"
                       class="btn btn-outline-secondary">{{ __('Limpar') }}</a>
                </div>
            </form>

            <div class="row">
                <div class="col-12">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-striped align-middle text-center">
                            <thead class="table-light">
                                <tr>
                                    <th>#</th>
                                    <th>
                                        @include('components.legislacao._listagem_sort_link', [
                                            'column' => 'nr_portaria',
                                            'label' => __('Número'),
                                        ])
                                    </th>
                                    <th>
                                        @include('components.legislacao._listagem_sort_link', [
                                            'column' => 'ds_ementa',
                                            'label' => __('Ementa'),
                                        ])
                                    </th>
                                    <th>
                                        @include('components.legislacao._listagem_sort_link', [
                                            'column' => 'dt_publicacao',
                                            'label' => __('Publicação'),
                                        ])
                                    </th>
                                    <th>{{ __('BO') }}</th>
                                    <th>
                                        @include('components.legislacao._listagem_sort_link', [
                                            'column' => 'cd_status',
                                            'label' => __('Status'),
                                        ])
                                    </th>
                                    <th>{{ __('Ações') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                    @php $i = $portarias->firstItem(); @endphp
                    @forelse ($portarias as $item)
                        <tr>
                            <td>{{ $i }}</td>
                            <td>{{ $item->nr_portaria }}</td>
                            <td>{{ \Illuminate\Support\Str::limit($item->ds_ementa ?? '', 80) }}</td>
                            <td>{{ $item->dt_publicacao ? $item->dt_publicacao->format('d/m/Y') : '-' }}</td>
                            <td>
                                @php
                                    $temBoPrincipalP = (bool) $item->boletim;
                                    $temBoRetificacaoP = $item->retificacoes->contains(fn ($r) => (bool) $r->boletim);
                                @endphp
                                @if ($item->boletim)
                                    <div class="mb-1">
                                        @include('components.legislacao._boletimNumeroLink', ['boletim' => $item->boletim])
                                    </div>
                                @endif
                                @foreach ($item->retificacoes as $ret)
                                    @if ($ret->boletim)
                                        <div class="d-flex flex-wrap align-items-center justify-content-center gap-1 @if (! $loop->first) mt-1 @endif">
                                            <span class="badge rounded-pill bg-warning-subtle text-dark small">
                                                {{ (int) $ret->cd_tipo === 1 ? __('Errata') : __('Republicação') }}
                                            </span>
                                            @include('components.legislacao._boletimNumeroLink', ['boletim' => $ret->boletim])
                                        </div>
                                    @endif
                                @endforeach
                                @if (! $temBoPrincipalP && ! $temBoRetificacaoP)
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            <td>{{ (int) $item->cd_status === 2 ? __('Publicado') : __('Pendente') }}</td>
                            <td>
                                <div class="d-flex justify-content-center gap-2">
                                    @if (auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_LEGISLACAO')['editar'])
                                        <a class="btn btn-sm btn-warning"
                                           href="{{ route('admin.legislacao.portarias.edit', [$item->id_portaria] + $filterParams) }}"
                                           title="{{ __('Editar') }}">
                                            <i class="fa-solid fa-pen-to-square"></i>
                                        </a>
                                    @endif
                                    @if (auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_LEGISLACAO')['excluir'])
                                        <form class="d-inline" method="post" action="{{ route('admin.legislacao.portarias.destroy', $item) }}"
                                            onsubmit="return confirmDelete(this)">
                                            @csrf
                                            @method('delete')
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value !== null && $value !== '')
                                                    <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <input type="hidden" name="confirm"
                                                value="{{ __('* Este registro será excluído e esta ação não poderá ser desfeita, deseja continuar?') }}">
                                            <button type="submit" class="btn btn-sm btn-danger" title="{{ __('Excluir') }}">
                                                <i class="fa-solid fa-trash"></i>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @php $i++; @endphp
                    @empty
                        <tr>
                            <td colspan="7" class="text-center">{{ __('Nenhum registro encontrado.') }}</td>
                        </tr>
                    @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="col-12 my-3">
                    <div class="d-flex justify-content-center">
                        {!! $portarias->links() !!}
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
