<?php

use App\Services\Chapter2Service;
use Livewire\Component;

new class extends Component
{
    public $data;
    public $years;
    public $total = [];
    public $school;
    public $cutting;
    protected $srvs;

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


    public function mount() {
        $this->data = $this->srvs->getCycle($this->school, 4);
        $this->years = $this->srvs->getYears($this->school, $this->cutting, 14, 16);
        $this->total = $this->srvs->getCmptYearTle_II($this->school, $this->cutting);
    }

    public function data($str, $year) {
        $datas = $this->srvs->cpteYearLevel($str, $year, $this->school, $this->cutting, 14, 16);
        return $datas;
    }

    public function total1($year) {
        $datas = $this->srvs->cpteYearTle($year, $this->school, $this->cutting, 14, 16);
        return $datas;
    }
};
?>

<div>
    <div class="card-block table-border-style">
        <div class="table-responsive">
            <table class="table table-bordered" style="width: 100%">
                <thead>
                    <tr>
                        <th class="text-center">Année</th>
                        <th class="text-center">Genre</th>
                        <th class="text-center">2nd</th>
                        <th class="text-center">1ere</th>
                        <th class="text-center">Tle</th>
                        <th class="text-center">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @if (count($years))
                    @foreach ($years as $year)
                    @php $tles = $this->total1($year) @endphp
                    <tr>
                        <th class="text-center pt-4">{{ $year }}</th>
                        <td class="p-0">
                            <table style="width: 100%">
                                <tr>
                                    <td class="text-center py-1">M</td>
                                </tr>
                                <tr>
                                    <td class="text-center py-1">F</td>
                                </tr>
                                <tr>
                                    <td class="text-center py-1">T</td>
                                </tr>
                            </table>
                        </td>
                        @foreach ($data as $item)
                        @php $dts = $this->data($item->id, $year) @endphp
                        <td class="p-0">
                            <table style="width: 100%">
                                <tr>
                                    <td class="text-center py-1">{{ $dts['sexeM'] < 10 ? '0'.$dts['sexeM']:$dts['sexeM'] }}</td>
                                </tr>
                                <tr>
                                    <td class="text-center py-1">{{ $dts['sexeF'] < 10 ? '0'.$dts['sexeF']:$dts['sexeF'] }}</td>
                                </tr>
                                <tr>
                                    <th class="text-center py-1">{{ $dts['total'] < 10 ? '0'.$dts['total']:$dts['total'] }}</th>
                                </tr>
                            </table>
                        </td>
                        @endforeach
                        <td class="p-0">
                            <table style="width: 100%">
                                <tr>
                                    <td class="text-center py-1">{{ $tles['sexeM'] < 10 ? '0'.$tles['sexeM']:$tles['sexeM'] }}</td>
                                </tr>
                                <tr>
                                    <td class="text-center py-1">{{ $tles['sexeF'] < 10 ? '0'.$tles['sexeF']:$tles['sexeF'] }}</td>
                                </tr>
                                <tr>
                                    <th class="text-center py-1">{{ $tles['total'] < 10 ? '0'.$tles['total']:$tles['total'] }}</th>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    @endforeach
                    <tr>
                        <th colspan="2" class="text-center py-1">Total</th>
                        <th class="text-center py-1">{{ $total['2nd'] < 10 ? '0'.$total['2nd']:$total['2nd'] }}</th>
                        <th class="text-center py-1">{{ $total['1ere'] < 10 ? '0'.$total['1ere']:$total['1ere'] }}</th>
                        <th class="text-center py-1">{{ $total['tle'] < 10 ? '0'.$total['tle']:$total['tle'] }}</th>
                        <th class="text-center py-1">{{ $total['total'] < 10 ? '0'.$total['total']:$total['total'] }}</th>
                    </tr>
                    @else
                        <tr>
                            <td colspan="8" class="text-center py-1">Data Underfined</td>
                        </tr>
                    @endif
                </tbody>
            </table>
        </div>
    </div>
</div>