@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: Lista de Parques e Jardins')
@section('content')

    @include("errors.messageFlash")


    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">Lista de Parques e Jardins</h5>
            
        </div>
        <div class="card-body">
            <div class="d-flex">
                @include('components.buttons._newButton', ['url' => 'admin.parquesJardins.create', 'text' => 'Novo parques e jardins'])
            </div>
            <form id="searchAccounts" class="d-flex mb-4 pt-4" method="get">
                <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                <input type="text" class="form-control me-2" name="q" placeholder="Pesquisar parques e jardins municipal..."
                    value="{{ request()->input('q') }}">
                <button class="btn btn-outline-success" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
            </form>

            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped align-middle text-center">
                    @include('components.tables._thead', ['thead' => ['Foto', 'Nome', 'Pertence a', 'Bairro', 'Status', 'Ações']])
                    <tbody>
                        @php $index = 0 @endphp
                        @forelse($parques as $parque)
                            <tr>
                                <td>
                                    <img src="{{ asset($parque->foto) }}" alt="{{ $parque->nome }}" class="rounded-circle"
                                        style="width: 90px; height: 90px; object-fit: cover; cursor: pointer;"
                                        data-bs-toggle="modal" data-bs-target="#modalFotoEscola{{ $parque->id }}" />

                                    <!-- Modal exclusivo para esse paciente -->
                                    <div class="modal fade" id="modalFotoEscola{{ $parque->id }}" tabindex="-1"
                                        aria-hidden="true">
                                        <div class="modal-dialog modal-fullscreen">
                                            <div class="modal-content bg-dark">
                                                <div class="modal-header border-0">
                                                    <button type="button" class="btn-close btn-close-white ms-auto"
                                                        data-bs-dismiss="modal" aria-label="Fechar"></button>
                                                </div>
                                                <div class="modal-body d-flex justify-content-center align-items-center">
                                                    <img src="{{ asset($parque->foto) }}" alt="{{ $parque->nome }}"
                                                        class="img-fluid rounded" style="max-height: 90vh;" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td>{{ $parque->nome }}</td>
                                <td>{{ $parque->pertence->name }}</td>
                                <td>{{ $parque->endereco->bairro }}</td>
                                <td>
                                    @php
                                        $permissao = auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_PARQUES_E_JARDINS');
                                    @endphp
                                    @if($permissao && !empty($permissao['editar']) && $permissao['editar'])
                                        <form id="{{ "checkStatus_" . $index }}" class="fix"
                                            action="{{ route("admin.parquesJardins.status", $parque->id)}}" method="post">
                                            @csrf
                                            <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                            @if($parque->status == "ATIVADO")
                                                <div class="form-check form-switch d-flex justify-content-center">
                                                    <input type="hidden" name="checkStatus" value="DESATIVADO">
                                                    <input class="form-check-input" type="checkbox" checked
                                                        onclick="javascript:$('#{{ "checkStatus_" . $index }}').submit()">
                                                </div>
                                            @else
                                                <div class="form-check form-switch d-flex justify-content-center">
                                                    <input type="hidden" name="checkStatus" value="ATIVADO">
                                                    <input class="form-check-input" type="checkbox"
                                                        onclick="javascript:$('#{{ "checkStatus_" . $index }}').submit()">
                                                </div>
                                            @endif
                                        </form>
                                    @endif
                                </td>
                                <td class="text-nowrap text-center align-middle">
                                    <div class="d-flex justify-content-center flex-wrap gap-1">
                                        <a href="{{ route('admin.parquesJardins.edit', $parque->id) }}"
                                            class="btn btn-sm btn-warning" title="Editar"><i
                                                class="fa-solid fa-pen-to-square"></i>
                                        </a>

                                        <a href="{{ route("admin.parquesJardins.arquivos.index", $parque->id) }}"
                                            class="btn btn-sm btn-warning">
                                            <i class="fa-solid fa-cloud-arrow-up"></i>
                                        </a>

                                        @php
                                            $permissao = auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_PARQUES_E_JARDINS');
                                        @endphp
                                        @if ($permissao && !empty($permissao['excluir']) && $permissao['excluir'])
                                            <form class="d-inline" action="{{ route('admin.parquesJardins.destroy', $parque->id) }}"
                                                method="post" onsubmit="return confirmDelete(this)">
                                                @csrf
                                                @method('delete')
                                                <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                                <input type="hidden" name="confirm"
                                                    value="{{ __('* Este parque e jardim 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 $index++ @endphp
                        @empty
                            <tr>
                                <td colspan="8" class="text-muted fst-italic">* Nenhum registro encontrado!</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {!! $parques->links() !!}
            </div>
        </div>
    </div>
@endsection