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

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

    <div class="card shadow-sm">
        @php $headLine = "<i class='fa-solid fa-file-signature'></i> " . __('Atas'); @endphp
        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' => __($headLine),
            'routeBack' => 'admin.dashboard',
            'routeBackParams' => [],
            'nameBack' => __('Voltar'),
            'routeAdd' => 'admin.atas.create',
            'routeAddParams' => request()->only(\App\Http\Resources\AtaResource::ADMIN_FILTER_KEYS),
            'nameAdd' => __('Nova ata'),
            'permission' => 'GERENCIADOR_DE_ATAS',
        ])

        <div class="card-body">
            <form method="get" class="row g-3 mb-3">
                <div class="col-12 col-lg-4">
                    <label class="form-label">{{ __('Busca geral') }}</label>
                    <input type="text" class="form-control" name="q" value="{{ request('q') }}"
                           placeholder="{{ __('Número, objeto ou processo') }}">
                </div>
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label">{{ __('Nº da ata') }}</label>
                    <input type="text" class="form-control" name="nr_ata" value="{{ request('nr_ata') }}"
                           placeholder="{{ __('Ex.: 012/2026') }}">
                </div>
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label">{{ __('Nº do processo') }}</label>
                    <input type="text" class="form-control" name="nr_processo" value="{{ request('nr_processo') }}"
                           placeholder="{{ __('Ex.: 2026.001234') }}">
                </div>
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label">{{ __('Data') }}</label>
                    <input type="date" class="form-control" name="dt_data_ata" value="{{ request('dt_data_ata') }}">
                </div>
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label">{{ __('Secretarias') }}</label>
                    <select class="form-select" name="id_secretaria_ata">
                        <option value="">{{ __('Todas') }}</option>
                        @foreach($secretarias as $secretaria)
                            <option value="{{ $secretaria->id }}" @selected((string) request('id_secretaria_ata') === (string) $secretaria->id)>
                                {{ $secretaria->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-12 d-flex justify-content-end gap-2">
                    <a href="{{ route('admin.atas.index') }}" class="btn btn-outline-secondary px-4">{{ __('Limpar') }}</a>
                    <button class="btn btn-success px-4" type="submit">{{ __('Filtrar') }}</button>
                </div>
            </form>

            <x-tables._table :thead="['#', __('Número'), __('Objeto'), __('Processo'), __('Licitação'), __('Secretaria'), __('Data'), __('Ações')]" :pagination="$atas->links()">
                <x-slot name="body">
                    @php $index = $atas->firstItem(); @endphp
                    @forelse($atas as $ata)
                        <tr>
                            <td>{{ $index }}</td>
                            <td>{{ $ata->nr_ata }}</td>
                            <td>{{ $ata->ds_objeto }}</td>
                            <td>{{ $ata->nr_processo }}</td>
                            <td>{{ $ata->licitacao->nr_licitacao ?? '-' }}</td>
                            <td>
                                {{
                                    $ata->secretariasAtas->isNotEmpty()
                                        ? $ata->secretariasAtas->pluck('name')->implode(', ')
                                        : ($ata->secretaria->name ?? '-')
                                }}
                            </td>
                            <td>{{ \Carbon\Carbon::parse($ata->dt_data_ata)->format('d/m/Y') }}</td>
                            <td>
                                <div class="d-flex justify-content-center gap-2">
                                    @if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_ATAS')['editar'])
                                        <a class="btn btn-sm btn-warning"
                                           href="{{ route('admin.atas.edit', [$ata->id_ata] + request()->only(\App\Http\Resources\AtaResource::ADMIN_FILTER_KEYS)) }}">
                                            <i class="fa-solid fa-pen-to-square"></i>
                                        </a>
                                    @endif
                                    @if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_ATAS')['excluir'])
                                        <form class="d-inline" action="{{ route('admin.atas.destroy', $ata->id_ata) }}" method="post" onsubmit="return confirmDelete(this)">
                                            @csrf
                                            @method('delete')
                                            @foreach(request()->only(\App\Http\Resources\AtaResource::ADMIN_FILTER_KEYS) as $key => $value)
                                                @if(is_array($value))
                                                    @foreach($value as $item)
                                                        <input type="hidden" name="{{ $key }}[]" value="{{ $item }}">
                                                    @endforeach
                                                @else
                                                    <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">
                                                <i class="fa-solid fa-trash"></i>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @php $index++; @endphp
                    @empty
                        <tr>
                            <td colspan="8" class="text-center">{{ __('Nenhum registro encontrado.') }}</td>
                        </tr>
                    @endforelse
                </x-slot>
            </x-tables._table>
        </div>
    </div>
@endsection

