<?php

use App\Services\Chapter1Service;
use Livewire\Component;

new class extends Component
{
    public $levels;
    public $school;
    public $cutting;
    protected $srvs;

    public function boot(Chapter1Service $srvs) {
        $this->srvs = $srvs;
    }

    public function getResultat($id) {
        return $this->srvs->getStudents($id, $this->school, $this->cutting);
    }

    public function getMoyen($id) {
        $dts = $this->srvs->getMoyenne($id, $this->cutting);
        return [
            'moyen' => $dts ? $dts->moyenne:'--',
            'rang' => $dts ? $dts->rang:'--',
            'ob' => $dts ? ($dts->moyenne != 'nc' ? appreciationGeneral($dts->moyenne):'--'):'--'
        ];
    }

    public function mount() {
        $this->levels = $this->srvs->getLevel($this->school, 3);
    }
};
?>

<div>
    @php $j = 1 @endphp
    @foreach ($levels as $level)
        @php $dts = $this->getResultat($level->id); @endphp
        <div style="font-size: 13px; text-decoration: none; margin-left: 110px;">
            <span><u>Niveau</u> : <strong>{{ $level->code }}</strong></span>
        </div>
        <div style="margin-bottom: 25px; margin-top: 25px;">
            <table class="table table-bordered" id="myTable_I_{{ $j++ }}" style="width: 100%">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 3%; padding: 8px">N°</th>
                        <th class="text-center" style="width: 8%; padding: 8px">Matricule</th>
                        <th class="text-center" style="width: 8%; padding: 8px">Nom</th>
                        <th class="text-center" style="width: 20%; padding: 8px">Prénoms</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Sexe</th>
                        <th class="text-center" style="width: 10%; padding: 8px">Date Nais</th>
                        <th class="text-center" style="width: 10%; padding: 8px">Lieu Nais</th>
                        <th class="text-center" style="width: 9%; padding: 8px">Nat.</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Red.</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Affecté</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Moy.</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Rang</th>
                        <th class="text-center" style="width: 5%; padding: 8px">Ob.</th>
                    </tr>
                </thead>
                <tbody>
                    @php $i = 1; @endphp
                    @forelse ($dts as $item)
                    @php $moyen = $this->getMoyen($item->id); @endphp
                    <tr>
                        <td class="text-center" style="padding: 8px">{{ $i < 10 ? '0'.$i++:$i++ }}</td>
                        <td class="text-left" style="padding: 8px">{{ $item->student->matricule }}</td>
                        <td class="text-left" style="padding: 8px">{{ strtoupper($item->student->first_name) }}</td>
                        <td class="text-left" style="padding: 8px">{{ ucwords($item->student->last_name) }}</td>
                        <td class="text-center" style="padding: 8px">{{ ucwords($item->student->sexe) }}</td>
                        <td class="text-center" style="padding: 8px">{{ date('d/m/Y', strtotime($item->student->date_birth)) }}</td>
                        <td class="text-left" style="padding: 8px">{{ ucwords($item->student->place_birth) }}</td>
                        <td class="text-left" style="padding: 8px">{{ ucwords($item->student->nationalitie->libelle) }}</td>
                        <td class="text-center" style="padding: 8px">{{ ucwords($item->redoublant) }}</td>
                        <td class="text-center" style="padding: 8px">{{ ucwords($item->affecte) }}</td>
                        <td class="text-center" style="padding: 8px">{{ $moyen['moyen'] }}</td>
                        <td class="text-center" style="padding: 8px">{{ $moyen['rang'] }}</td>
                        <td class="text-left" style="padding: 8px">{{ ucwords($moyen['ob']) }}</td>
                    </tr>
                    @empty
                        <tr>
                            <td colspan="13" class="text-center" style="padding: 8px">Under defined</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    @endforeach
</div>