@extends('layouts.landing')

@section('title', 'Reporte — ' . $evaluacion->titulo)

@section('content')
<div class="container-fluid">

    {{-- Header --}}
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <div>
            <h5 class="m-0 font-weight-bold text-primary">
                <i class="fas fa-chart-bar mr-1"></i> {{ $evaluacion->titulo }}
            </h5>
            <small class="text-muted">Reporte de resultados</small>
        </div>
        <div class="d-flex flex-wrap" style="gap:.4rem;">
            <a href="{{ route('evaluacion.reporte.pdf', $evaluacion) }}"
               class="btn btn-sm btn-danger shadow-sm">
                <i class="fas fa-file-pdf mr-1"></i> Exportar PDF
            </a>
            <a href="{{ route('evaluacion.show', $evaluacion) }}"
               class="btn btn-sm btn-secondary shadow-sm">
                <i class="fas fa-arrow-left mr-1"></i> Regresar
            </a>
        </div>
    </div>

    {{-- Tarjetas de resumen --}}
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Participantes</div>
                            <div class="h4 mb-0 font-weight-bold text-gray-800">{{ $totalSubmissions }}</div>
                        </div>
                        <div class="col-auto"><i class="fas fa-users fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Preguntas</div>
                            <div class="h4 mb-0 font-weight-bold text-gray-800">{{ count($datos) }}</div>
                        </div>
                        <div class="col-auto"><i class="fas fa-list-ol fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Estado</div>
                            <div class="h4 mb-0 font-weight-bold text-gray-800">{{ ucfirst($evaluacion->status) }}</div>
                        </div>
                        <div class="col-auto"><i class="fas fa-clipboard-check fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Dirigida a</div>
                            <div class="h4 mb-0 font-weight-bold text-gray-800">{{ $evaluacion->tipo?->nombre }}</div>
                        </div>
                        <div class="col-auto"><i class="fas fa-tag fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- Sin respuestas --}}
    @if($totalSubmissions === 0)
        <div class="alert alert-info text-center">
            <i class="fas fa-info-circle fa-2x mb-2 d-block"></i>
            Esta evaluación aún no tiene respuestas registradas.
        </div>
    @endif

    {{-- Preguntas --}}
    @foreach($datos as $i => $item)
    @php $pregunta = $item['pregunta']; @endphp

    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex align-items-center justify-content-between flex-wrap" style="gap:.5rem;">
            <h6 class="m-0 font-weight-bold text-primary">
                {{ $i + 1 }}. {{ $pregunta->texto }}
                @if($pregunta->es_docente)
                    <span class="badge badge-info ml-1">
                        <i class="fas fa-chalkboard-teacher mr-1"></i>Por docente
                    </span>
                @endif
                @if($pregunta->requerida)
                    <span class="text-danger ml-1" title="Obligatoria">*</span>
                @endif
            </h6>
            <div class="d-flex align-items-center" style="gap:.5rem;">
                <span class="badge badge-light border">{{ $pregunta->tipo?->nombre }}</span>
                @if(!$item['es_docente'] && isset($item['promedio']))
                    <span class="badge badge-primary px-3">
                        Promedio: {{ $item['promedio'] }}
                    </span>
                @endif
            </div>
        </div>
        <div class="card-body">

            {{-- Texto libre --}}
            @if($item['tipo'] === 'texto_libre')

                @if(!$item['es_docente'])
                    @forelse($item['textos'] as $txt)
                        <div class="alert alert-light border mb-2 py-2 px-3">
                            <i class="fas fa-quote-left text-muted mr-2 small"></i>{{ $txt }}
                        </div>
                    @empty
                        <p class="text-muted mb-0"><em>Sin respuestas registradas.</em></p>
                    @endforelse
                @else
                    @foreach($item['por_docente'] as $pd)
                    <div class="mb-4">
                        <h6 class="font-weight-bold text-info mb-2">
                            <i class="fas fa-chalkboard-teacher mr-1"></i>{{ $pd['docente_nombre'] }}
                            <small class="text-muted font-weight-normal">({{ $pd['total'] }} respuesta(s))</small>
                        </h6>
                        @forelse($pd['textos'] as $txt)
                            <div class="alert alert-light border mb-2 py-2 px-3">
                                <i class="fas fa-quote-left text-muted mr-2 small"></i>{{ $txt }}
                            </div>
                        @empty
                            <p class="text-muted small"><em>Sin respuestas.</em></p>
                        @endforelse
                    </div>
                    @endforeach
                @endif

            {{-- Gráfica única --}}
            @elseif(!$item['es_docente'])
                @if($item['total'] > 0)
                    <div class="row align-items-center">
                        <div class="col-lg-8">
                            <canvas id="chart-{{ $i }}"
                                    height="{{ in_array($item['tipo'], ['si_no']) ? 200 : 160 }}">
                            </canvas>
                        </div>
                        <div class="col-lg-4 text-center">
                            <p class="mb-2 text-muted">
                                <i class="fas fa-poll mr-1"></i>
                                <strong>{{ $item['total'] }}</strong> respuesta(s)
                            </p>
                            @if(isset($item['promedio']))
                                <div class="h4 font-weight-bold text-primary mb-0">
                                    {{ $item['promedio'] }}
                                </div>
                                <small class="text-muted">promedio</small>
                            @endif
                        </div>
                    </div>
                @else
                    <p class="text-muted"><em>Sin respuestas registradas.</em></p>
                @endif

            {{-- Gráficas por docente --}}
            @else
                @if(empty($item['por_docente']))
                    <p class="text-muted"><em>Sin respuestas registradas.</em></p>
                @else
                    <div class="row">
                        @foreach($item['por_docente'] as $di => $pd)
                        <div class="col-md-6 mb-4">
                            <div class="card border h-100">
                                <div class="card-header py-2 bg-light d-flex align-items-center justify-content-between">
                                    <span class="font-weight-bold text-info small">
                                        <i class="fas fa-chalkboard-teacher mr-1"></i>{{ $pd['docente_nombre'] }}
                                    </span>
                                    @if(isset($pd['promedio']))
                                        <span class="badge badge-primary">Prom: {{ $pd['promedio'] }}</span>
                                    @endif
                                </div>
                                <div class="card-body py-2">
                                    @if($pd['total'] > 0)
                                        <canvas id="chart-{{ $i }}-{{ $di }}" height="200"></canvas>
                                        <p class="text-center text-muted small mt-2 mb-0">
                                            {{ $pd['total'] }} respuesta(s)
                                        </p>
                                    @else
                                        <p class="text-muted text-center small py-3"><em>Sin respuestas.</em></p>
                                    @endif
                                </div>
                            </div>
                        </div>
                        @endforeach
                    </div>
                @endif
            @endif

        </div>
    </div>
    @endforeach

</div>
@endsection

@section('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
Chart.defaults.font.family = "'Nunito', sans-serif";
Chart.defaults.font.size   = 13;

@foreach($datos as $i => $item)
    @if($item['tipo'] !== 'texto_libre' && $item['total'] > 0)
        @if(!$item['es_docente'] && isset($item['chart_config']))
            new Chart(
                document.getElementById('chart-{{ $i }}'),
                @json($item['chart_config'])
            );
        @elseif($item['es_docente'])
            @foreach($item['por_docente'] as $di => $pd)
                @if(isset($pd['chart_config']) && $pd['total'] > 0)
                new Chart(
                    document.getElementById('chart-{{ $i }}-{{ $di }}'),
                    @json($pd['chart_config'])
                );
                @endif
            @endforeach
        @endif
    @endif
@endforeach
</script>
@endsection
