@extends('layouts.app')

@section('title', $professor->full_name)

@section('content')
    <div class="mdc-layout-grid__inner">
        <h1 class="mdc-layout-grid__cell mdc-typography--heading1">{{ $professor->full_name }}</h1>
    </div>
    <br>
    <div class="mdc-layout-grid__inner">
        <h5 class="mdc-layout-grid__cell mdc-typography--heading5">
            Email: {{ $professor->email ?? 'n/a' }}
        </h5>
    </div>
    <hr>
    <div class="mdc-layout-grid__inner">
        <h2 class="mdc-layout-grid__cell mdc-typography--heading2">Semestres</h2>
    </div>
    <div class="mdc-layout-grid__inner">
        <div class="mdc-layout-grid__cell--span-12">
            @foreach ($semesters as $semester)
                @php
                    $semester_sections = $sections->get($semester->code);
                    $semester_loads = $loads->get($semester->code);
                @endphp
                @if ($semester_sections || ($semester_loads && (is_numeric($semester_loads->pivot->investigative_load) || is_numeric($semester_loads->pivot->investigative_load))))
                    <div class="mdc-layout-grid__inner">
                        <div class="mdc-layout-grid__cell">
                            <h4 class="mdc-typography--heading4">{{ $semester->alpha }}</h4>
                        </div>
                    </div>
                    <div class="mdc-layout-grid__inner">
                        <div class="mdc-data-table mdc-layout-grid__cell--span-6 prof-sections-table">
                            <div class="mdc-data-table">
                                <table class="mdc-data-table__table">
                                    <thead class="thead-light">
                                        <tr class="mdc-data-table__header-row">
                                            <th scope="col" class="mdc-data-table__header-cell">Curso</th>
                                            <th scope="col" class="mdc-data-table__header-cell">Sección</th>
                                            <th scope="col" class="mdc-data-table__header-cell">Horario</th>
                                            <th scope="col" class="mdc-data-table__header-cell">Evaluación</th>
                                            <th scope="col" class="mdc-data-table__header-cell"># de Estudiantes</th>
                                            <th scope="col" class="mdc-data-table__header-cell">Créditos</th>
                                        </tr>
                                    </thead>
                                    <tbody class="mdc-data-table__content">
                                        @php $academic_load = $professor->getAcademicLoad($semester); @endphp
                                            @if ($semester_sections)
                                                @foreach ($semester_sections as $section)
                                                    <tr class="mdc-data-table__row">
                                                        <td class="mdc-data-table__cell">
                                                            <a href="/course/{{ $section->course->id }}">{{ $section->course->code }}</a>
                                                        </td>
                                                        <td class="mdc-data-table__cell">{{ $section->code }}</td>
                                                        <td class="mdc-data-table__cell">{{ $section->schedule ?: 'n/a'}}</td>
                                                        <td class="mdc-data-table__cell">{{ $section->eval ?? 'n/a' }}</td>
                                                        <td class="mdc-data-table__cell">{{ $section->student_count ?? 'n/a' }}</td>
                                                        <td class="mdc-data-table__cell">{{ $section->credits ?? 'n/a' }}</td>
                                                    </tr>
                                                @endforeach
                                                <tr class="mdc-data-table__row">
                                                    <th scope="row" class="mdc-data-table__cell" colspan="5">Carga Academica</th>
                                                    <td class="mdc-data-table__cell">{{ $academic_load }}</td>
                                                </tr>
                                            @endif
                                        @if (Auth::check() || $semester_loads && is_numeric($semester_loads->pivot->admin_load))
                                            <tr class="mdc-data-table__row">
                                                <th class="mdc-data-table__cell" scope="row" @auth colspan="4" @else colspan="5" @endauth>Carga Administrativa</th>
                                                @auth
                                                    <td class="mdc-data-table__cell">
                                                        <button class="mdc-icon-button material-icons" data-toggle="modal" data-target="#modal-professor-loads" data-semester-code="{{ $semester->code }}">edit</button>
                                                    </td>
                                                @endauth
                                                <td class="mdc-data-table__cell">{{ $semester_loads ? $semester_loads->pivot->admin_load : 'n/a' }}</td>
                                            </tr>
                                        @endif
                                        @if (Auth::check() || $semester_loads && is_numeric($semester_loads->pivot->investigative_load))
                                            <tr class="mdc-data-table__row">
                                                <th class="mdc-data-table__cell" scope="row" @auth colspan="4" @else colspan="5" @endauth>Carga Investigativa</th>
                                                @auth
                                                    <td class="mdc-data-table__cell">
                                                        <button class="mdc-icon-button material-icons" data-toggle="modal" data-target="#modal-professor-loads" data-semester-code="{{ $semester->code }}">edit</button>
                                                    </td>
                                                @endauth
                                                <td class="mdc-data-table__cell">{{ $semester_loads ? $semester_loads->pivot->investigative_load : 'n/a' }}</td>
                                            </tr>
                                        @endif
                                        @if ($semester_sections || $semester_loads)
                                            <tr class="mdc-data-table__row">
                                                <th class="mdc-data-table__cell" scope="row" colspan="5">Carga Total</th>
                                                <td class="mdc-data-table__cell">{{ $academic_load + ($semester_loads->pivot->admin_load ?? 0) + ($semester_loads->pivot->investigative_load ?? 0) }}</td>
                                            </tr>
                                        @endif

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @if (!$loop->last)
                        <br>
                    @endif
                @endif
            @endforeach
        </div>
    </div>
@endsection

@section('modals')
    @include('modal.professor.edit')
    @include('modal.professor.loads')
    {{-- @include('modal.semesterload') --}}
@endsection

@section('scripts')
    <script type="text/javascript" src="/js/prof.js"></script>
@endsection
