<?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, 4);
    }
};
?>

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