@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Lista de Creches e Unidades Escolares'))
@section('content')

    @include("errors.messageFlash")

    @php $permissao = auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_ESCOLAS'); @endphp

    <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 Creches e Unidades Escolares') }}</h5>
        </div>
        <div class="card-body">

            @if($permissao['adicionar'])
                <div class="d-flex">
                    @include('components.buttons._newButton', ['url' => 'admin.escolas.create', 'text' => __('Nova unidade')])
                </div>
            @endif

            <form id="searchEscolas" 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 unidade escolar...') }}"
                    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'),
                            __('Email'),
                            __('Telefone'),
                            __('Tipo'),
                            __('Possui Recursos Multifuncionais'),
                            __('Status'),
                            __('Ações'),
                        ]
                    ])
                    <tbody>
                        @php $index = 0; @endphp
                        @forelse($escolas as $escola)
                            <tr>
                                <td>
                                    <img src="{{ asset($escola->foto) }}" alt="{{ $escola->nome }}"
                                        class="rounded-circle"
                                        style="width: 90px; height: 90px; object-fit: cover; cursor: pointer;"
                                        data-bs-toggle="modal"
                                        data-bs-target="#modalFotoEscola{{ $escola->id }}" />

                                    <div class="modal fade" id="modalFotoEscola{{ $escola->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($escola->foto) }}"
                                                        alt="{{ $escola->nome }}"
                                                        class="img-fluid rounded"
                                                        style="max-height: 90vh;" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td>{{ $escola->nome }}</td>
                                <td>{{ optional($escola->pertence)->name ?? __('Não informado') }}</td>
                                <td>{{ optional($escola->endereco)->bairro ?? __('Não informado') }}</td>
                                <td>{{ $escola->email }}</td>
                                <td>{{ $escola->telefone }}</td>
                                <td>{{ $escola->tipoUnidadeEscolar }}</td>
                                <td>
                                    @if($escola->possuiRecursosMultifuncionais)
                                        <i class="fa-solid fa-circle-check text-success"></i>
                                    @else
                                        <i class="fa-solid fa-circle-xmark text-danger"></i>
                                    @endif
                                </td>
                                <td>
                                    @if($permissao['editar'])
                                        <form id="{{ 'checkStatus_' . $index }}" class="fix"
                                            action="{{ route('admin.escolas.status', $escola->id) }}"
                                            method="post">
                                            @csrf
                                            <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                            @if(request()->filled('q'))
                                                <input type="hidden" name="q" value="{{ request()->input('q') }}">
                                            @endif
                                            @if($escola->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>
                                    @else
                                        <span class="badge {{ $escola->status === 'ATIVADO' ? 'bg-success' : 'bg-secondary' }}">
                                            {{ $escola->status === 'ATIVADO' ? __('Ativado') : __('Desativado') }}
                                        </span>
                                    @endif
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        @if($permissao['editar'])
                                            <a href="{{ route('admin.escolas.edit', $escola->id) }}"
                                                class="btn btn-sm btn-warning"
                                                title="{{ __('Editar') }}">
                                                <i class="fa-solid fa-pen-to-square"></i>
                                            </a>
                                        @endif

                                        @if($permissao['excluir'])
                                            <form class="d-inline"
                                                action="{{ route('admin.escolas.destroy', $escola->id) }}"
                                                method="post"
                                                onsubmit="return confirmDelete(this)">
                                                @csrf
                                                @method('delete')
                                                <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                                @if(request()->filled('q'))
                                                    <input type="hidden" name="q" value="{{ request()->input('q') }}">
                                                @endif
                                                <input type="hidden" name="confirm"
                                                    value="{{ __('* Esta escola será excluída 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="10" class="text-muted fst-italic">
                                    {{ __('* Nenhum registro encontrado!') }}
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

@endsection
