@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: Lista de Pacientes com Fibromialgia')
@push('scripts')
    <script src="{{ asset('pmar/js/janelas.js') }}?v={{ time() }}"></script>
@endpush
@section('content')

<div class="card shadow-sm">

    @php $headLine = "<i class='fa-solid fa-user-injured'></i> Lista de Pacientes com Fibromialgia"; @endphp

    @include('components.buttons._headLineButtonsBackAdd', [
    'headLine' => __($headLine),
    'routeAdd' => 'admin.saude.fibromialgia.verify',
    'routeAddParams' => [],
    'nameAdd' => __('Novo paciente'),
    'routeOther' => "admin.saude.fibromialgia.chamados.abertos",
    'nameOther' => __('Chamados'),
    'fontawesomeOther' => "<i class='fa-solid fa-headset'></i>",
    "permission" => "GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA",
    ])

    @if (auth()->user()->getPermissaoDetalhes("GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA")['visualizar'])
    {{-- Modal de Exportação com formulário interno --}}
    <div class="modal fade" id="modalExportacao" tabindex="-1" aria-labelledby="modalExportacaoLabel"
         data-bs-backdrop="false">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form id="formRelatorio" action="{{ route('admin.saude.fibromialgia.relatorio') }}" method="POST" target="_blank">
                    @csrf
                    <input type="hidden" name="tipo_relatorio" id="tipoRelatorioInput">

                    <div class="modal-header bg-primary text-white">
                        <h5 class="modal-title" id="modalExportacaoLabel">{{ __('Título do Relatório') }}</h5>
                        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        <div id="bloco-carteirinha" class="row d-none">
                            @foreach ($carteirinhaStatuses as $status)
                            <div class="col-md-4 mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="carteirinha_paciente[]"
                                           value="{{ $status->name }}" id="carteirinha_{{ $status->name }}" checked>
                                    <label class="form-check-label" for="carteirinha_{{ $status->name }}">
                                        {{ $status->value }}
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>

                        <div id="bloco-cadastro" class="row d-none">
                            @foreach ($cadastroStatuses as $status)
                            <div class="col-md-4 mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="estado_cadastro[]"
                                           value="{{ $status->name }}" id="cadastro_{{ $status->name }}" checked>
                                    <label class="form-check-label" for="cadastro_{{ $status->name }}">
                                        {{ $status->value }}
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>

                        <div id="bloco-dados-cadastro" class="row d-none">
                            @foreach ($camposFibromialgia as $campo => $label)
                            <div class="col-md-4 mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="campos_dados[]"
                                           value="{{ $campo }}" id="campo_{{ $campo }}" checked>
                                    <label class="form-check-label" for="campo_{{ $campo }}">
                                        {{ $label }}
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-file-export"></i> {{ __('Exportar Selecionados') }}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <form id="formGerarCarteirinha" action="{{ route('admin.saude.fibromialgia.carteirinhas') }}" method="POST">
        @csrf

        <div class="modal fade" id="modalGerarCarteirinha" tabindex="-1" aria-labelledby="modalGerarCarteirinhaLabel"
             data-bs-backdrop="false">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">

                    <div class="modal-header bg-primary text-white">
                        <h5 class="modal-title" id="modalGerarCarteirinhaLabel">{{ __('Gerar Carteirinhas') }}</h5>
                        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                    </div>

                    <div class="modal-body">
                        @foreach ($pacientes as $p)
                        <input type="hidden" name="ids[]" value="{{ $p->id }}">
                        @endforeach

                        <div class="row">
                            @foreach ($carteirinhaStatuses as $status)
                            <div class="col-md-4 mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="carteirinha_paciente[]"
                                           value="{{ $status->name }}" id="carteirinha_{{ $status->name }}" checked>
                                    <label class="form-check-label" for="carteirinha_{{ $status->name }}">
                                        {{ $status->value }}
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" onclick="document.getElementById('formGerarCarteirinha').submit();">
                            <i class="fas fa-file-export"></i> {{ __('Exportar Selecionados') }}
                        </button>
                    </div>

                </div>
            </div>
        </div>
    </form>
    @endif


    <div class="card-body">

        @include("errors.messageFlash")

        @include('components/forms/_filtroPesquisaDefault', [
        "id" => "cadastroDeUsuario",
        "placeholder" => "Pesquisar por paciente com fibromialgia..."
        ])


        <x-tables._table :thead="['#','Nome', 'Email', 'Telefone', 'CPF', 'Situação Cadastral', 'Ações']" :pagination="$pacientes->links()">
            <x-slot name="body">
                @php $index = 0; @endphp
                @foreach($pacientes as $paciente)
                <tr>
                    <td>
                        @php
                            $pathSystem = $_SERVER['DOCUMENT_ROOT'] . "/";
                            $angraDefault = asset("/pmar/assets/img/icons/usuario.png");
                            $photo = file_exists( $pathSystem . optional($paciente->user)->photo ) ? optional($paciente->user)->photo : $angraDefault
                        @endphp

                        <img src="{{ asset($photo) }}"
                             alt="Foto de {{ $paciente->user->firstName }}" class="rounded-circle"
                             style="width: 90px; height: 90px; object-fit: cover; cursor: pointer;"
                             data-bs-toggle="modal" data-bs-target="#modalFotoPaciente{{ $paciente->id }}" />

                        <div class="modal fade" id="modalFotoPaciente{{ $paciente->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(optional($paciente->user)->photo ?? 'pmar/assets/img/icons/usuario.png') }}"
                                             alt="Foto de {{ $paciente->user->firstName }}"
                                             class="img-fluid rounded" style="max-height: 90vh;" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </td>
                    <td>{{ $paciente->user->name }}</td>
                    <td>{{ $paciente->user->email }}</td>
                    <td style="white-space: nowrap;">{{ $paciente->user->phoneNumber }}</td>
                    <td style="white-space: nowrap;">{{ $paciente->user->cpf }}</td>
                    <td class="text-start">
                        <div>
                            <strong>{{ __("Cadastro") }}:</strong>
                            <span class="badge bg-warning text-dark">{{ $paciente->status_cadastro }}</span>
                        </div>

                        <div>
                            <strong>{{ __("Status da carteirinha") }}:</strong>
                            <span
                                class="badge bg-danger text-dark">{{ strtoupper($paciente->carteirinha_paciente) }}</span>
                        </div>
                        <div>
                            <small>{{ $paciente->status_carteirinha_label }} (<a href="{{ route('admin.saude.fibromialgia.carteirinha', $paciente->id) }}" target="_blank">atualizar</a>)</small>
                        </div>
                    </td>
                    <td>
                        <div class="btn-group" role="group">

                            @if(auth()->user()->getPermissaoDetalhes("GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA")['adicionar'])
                            <button type="button" class="btn btn-sm btn-info text-white"
                                    data-bs-toggle="modal"
                                    data-bs-target="#modalNovoChamado{{ $paciente->id }}"
                                    title="{{ __('Novo Chamado') }}">
                                <i class="fa-solid fa-headset"></i>
                            </button>
                            @endif

                            @include('components.buttons._editButton', [
                            "route" => "admin.saude.fibromialgia.edit",
                            "id" => $paciente->id,
                            "title" => "Editar",
                            "fontawesome" => null,
                            "permission" => "GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA",
                            ])

                            <button
                                onclick="document.getElementById('formCarteirinhaIndividual_{{ $index }}').submit();"
                                class="btn btn-sm btn-dark text-white" title="Carteirinha">
                                <i class="fa-solid fa-address-card"></i>
                            </button>

                            <form id="formCarteirinhaIndividual_{{ $index }}"
                                  action="{{ route('admin.saude.fibromialgia.carteirinhas', $paciente->id) }}" method="POST"
                                  target="_blank" style="display: none;">
                                @csrf
                                <input type="hidden" name="ids[]" value="{{ $paciente->id }}">
                            </form>

                            @include('components.buttons._editButton', [
                            "route" => "admin.saude.fibromialgia.anotacoes",
                            "id" => $paciente->id,
                            "title" => "Anotações",
                            "fontawesome" => "<i class='fa-solid fa-clipboard'></i>",
                            "permission" => "GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA",
                            ])

                            @include('components.buttons._deleteButton', [
                            "message" => __("* Este paciente será excluído do cadastro de pacientes com fibromialgia e esta ação não poderá ser desfeita, deseja continuar?"),
                            "route" => "admin.saude.fibromialgia.destroy",
                            "id" => $paciente->id,
                            "title" => "Excluir",
                            "fontawesome" => null,
                            "permission" => "GERENCIADOR_DO_CADASTRO_DE_PACIENTES_COM_FIBROMIALGIA",
                            ])

                        </div>
                    </td>
                </tr>

                @php $index++; @endphp
                @endforeach

            </x-slot>
        </x-tables._table>

        @foreach($pacientes as $paciente)
        <!-- Modal Novo Chamado -->
        <div class="modal fade" id="modalNovoChamado{{ $paciente->id }}" tabindex="-1" aria-labelledby="modalNovoChamadoLabel{{ $paciente->id }}" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary text-white">
                        <h5 class="modal-title" id="modalNovoChamadoLabel{{ $paciente->id }}">
                            <i class="fa-solid fa-headset"></i> {{ __('Novo Chamado') }} - {{ $paciente->user->name }}
                        </h5>
                        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <form action="{{ route('admin.saude.fibromialgia.chamados.create', $paciente->id) }}" method="POST">
                        @csrf
                        <div class="modal-body">
                            @include("errors.messageFlash")

                            <div class="mb-3">
                                <label for="assunto{{ $paciente->id }}" class="form-label">{{ __('Assunto') }} <span class="text-danger">*</span></label>
                                <input type="text"
                                       class="form-control @error('assunto') is-invalid @enderror"
                                       id="assunto{{ $paciente->id }}"
                                       name="assunto"
                                       value="{{ old('assunto') }}"
                                       required
                                       maxlength="255"
                                       placeholder="{{ __('Digite o assunto do chamado') }}">
                                @error('assunto')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('Cancelar') }}</button>
                            <button type="submit" class="btn btn-primary">
                                <i class="fa-solid fa-plus"></i> {{ __('Criar Chamado') }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        @endforeach

    </div>
</div>
@endsection
