@extends('Vzdelavanie::layout.integrated')

@section('content')
    @parent
    <section class="bg-white space-sm">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col">
                    <h1 class="h2 mb-2">História testov</h1>
                @hasrole('spravca_pek')
                    <span>Zoznam spustených testov na mojom pracovisku</span>
                @else
                    <span>Zoznam mojich spustených testov</span>
                @endhasrole
                </div>
            </div>
        </div>
    </section>

    <section>
        <div class="container">
            @hasrole('spravca_pek')
            <div class="row">
                <div class="col-12">
                    <div class="alert alert-primary text-small">
                        <i class="icon-info mr-2"></i>
                        <span>Správca pracoviska má prístup k histórii všetkých testov spustených študentami z jeho pracoviska.</span>
                    </div>
                </div>
            </div>
            @endhasrole
            <div class="row">
                <div class="col">
                @if($testy->isEmpty())
                    <div class="alert alert-primary text-small">
                        <i class="icon-info mr-2"></i>
                        Zatiaľ neexistujú žiadne testy...
                    </div>
                @else
                    <div class="card card-sm shadow">
                        <div class="card-header bg-secondary d-flex justify-content-between align-items-center">
                            <div>
                                <h6>Úspešnosť riešenia testov graficky</h6>
                            </div>
                            @hasrole('spravca_pek')
                            <form class="d-flex align-items-center">
                                <span class="mr-2 text-muted text-small text-nowrap">Používateľ:</span>
                                <select class="custom-select mr-2" id="chartUser" autocomplete="off">
                                @foreach($zoznamPouzivatelov as $pouzivatel)
                                    <option value="{{ $pouzivatel->hashId() }}" @if($pouzivatel->id == auth()->id()) selected="selected" @endif>{{ $pouzivatel->meno }}</option>
                                @endforeach
                                </select>
                                <span class="mx-2 text-muted text-small text-nowrap">Obdobie:</span>
                                <select class="custom-select" id="chartPeriod" autocomplete="off">
                                    <option value="day">dnes</option>
                                    <option value="week" selected="selected">posledný týždeň</option>
                                    <option value="month">posledný mesiac</option>
                                </select>
                            </form>
                            @endhasrole
                        </div>
                        <div id="chart" class="mt-1" style="height: 300px;"></div>
                    </div>

                    <div class="card card-sm shadow">
                        <div class="card-header bg-secondary d-flex justify-content-between align-items-center">
                            <div>
                                <span class="text-muted text-small">Počet položiek {{ ($testy->currentPage() * $testy->perPage()) - $testy->perPage() + 1 }} - @if($testy->total() > ($testy->currentPage() * $testy->perPage())) {{ $testy->currentPage() * $testy->perPage() }} @else {{ $testy->total() }} @endif z {{ $testy->total() }}</span>
                            </div>
                        </div>
                        <div class="table-responsive">
                            <table class="table align-items-center mb-0">
                                <thead>
                                    <tr>
                                        <th scope="col">#</th>
                                        @hasrole('spravca_pek')
                                        <th scope="col">Používateľ</th>
                                        @endhasrole
                                        <th scope="col">Názov testu</th>
                                        <th scope="col" class="text-center">Úspešnosť</th>
                                        <th scope="col" class="text-center">Spustené</th>
                                        <th scope="col" class="text-center">Vyhodnotené</th>
                                        <th scope="col" class="text-center">Akcia</th>
                                    </tr>
                                </thead>
                                <tbody>
                                @foreach($testy as $test)
                                    <tr class="text-small">
                                        <td>{{ ($testy->currentPage() * $testy->perPage()) - $testy->perPage() + $loop->index + 1 }}.</td>
                                        @hasrole('spravca_pek')
                                        <td class="w-15">
                                            <h5 class="h6">{{ $test->pouzivatel->meno }} <span class="badge badge-light">{{ $test->pouzivatel->login }}</span></h5>
                                        </td>
                                        @endhasrole
                                        <td class="w-auto">
                                            <div class="media align-items-center">
                                                <div class="media-body">
                                                    <span class="h5 mb-0">{{ $test->nazov }}</span>
                                                    <span class="text-muted">{{ \Illuminate\Support\Str::limit($test->popis, 60) }}</span>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="progress progress-sm">
                                                <div class="progress-bar bg-success" role="progressbar" style="width: {{ $test->spravneZodpovedaneOtazkyPercentualne }}%" aria-valuenow="{{ $test->spravneZodpovedaneOtazkyPercentualne }}" aria-valuemin="0" aria-valuemax="100" data-toggle="tooltip" data-placement="top" title="Podiel správne zodpovedaných otázok"></div>
                                                <div class="progress-bar bg-danger" role="progressbar" style="width: {{ $test->nespravneZodpovedaneOtazkyPercentualne }}%" aria-valuenow="{{ $test->nespravneZodpovedaneOtazkyPercentualne }}" aria-valuemin="0" aria-valuemax="100" data-toggle="tooltip" data-placement="top" title="Podiel nesprávne zodpovedaných otázok"></div>
                                            </div>
                                        </td>
                                        <td class="text-center">{{ $test->casova_znacka_spustenia ? $test->casova_znacka_spustenia->longRelativeDiffForHumans() : '-' }}</td>
                                        <td class="text-center">{{ $test->casova_znacka_vyhodnotenia ? $test->casova_znacka_vyhodnotenia->longRelativeDiffForHumans() : '-' }}</td>
                                        <td class="w-auto">
                                        @if($test->jeVyhodnoteny())
                                            <a class="btn btn-md btn-success btn-block" href="{{ route('vzdelavanie.testy.pouzivatela.test.vyhodnotenie', $test) }}">
                                                Zobraziť
                                                <i class="icon-right-open"></i>
                                            </a>
                                        @else
                                            <a class="btn btn-md btn-primary btn-block" href="{{ route('vzdelavanie.testy.pouzivatela.test.spustenie', $test) }}">
                                                <i class="icon-play"></i>
                                                Dokončiť
                                            </a>
                                        @endif
                                        </td>
                                    </tr>
                                    @if($loop->remaining)
                                        <tr class="table-divider"></tr>
                                    @endif
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                @endif
                </div>
            </div>
            @if($testy->count() && $testy->hasPages())
            <div class="row">
                <div class="col-12 d-flex justify-content-center">
                    {{ $testy->onEachSide(5)->links() }}
                </div>
            </div>
            @endif
        </div>
    </section>
@endsection

@if($testy->isNotEmpty())
    @push('scripts')
        <script type="text/javascript">
          $(function () {
            $('[data-toggle="tooltip"]').tooltip()
          })
        </script>

        <script src="{{ mix('/js/chartisan/echarts.min.js', '/vendor/seka/vzdelavanie') }}"></script>
        <script src="{{ mix('/js/chartisan/chartisan_echarts.js', '/vendor/seka/vzdelavanie') }}"></script>

        <script type="application/javascript">
          let urlParams = {
            pouzivatelId: '{!! \Elceka\Laravel\HashId\Hasher::encode(auth()->id()) !!}',
            obdobie: 'week'
          }

          const url = function () {
            return "@chart('vzdelavanie_testy_uspesnost_riesenia_na_pek')" + '?pouzivatel_id=' + urlParams.pouzivatelId + '&obdobie=' + urlParams.obdobie
          }

          const chart = new Chartisan({
            el: '#chart',
            url: "@chart('vzdelavanie_testy_uspesnost_riesenia_na_pek')",
            loader: {
              color: '#333',
              size: [30, 30],
              type: 'bar',
              textColor: '#333',
              text: 'Získavajú sa údaje...',
            },
            type: 'line',
            hooks: new ChartisanHooks()
              .datasets(['bar', 'bar', {type: 'line', fill: false}])
              .colors([
                '#02b875',
                '#d9534f',
                '#4582ec'
              ])
              .legend({
                position: 'bottom',
              })
              .tooltip(true)
          })

          const selectUser = document.getElementById('chartUser');
          selectUser.addEventListener('change', function (event) {
            urlParams.pouzivatelId = event.target.value
            chart.update({
              url: url()
            })
          })

          const selectPeriod = document.getElementById('chartPeriod');
          selectPeriod.addEventListener('change', function (event) {
            urlParams.obdobie = event.target.value
            chart.update({
              url: url()
            })
          })
        </script>
    @endpush
@endif