<?php

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

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

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

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

    public function mount() {
        $this->dts = $this->srvs->getClasse($this->class, $this->school, $this->cutting);
    }

    public function getVals($val) {
        return $val < 10 ? '0'.$val:$val;
    }

    public function getNbre($tls1, $tls2, $tls3) {
        $tls = $tls1 + $tls2 + $tls3;
        return $this->getVals($tls);
    }

    public function getPrtge($tls1, $tls2, $tls3) {
        $tls = null;
        if($tls1) {
            $tls = round(($tls1 / ($tls1 + $tls2 + $tls3)) * 100, 2);
        }
        return $tls ? $tls.'%':'00';
    }
};
?>

<div>
    <div class="table-responsive">
        <table class="table table-bordered" style="width: 100%">
            <thead>
                <tr>
                    <th class="text-center">N°</th>
                    <th class="text-center">Classe</th>
                    <th class="text-center">Effectif</th>
                    <th class="text-center">Classé</th>
                    <th class="text-center">Non classé</th>
                    <th class="text-center">Moy ≥ 10</th>
                    <th class="text-center">8.5 ≤ Moy ≤ 9.99</th>
                    <th class="text-center">Moy < 8.5</th>
                    <th class="text-center">Moy de classe</th>
                </tr>
            </thead>
            <tbody>
                @php $i = 1; @endphp
                @forelse ($dts as $item)
                @php $dts = $this->getResultat($item->id); @endphp
                <tr>
                    <td class="text-center">{{ $i < 10 ? '0'.$i++:$i++ }}</td>
                    <td class="text-center">{{ $item->libelle }}</td>
                    <td class="text-center">{{ $this->getVals($item->inscrits) }}</td>
                    <td class="text-center">
                        {{ $dts ? $this->getNbre($dts->nbre_superieur, $dts->nbre_entre, $dts->nbre_inferieur):'Underdefine' }}
                    </td>
                    <td class="text-center">{{ $dts ? $this->getVals($dts->nbre_non_classe):'Underdefine' }}</td>
                    <td class="text-center">
                        {{ $dts ? $this->getVals($dts->nbre_superieur).' soit '.$this->getPrtge($dts->nbre_superieur, $dts->nbre_entre, $dts->nbre_inferieur):'Underdefine' }}
                    </td>
                    <td class="text-center">
                        {{ $dts ? $this->getVals($dts->nbre_entre).' soit '.$this->getPrtge($dts->nbre_entre, $dts->nbre_superieur, $dts->nbre_inferieur):'Underdefine' }}
                    </td>
                    <td class="text-center">
                        {{ $dts ? $this->getVals($dts->nbre_inferieur).' soit '.$this->getPrtge($dts->nbre_inferieur, $dts->nbre_superieur, $dts->nbre_entre):'Underdefine' }}
                    </td>
                    <td class="text-center">{{ $dts ? $dts->moyenne:'Underdefine' }}</td>
                </tr>
                @empty
                <tr>
                    <td colspan="9" class="text-center">Underdefined data</td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>