@extends('layouts.landing')

@section('head')
    @include('layouts._partials.tablaStyle')
@endsection

@section('title', 'Detalle de Tarea')

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

    {{-- Info de la tarea --}}
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <div class="d-sm-flex align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold text-primary">
                    {{ $tarea->titulo }}
                    &mdash; {{ $material->name }} &middot; Grupo: {{ $grupo->clave }}
                </h6>
                <a href="{{ route('grupo.material.tareas', [$grupo, $material]) }}"
                   class="btn btn-sm btn-secondary shadow-sm">
                    <i class="fas fa-arrow-left fa-sm"></i> Regresar
                </a>
            </div>
        </div>
        <div class="card-body">
            @if (session('info'))
                <div class="alert alert-success"><strong>{{ session('info') }}</strong></div>
            @endif
            @if ($tarea->descripcion)
                <p class="mb-2"><strong>Instrucciones:</strong> {{ $tarea->descripcion }}</p>
            @endif
            @if ($tarea->fecha_entrega)
                <p class="mb-0">
                    <strong>Fecha límite:</strong>
                    {{ \Carbon\Carbon::parse($tarea->fecha_entrega)->format('d/m/Y') }}
                    @if (\Carbon\Carbon::parse($tarea->fecha_entrega)->isPast())
                        <span class="badge badge-danger ml-1">Vencida</span>
                    @else
                        <span class="badge badge-success ml-1">Activa</span>
                    @endif
                </p>
            @endif
        </div>
    </div>

    {{-- Entregas de alumnos --}}
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Entregas de alumnos</h6>
        </div>
        <div class="card-body table-responsive">
            <table id="tcont" class="table table-striped table-bordered table-hover" style="width:100%">
                <thead>
                    <tr>
                        <th>Alumno</th>
                        <th>Status</th>
                        <th>Entrega</th>
                        @hasrole('Docente')
                            <th>Calificar / Calificación</th>
                        @endhasrole
                        @hasrole('Coordinación académica')
                            <th>Calificación</th>
                        @endhasrole
                    </tr>
                </thead>
                <tbody>
                    @foreach ($alumnos as $alumno)
                        @php $entrega = $entregas[$alumno->id] ?? null; @endphp
                        <tr>
                            <td class="align-middle">
                                {{ $alumno->alumnos->first()?->apellidoPaterno }}
                                {{ $alumno->alumnos->first()?->apellidoMaterno }}
                                {{ $alumno->alumnos->first()?->name }}
                            </td>

                            <td class="align-middle">
                                @if (!$entrega || $entrega->status === 'sin_subir')
                                    <span class="badge badge-secondary">Sin subir</span>
                                @elseif ($entrega->status === 'pendiente')
                                    <span class="badge badge-warning">Pendiente</span>
                                @else
                                    <span class="badge badge-success">Calificada</span>
                                @endif
                            </td>

                            <td class="align-middle" style="max-width:280px">
                                @if ($entrega && ($entrega->texto || $entrega->archivo))
                                    @if ($entrega->texto)
                                        <div class="border rounded p-2 bg-light mb-1"
                                             style="max-height:100px;overflow-y:auto;font-size:.85rem;white-space:pre-wrap;">{{ $entrega->texto }}</div>
                                    @endif
                                    @if ($entrega->archivo)
                                        <a href="{{ route('files.serve', $entrega->archivo) }}"
                                           target="_blank" class="btn btn-sm btn-outline-info">
                                            <i class="fas fa-file fa-sm"></i> Ver archivo
                                        </a>
                                    @endif
                                @else
                                    <span class="text-muted small">Sin entrega</span>
                                @endif
                            </td>

                            {{-- Docente: puede calificar --}}
                            @hasrole('Docente')
                            <td class="align-middle" style="min-width:300px">
                                @if ($entrega && in_array($entrega->status, ['pendiente', 'calificada']))
                                    <form action="{{ route('tarea.calificar', [$tarea, $alumno]) }}" method="POST"
                                          class="d-flex align-items-center flex-wrap" style="gap:4px">
                                        @csrf
                                        <input type="number" name="calificacion" min="0" max="10" step="0.1"
                                               class="form-control form-control-sm" style="width:80px"
                                               placeholder="0–10"
                                               value="{{ $entrega->status === 'calificada' ? $entrega->calificacion : '' }}"
                                               required>
                                        <input type="text" name="comentario" class="form-control form-control-sm"
                                               style="width:140px" placeholder="Comentario"
                                               value="{{ $entrega->comentario ?? '' }}">
                                        <button type="submit"
                                                class="btn btn-sm {{ $entrega->status === 'calificada' ? 'btn-warning' : 'btn-success' }}">
                                            <i class="fas fa-{{ $entrega->status === 'calificada' ? 'redo' : 'check' }} fa-sm"></i>
                                            {{ $entrega->status === 'calificada' ? 'Recalificar' : 'Calificar' }}
                                        </button>
                                    </form>
                                @else
                                    <span class="text-muted small">Sin entrega aún</span>
                                @endif
                            </td>
                            @endhasrole

                            {{-- Coordinación: solo lectura de calificación --}}
                            @hasrole('Coordinación académica')
                            <td class="align-middle">
                                @if ($entrega?->status === 'calificada')
                                    <strong class="text-success">{{ number_format($entrega->calificacion, 1) }}</strong>
                                    @if ($entrega->comentario)
                                        <br><small class="text-muted">{{ $entrega->comentario }}</small>
                                    @endif
                                @else
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            @endhasrole
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

</div>
@endsection

@section('scripts')
    @include('layouts._partials.tablaScript')
@endsection
