@extends('layouts.landing')

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

@section('title', 'Tareas')

@section('content')
<div class="container-fluid">
    <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">
                    Tareas &mdash; {{ $material->name }} &middot; Grupo: {{ $grupo->clave }}
                </h6>
                <div>
                    {{-- Botón regresar según rol --}}
                    @hasrole('Coordinación académica')
                        <a href="{{ route('grupo.ver.materias', $grupo) }}" class="btn btn-sm btn-secondary shadow-sm mr-2">
                            <i class="fas fa-arrow-left fa-sm"></i> Regresar
                        </a>
                    @else
                        <a href="{{ route('grupo.administrar', $grupo) }}" class="btn btn-sm btn-secondary shadow-sm mr-2">
                            <i class="fas fa-arrow-left fa-sm"></i> Regresar
                        </a>
                        <a href="{{ route('grupo.material.tareas.acta', [$grupo, $material]) }}"
                           class="btn btn-sm btn-success shadow-sm mr-2">
                            <i class="fas fa-file-alt fa-sm text-white-50"></i> Generar Acta
                        </a>
                        <a href="{{ route('grupo.material.tareas.create', [$grupo, $material]) }}"
                           class="btn btn-sm btn-primary shadow-sm">
                            <i class="fas fa-plus fa-sm text-white-50"></i> Nueva Tarea
                        </a>
                    @endhasrole
                </div>
            </div>
        </div>

        <div class="card-body">

            @if (session('info'))
                <div class="alert alert-success"><strong>{{ session('info') }}</strong></div>
            @endif

            @if ($tareas->isEmpty())
                <div class="alert alert-info">Aún no hay tareas para esta materia.</div>
            @else
                <table id="tcont" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>Título</th>
                            <th>Fecha límite</th>
                            <th>Pendientes</th>
                            <th>Calificadas</th>
                            <th>Total alumnos</th>
                            <th>Opciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($tareas as $tarea)
                            <tr>
                                <td>{{ $tarea->titulo }}</td>
                                <td>
                                    @if ($tarea->fecha_entrega)
                                        {{ \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
                                    @else
                                        <span class="text-muted">Sin fecha</span>
                                    @endif
                                </td>
                                <td><span class="badge badge-warning">{{ $tarea->pendientes_count }}</span></td>
                                <td><span class="badge badge-success">{{ $tarea->calificadas_count }}</span></td>
                                <td>{{ $tarea->entregas_count }}</td>
                                <td>
                                    <a href="{{ route('grupo.material.tareas.show', [$grupo, $material, $tarea]) }}"
                                       class="btn btn-sm btn-info">
                                        <i class="fas fa-eye"></i> Ver
                                    </a>
                                    @hasrole('Docente')
                                        <a href="{{ route('grupo.material.tareas.edit', [$grupo, $material, $tarea]) }}"
                                           class="btn btn-sm btn-warning">
                                            <i class="fas fa-edit"></i> Editar
                                        </a>
                                        <form class="delete-form d-inline"
                                              action="{{ route('grupo.material.tareas.destroy', [$grupo, $material, $tarea]) }}"
                                              method="POST">
                                            @csrf @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-danger">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    @endhasrole
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            @endif

        </div>
    </div>
</div>
@endsection

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