@extends('layouts.landing')

@section('title',"Editar")

@section('content')

<div class="container-fluid">
    {{-- Card editar grupo --}}
    <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">Editar grupo</h6>
                <a href="{{route('grupo.index')}}" class="d-sm-inline-block btn btn-sm btn-primary shadow-sm">
                    <i class="fas fa-download fa-sm text-white-50"></i> Regresar
                </a>
            </div>
        </div>

        <div class="card-body">
            @if (session('info'))
                <div class="alert alert-success">
                    <strong>{{session('info')}}</strong>
                </div>
            @endif
            <form action="{{route('grupo.update',$grupo->id)}}" method="POST">
                @method('PUT')
                @csrf
                <div class="mb-3">
                    <label>Carrera</label>
                    <select class="form-control" name="plan_id" disabled>
                        <option value="">Seleccionar plan</option>
                        @foreach ($plans as $nivel => $grupos)
                            <optgroup label="{{ $nivel }}">
                                @foreach ($grupos as $plan)
                                    <option value="{{ $plan->id }}" {{ $plan->id == $grupo->plan_id ? 'selected' : '' }}>
                                        {{ $plan->carreraRel->name }}
                                        ({{ $plan->name }}-{{ $plan->rvoe }})
                                    </option>
                                @endforeach
                            </optgroup>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label>Clave</label>
                    <input type="text" class="form-control" name="clave" value="{{ $grupo->clave }}" disabled>
                </div>

                <div class="mb-3">
                    <label>ciclo</label>
                    <select class="form-control" name="ciclo">
                        <option value="0">Seleccionar ciclo</option>
                        @for ($i=1 ; $i <= $ciclos->duracion ; $i++)
                            <option value="{{ $i }}" @selected($i == $grupo->ciclo)>{{$i}}</option>
                        @endfor
                    </select>
                </div>

                <div class="mb-3">
                    <label>turno</label>
                    <select class="form-control" name="turno">
                        <option value="0">Seleccionar turno</option>
                        @foreach ($turnos as $turno)
                            <option value="{{ $turno->id }}" {{ $turno->id == $grupo->turno ? 'selected' : '' }}>
                                {{$turno->name}}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label>plantel</label>
                    <select class="form-control" name="plantel" disabled>
                        <option value="0">Seleccionar plantel</option>
                        @foreach ($planteles as $plantel)
                            <option value="{{ $plantel->id }}" {{ $plantel->id == $grupo->plantel ? 'selected' : '' }}>
                                {{$plantel->name}}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label>status</label>
                    <select class="form-control" name="status">
                        <option value="1" {{ 1 == $grupo->status ? 'selected' : '' }}>Activo</option>
                        <option value="0" {{ 0 == $grupo->status ? 'selected' : '' }}>Inactivo</option>
                    </select>
                </div>

                <input class="btn btn-primary" type="submit" value="Guardar">
            </form>
        </div>
    </div>

    {{-- Card Materias y Docentes --}}
    <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">Materias</h6>
                @hasrole('Coordinación académica')
                    <button onclick="$('#modalDocente').modal('show')"
                        class="d-sm-inline-block btn btn-sm btn-success shadow-sm">
                        <i class="fas fa-plus fa-sm text-white-50"></i> Asignar materia a docente
                    </button>
                @endcan
            </div>
        </div>

        <div class="card-body">
            <livewire:grupo-docente :grupo="$grupo"/>
        </div>
    </div>

    {{-- Card Alumnos --}}
    <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">Alumnos</h6>
                    @hasrole('Control escolar')
                        <button onclick="Livewire.dispatch('create-grupo')"
                            class="d-sm-inline-block btn btn-sm btn-success shadow-sm">
                            <i class="fas fa-plus fa-sm text-white-50"></i> Agregar alumno
                        </button>
                    @endhasrole
            </div>
        </div>

        <div class="card-body">
            <div class="container-fluid">
                <table id="tcont" class="table table-striped table-bordered nowrap table-hover" style="width:100%;">
                    <thead>
                        <tr>
                            {{-- <th>Id</th> --}}
                            <th>Nombre</th>
                            @hasrole('Control escolar')
                                <th>Opciones</th>
                            @endhasrole

                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($grupo->alumnos as $alumno)
                            <tr>
                                {{-- <td>{{$alumno->id}}</td> --}}
                                <td>
                                    {{$alumno->usuario->first()?->apellidoPaterno}}
                                    {{$alumno->usuario->first()?->apellidoMaterno}}
                                    {{$alumno->usuario->first()?->name}}
                                </td>

                                @hasrole('Control escolar')
                                <td>

                                    <form class="delete-form"
                                        action="{{route('grupo.destroy',$grupo->id)}}"
                                        method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <input class="d-none" name="alumno" value="{{$alumno->id}}">
                                        <button type="submit" style="border:none; background:none;">
                                            <i class="fa-regular fa-circle-user-circle-minus fa-2xl" style="color: rgb(255, 0, 0);"></i>
                                        </button>
                                    </form>

                                </td>
                                    @endhasrole


                            </tr>
                        @empty
                            <tr>Sin alumnos.</tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <livewire:grupo-alumno :grupo="$grupo"/>
        </div>
    </div>
</div>

@endsection

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

<script>
document.addEventListener('livewire:init', () => {
    Livewire.on('open-grupo-modal', () => {
        $('#exampleModal').modal('show');
    });
    Livewire.on('close-modal', () => {
        $('#exampleModal').modal('hide');
    });
});
</script>
@endsection
