@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: Arquivos do ' . $parque->nome)
@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">{{ 'Arquivos do ' . $parque->nome }}</h5>
            <div class="d-flex">
                <button class="btn btn-light btn-sm me-2" onclick="window.location.href='{{ route('admin.parquesJardins.index') }}'">
                    <i class="fa-solid fa-arrow-left"></i> Voltar
                </button>
                <button class="btn btn-light btn-sm" onclick="window.location.href='{{ route('admin.parquesJardins.arquivos.create', $parque->id) }}'">
                    <i class="fa-solid fa-plus"></i> Novo arquivo
                </button>
            </div>
        </div>
        <div class="card-body">
            <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>Tipo</th>
                            <th>Data</th>
                            <th>Pertence a</th>
                            <th>Nome</th>
                            <th>Status</th>
                            <th>Ações</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php $index = 0 @endphp
                        @forelse($arquivos as $arquivo)
                            <tr>
                                <td>
                                    @php
                                        $extensoesImagem = ['jpg', 'jpeg', 'png', 'gif'];
                                        $extensao = pathinfo($arquivo->link, PATHINFO_EXTENSION);
                                    @endphp
                                    @if (in_array(strtolower($extensao), $extensoesImagem))

                                        <button type="button" style="border: 0; background" class="btn" data-bs-toggle="modal" data-bs-target="#open_{{ $index }}">
                                            <img src="{{ asset($arquivo->link) }}" alt="{{ $arquivo->nome }}" title="{{ $arquivo->nome }}" width="90px" class="img-fluid rounded-2" width="90px">
                                        </button>

                                        <div class="modal fade" id="open_{{ $index }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog modal-xl">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h1 class="modal-title fs-5" id="exampleModalLabel">Visualizar avatar</h1>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <img src="{{ asset($arquivo->link) }}" alt="{{ $arquivo->nome }}" title="{{ $arquivo->nome }}" class="img-fluid rounded-2">
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary"
                                                        data-bs-dismiss="modal">Fechar</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    @else
                                        <span class="text-muted">Arquivo não suportado</span>
                                    @endif
                                </td>
                                <td>{{ $arquivo->tipo }}</td>
                                <td>{{ date("d/m/Y", strtotime($arquivo->evento_at)) }}</td>
                                <td>
                                    <a href="{{ route("admin.parquesJardins.edit", $parque->id) }}" class="text-dark">
                                        {{ $arquivo->parque->nome }}
                                    </a>
                                </td>
                                <td>{{ $arquivo->nome }}</td>
                                <td>
                                    <form id="{{ "checkStatus_" . $index }}" class="fix"
                                  action="{{ route("admin.parquesJardins.arquivos.status", [$parque->id, $arquivo->id])}}" method="post">
                                        @csrf
                                        <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                        @if($arquivo->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>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        @if (in_array('DELETE', explode(',', auth()->user()->papers)))
                                            <form class="d-inline" action="{{ route('admin.parquesJardins.arquivos.destroy', [$parque->id, $arquivo->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 arquivo 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">
                {!! $arquivos->links() !!}
            </div>
        </div>
    </div>
@endsection