@extends('layouts.app')

@section('title', 'Profesores')

{{-- {{dd($professors)}} --}}
@section('content')
    <div class="mdc-layout-grid__inner">
        <div class="mdc-layout-grid__cell--span-2">
            <h1>Profesores</h1>
        </div>
        <div class="mdc-layout-grid__cell--span-2 mdc-layout-grid__cell--align-middle">
            @auth
                <button type="button" class="mdc-button mdc-button--outlined" data-toggle="modal" data-target="#modal-professor-create">
                    <span class="mdc-button__label">Añadir Profesor</span>
                </button>
            @endauth
        </div>
    </div>
    @if ($errors->any())
        <div class="row">
            <div class="alert alert-danger alert-dismissible fade show col-6" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
              <ul>
                  @foreach ($errors->all() as $error)
                      <li>{{ $error }}</li>
                  @endforeach
              </ul>
            </div>
        </div>
    @endif

    <div class="mdc-layout-grid__inner">
        <div class="mdc-data-table mdc-layout-grid__cell--span-12 table-fixed-col-head table-fixed-row-head table--fit-screen">
            <table id="prof-table" class="mdc-data-table__table">
                <thead>
                    <tr class="mdc-data-table__header-row">
                        <th class="mdc-data-table__header-cell mdc-elevation--z3" scope="col">Profesor</th>
                        @foreach ($semesters as $semester)
                            <th class="mdc-data-table__header-cell mdc-elevation--z2" scope="col">Carga Total<br>{{ $semester->alpha }}</th>
                        @endforeach
                    </tr>
                </thead>
                <tbody class="mdc-data-table__content">
                    @foreach ($professors as $professor)
                        <tr class="mdc-data-table__row">
                            <th scope="row" class="mdc-data-table__cell mdc-elevation--z1">
                                <a href="/professor/{{ $professor->id }}">{{ $professor->last_name . ', ' . $professor->first_name }}</a>
                            </th>
                            @foreach ($semesters as $semester)
                                <td class="mdc-data-table__cell mdc-data-table__cell--numeric">
                                    @php
                                        $total_load = 0;

                                        $prof_sem = $professor->semesters->find($semester->code);
                                        $prof_sections = $professor->sections->where('semester_code', '=', $semester->code);

                                        if ($prof_sem) {
                                            $total_load += ($prof_sem->pivot->admin_load ?? 0) + ($prof_sem->pivot->investigative_load ?? 0);
                                        }
                                        $total_load += $professor->getAcademicLoad($semester);
                                    @endphp
                                    {{ $total_load }}
                                </td>
                            @endforeach
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection

@section('modals')
    @include('modal.professor.create')
@endsection
