@extends('layouts.app')

@section('title', 'Cursos')

@section('content')
    <div class="mdc-layout-grid__inner">
        <div class="mdc-layout-grid__cell--span-2">
            <h1 class="mdc-typography--heading1">Cursos</h1>
        </div>
        <div class="mdc-layout-grid__cell--span-2 mdc-layout-grid__cell--align-middle">
            <button type="button" class="mdc-button mdc-button--outlined" data-toggle="modal" data-target="#modal-course-create">
                <span class="mdc-button__label mdc-typography--button">Añadir Curso</span>
            </button>
        </div>
    </div>
    @if ($errors->any())
        <div class="mdc-layout-grid__inner">
            <div class="alert alert-danger alert-dismissible fade show mdc-layout-grid__cell--span-12" role="alert">
                <button type="button" class="close" data-dismiss="modal" aria-label="close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <ul class="mdc-list">
                    @foreach ($errors->all() as $error)
                        <li class="mcd-list-item"><span class="mdc-list-item__text">{{ $error }}</span></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-row-head table-fixed-col-head table--fit-screen">
            <table id="course-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">Curso</th>
                        <th class="mdc-data-table__header-cell mdc-elevation--z2" scope="col">Title</th>
                        <th class="mdc-data-table__header-cell mdc-elevation--z2" scope="col">Prontuario</th>
                        @foreach ($semesters as $semester)
                            <th class="mdc-data-table__header-cell mdc-elevation--z2" scope="col"># de Secciones<br>{{ $semester->alpha }}</th>
                        @endforeach
                    </tr>
                </thead>
                <tbody class="mdc-data-table__content">
                @foreach ($courses as $course)
                    <tr class="mdc-data-table__row">
                        <th scope="row" class="mdc-data-table__cell mdc-elevation--z1">
                            <a href="{{ route('course.show', ['id' => $course->id]) }}">{{ $course->code }}</a>
                        </th>
                        <td class="mdc-data-table__cell" data-toggle="tooltip" title="{{ $course->title }}">{{ substr($course->title, 0, 50) . (strlen($course->title) > 50 ? '...' : '') }}</td>
                        <td class="mdc-data-table__cell">
                            @if (!is_null($course->syllabus))
                                <a href="{{ $course->syllabus }}">PDF</a>
                            @else
                                @auth
                                    <button type="button" class="mdc-button mdc-button--dense mdc-button--unelevated" data-toggle="modal" data-target="#modal-syllabus" data-course-id="{{ $course->id }}">
                                        <span class="mdc-button__label">Subir</span>
                                    </button>
                                @else
                                    n/a
                                @endauth
                            @endif
                        </td>
                        @foreach ($semesters as $semester)
                        <td class="mdc-data-table__cell mdc-data-table__cell--numeric"
                        @if (Auth::check() && $semesters->take(-6)->contains($semester->code))
                            onclick="addSection({{ $course->id . ',\'' . $semester->code }}')"
                        @endif >
                            {{ $course->getSemesterSectionCount($semester->code) }}
                        </td>
                        @endforeach
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection

@section('modals')
    @include('modal.course.create')
    @include('modal.section.create')
    @include('modal.syllabus')
@endsection

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