@php
    header('cache-Control: no-store, no-cache, must-revalidate');
    header('cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
@endphp

<!DOCTYPE html>
<html id="html" lang="{{ app()->getLocale() }}">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"
        integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="/css/connexion.css">
    <link rel="stylesheet" href="/css/style.css">
    <title>UpSchool</title>
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
        crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/cleave.js@1.6.0/dist/cleave.min.js"
        integrity="sha256-frGUwmSN4CLLjyk5m59ECdXsDMUxTW5O6hdceNHVCJo=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/cleave.js@1.6.0/dist/addons/cleave-phone.sn.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/additional-methods.min.js"
        integrity="sha512-XZEy8UQ9rngkxQVugAdOuBRDmJ5N4vCuNXCh8KlniZgDKTvf7zl75QBtaVG1lEhMFe2a2DuA22nZYY+qsI2/xA=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
    <link rel="icon" href="/img/upschool-favicon.png">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        h1 {
            font-family: 'New Century Schoolbook', 'TeX Gyre Schola', 'serif';
        }

        @media print {
            @page {
                margin-top: 0;
                margin-bottom: 0;
            }
        }

        * {
            -webkit-print-color-adjust: exact !important;
            /* Chrome, Safari, Edge */
            color-adjust: exact !important;
            /*Firefox*/
        }
    </style>
</head>

<body style="background-image: url('/img/bgbulletin.png'); background-size: contain;"
    class="has-background-white px-5 my-0 is-family-monospace" id="print">
    {{-- <button id="btn_print" onclick="printDiv('print')">Print</button> --}}
    @php
        $rang = 1;
        $rangEleve = 1;
        $laMoyennePrec = 0;
    @endphp
    @foreach ($bulletins as $bulletin)
        <section style="page-break-after:always;">
            <section class="columns mx-0 mt-2 is-flex is-flex-wrap-nowrap">
                <div class="column pb-0">
                    <h1 style="font-family: New Century Schoolbook, TeX Gyre Schola, serif"
                        class="title is-3 has-text-weight-bold">{{ env('SCHOOL_NAME') }}</h1>
                </div>
                <div class="column is-flex is-justify-content-flex-end pb-0">
                    <h5 class="title is-7 has-text-weight-normal pt-5">ANNEE SCOLAIRE {{ env('SCHOOL_YEAR') }}</h3>
                </div>
            </section>
            <section class="columns mx-0">
                <div class="column pt-0">
                    <p class="is-size-7">{{ env('SCHOOL_ADDRESS') }}</p>
                    <p class="is-size-7">{{ env('SCHOOL_AUT') }}</p>
                    <p class="is-size-7">Tel: {{ env('SCHOOL_PHONE') }}</p>
                </div>
            </section>
            <section class="is-flex is-justify-content-center mb-2">
                @switch($periode)
                    @case('trimestre1')
                        <h1 class="title is-4">BULLETIN DE NOTES DU 1<sup>er</sup> TRIMESTRE</h1>
                    @break

                    @case('trimestre2')
                        <h1 class="title is-4">BULLETIN DE NOTES DU 2<sup>e</sup> TRIMESTRE</h1>
                    @break

                    @case('trimestre3')
                        <h1 class="title is-4">BULLETIN DE NOTES DU 3<sup>e</sup> TRIMESTRE</h1>
                    @break

                    @case('semestre1')
                        <h1 class="title is-4">BULLETIN DE NOTES DU 1<sup>er</sup> SEMESTRE</h1>
                    @break

                    @default
                        <h1 class="title is-4">BULLETIN DE NOTES DU 2<sup>e</sup> SEMESTRE</h1>
                @endswitch
            </section>
            <section class="columns mx-0 is-flex is-flex-wrap-nowrap">
                <div class="column is-1">
                </div>
                <div class="column pb-0 has-text-weight-bold">
                    <span class="has-text-weight-bold">Prénom : </span>{{ $bulletin['eleve']->prenom }}
                </div>
                <div class="column is-flex is-justify-content-flex-end pb-0 has-text-weight-bold">
                    <span class="has-text-weight-bold">Nom : </span>{{ $bulletin['eleve']->nom }}
                </div>
                <div class="column is-1">
                </div>
            </section>
            <section class="columns mx-0 is-flex is-flex-wrap-nowrap">
                <div class="column is-1">
                </div>
                <div class="column pt-0 pb-0">
                    <span class="has-text-weight-bold">Date et lieu de naissance :
                    </span>{{ date('d/m/Y', strtotime($bulletin['eleve']->date_naissance)) }} à
                    {{ $bulletin['eleve']->lieu_naissance }}
                </div>

            </section>
            <section class="columns mx-0 is-flex is-flex-wrap-nowrap">
                <div class="column is-1">
                </div>
                <div class="column pt-0 pb-0">
                    <span class="has-text-weight-bold">Classe : </span>{{ $classe->libelle }}
                </div>
                <div class="column is-flex is-justify-content-flex-end pt-0">
                    <span class="has-text-weight-bold">Nbre d'élèves : </span>{{ $classe->effectif }}
                </div>
                <div class="column is-1">
                </div>
            </section>
            <table class="table is-bordered is-fullwidth has-text-centered is-size-7">
                <thead>
                    <tr>
                        <th>Matières</th>
                        @if ($classe->type == 'Primaire')
                            <th>Notes CC</th>
                            <th>Compo.</th>
                            <th>Moyennes</th>
                            <th>Sur</th>
                            <th>Moy. * Bar.</th>
                        @else
                            <th>Notes Dev. /20</th>
                            <th>Exam. /20</th>
                            <th>Moyennes /20</th>
                            <th>Coef.</th>
                            <th>Moy. * Coef.</th>
                        @endif
                        <th>Rangs</th>
                        <th>Appréciations</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($bulletin['resultats'] as $resultat)
                        <tr>
                            <th>{{ $resultat['matiere']->libelle }}</th>
                            <td>{{ $resultat['noteDevoir'] }}</td>
                            <td>{{ $resultat['noteExam'] }}</td>
                            <td>{{ $resultat['moyenne'] }}</td>
                            <td>{{ $resultat['coef'] }}</td>
                            <td>{{ $resultat['moyenneCoef'] }}</td>
                            @php
                                $arrayRangsMatiere = [];
                                foreach ($rangsMatieresEleves as $rangMatieresEleve) {
                                    foreach ($rangMatieresEleve['rangsMatieres'] as $rangMatiereEleve) {
                                        if ($resultat['matiere'] == $rangMatiereEleve['matiere']) {
                                            $arrayRangsMatiere[] = [
                                                'moy' => $rangMatiereEleve['moyenneCoef'],
                                                'eleve' => $rangMatieresEleve['eleve'],
                                            ];
                                        }
                                    }
                                }
                                arsort($arrayRangsMatiere);
                                $rangMat = 1;
                                $moyPrec = 0;
                                $rangEleveMat = 1;
                            @endphp
                            @foreach ($arrayRangsMatiere as $rangsMatiere)
                                @php
                                    if ($moyPrec != $rangsMatiere['moy']) {
                                        $rangEleveMat = $rangMat;
                                    }
                                @endphp
                                @if ($rangsMatiere['eleve']->id == $bulletin['eleve']->id)
                                    <td>{{ $rangEleveMat }}</td>
                                @endif
                                @php
                                    $rangMat++;
                                @endphp
                                @php
                                    $moyPrec = $rangsMatiere['moy'];
                                @endphp
                            @endforeach
                            @if ($classe->type == 'Primaire')
                                @if (($resultat['moyenne'] * 10) / $resultat['coef'] == 10)
                                    <td>Très Bien</td>
                                @elseif (($resultat['moyenne'] * 10) / $resultat['coef'] >= 7 && ($resultat['moyenne'] * 10) / $resultat['coef'] < 10)
                                    <td>Bien</td>
                                @elseif (($resultat['moyenne'] * 10) / $resultat['coef'] >= 6 && ($resultat['moyenne'] * 10) / $resultat['coef'] < 7)
                                    <td>Assez Bien</td>
                                @elseif (($resultat['moyenne'] * 10) / $resultat['coef'] >= 5 && ($resultat['moyenne'] * 10) / $resultat['coef'] < 6)
                                    <td>Passable</td>
                                @elseif (($resultat['moyenne'] * 10) / $resultat['coef'] >= 0 && ($resultat['moyenne'] * 10) / $resultat['coef'] < 5)
                                    <td>Insuffisant</td>
                                @endif
                            @else
                                @if ($resultat['moyenne'] >= 19)
                                    <td>Excellent</td>
                                @elseif ($resultat['moyenne'] >= 16 && $resultat['moyenne'] < 19)
                                    <td>Très Bien</td>
                                @elseif ($resultat['moyenne'] >= 14 && $resultat['moyenne'] < 16)
                                    <td>Bien</td>
                                @elseif ($resultat['moyenne'] >= 12 && $resultat['moyenne'] < 14)
                                    <td>Assez Bien</td>
                                @elseif ($resultat['moyenne'] >= 10 && $resultat['moyenne'] < 12)
                                    <td>Passable</td>
                                @elseif ($resultat['moyenne'] >= 8 && $resultat['moyenne'] < 10)
                                    <td>Médiocre</td>
                                @elseif ($resultat['moyenne'] >= 4 && $resultat['moyenne'] < 8)
                                    <td>Mal</td>
                                @elseif ($resultat['moyenne'] >= 0 && $resultat['moyenne'] < 4)
                                    <td>Très Mal</td>
                                @endif
                            @endif
                        </tr>
                    @endforeach
                </tbody>
                @php
                    $moyenneAnnuelle = 0;
                @endphp
                <tfoot>
                    @if ($periode != 'semestre2' && $periode != 'trimestre3')
                        <tr>
                            <th class="is-vcentered is-size-7" colspan="3" rowspan="2">Total :
                                {{ $bulletin['total'] }}
                            </th>
                            @if ($laMoyennePrec != $bulletin['laMoyenne'])
                                @php
                                    $rangEleve = $rang;
                                @endphp
                            @endif
                            <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Rang :
                                {{ $rangEleve <= 10 ? $rangEleve : '' }}</th>
                            <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Moyenne :
                                {{ $bulletin['laMoyenne'] }}</th>
                            <th>Retards : </th>
                        </tr>
                        <tr>
                            <th>Absences :
                                {{ count($bulletin['eleve']->absences->where('promo', env('SCHOOL_YEAR'))) }}</th>
                        </tr>
                    @else
                        <tr>
                            @if ($periode == 'trimestre3')
                                <th class="is-vcentered is-size-7" colspan="3" rowspan="5">Total :
                                    {{ $bulletin['total'] }}</th>
                            @else
                                <th class="is-vcentered is-size-7" colspan="3" rowspan="4">Total :
                                    {{ $bulletin['total'] }}</th>
                            @endif
                            @switch($periode)
                                @case('semestre2')
                                    @php
                                        $rangEleve1 = 1;
                                        $rang1 = 1;
                                        $moyennePrec1 = 0;
                                    @endphp
                                    @foreach ($bulletins1 as $bulletin1)
                                        @if ($bulletin1['laMoyenne'] != $moyennePrec1)
                                            @php
                                                $rangEleve1 = $rang1;
                                            @endphp
                                        @endif
                                        @if ($bulletin1['eleve']->id == $bulletin['eleve']->id)
                                            <th class="is-vcentered is-size-7" colspan="2">Rang 1<sup>er</sup> Semestre :
                                                {{ $rangEleve1 <= 10 ? $rangEleve1 : '' }}</th>
                                            <th class="is-vcentered is-size-7" colspan="3">Moyenne 1<sup>er</sup> Semestre :
                                                {{ $bulletin1['laMoyenne'] }}</th>
                                            @php
                                                $moyenneAnnuelle = $bulletin['laMoyenne'] + $bulletin1['laMoyenne'];
                                            @endphp
                                        @endif
                                        @php
                                            $moyennePrec1 = $bulletin1['laMoyenne'];
                                            $rang1++;
                                        @endphp
                                    @endforeach
                                @break

                                @case('trimestre3')
                                    @php
                                        $rangEleve1 = 1;
                                        $rang1 = 1;
                                        $moyennePrec1 = 0;
                                    @endphp
                                    @foreach ($bulletins1 as $bulletin1)
                                        @if ($bulletin1['laMoyenne'] != $moyennePrec1)
                                            @php
                                                $rangEleve1 = $rang1;
                                            @endphp
                                        @endif
                                        @if ($bulletin1['eleve']->id == $bulletin['eleve']->id)
                                            <th class="is-vcentered is-size-7" colspan="2">Rang 1<sup>er</sup> Trimestre :
                                                {{ $rangEleve1 <= 10 ? $rangEleve1 : '' }}</th>
                                            <th class="is-vcentered is-size-7" colspan="3">Moyenne 1<sup>er</sup> Trimestre :
                                                {{ $bulletin1['laMoyenne'] }}</th>
                                            @php
                                                $moyenneAnnuelle = $bulletin['laMoyenne'] + $bulletin1['laMoyenne'];
                                            @endphp
                                        @endif
                                        @php
                                            $moyennePrec1 = $bulletin1['laMoyenne'];
                                            $rang1++;
                                        @endphp
                                    @endforeach
                                @break
                            @endswitch
                        </tr>
                        <tr>
                            @switch($periode)
                                @case('semestre2')
                                    @if ($laMoyennePrec != $bulletin['laMoyenne'])
                                        @php
                                            $rangEleve = $rang;
                                        @endphp
                                    @endif
                                    <th class="is-vcentered is-size-7" colspan="2">Rang 2<sup>e</sup> Semestre :
                                        {{ $rangEleve <= 10 ? $rangEleve : '' }}</th>
                                    <th class="is-vcentered is-size-7" colspan="3">Moyenne 2<sup>e</sup> Semestre :
                                        {{ $bulletin['laMoyenne'] }}</th>
                                @break

                                @case('trimestre3')
                                    @php
                                        $rangEleve2 = 1;
                                        $rang2 = 1;
                                        $moyennePrec2 = 0;
                                    @endphp
                                    @foreach ($bulletins2 as $bulletin2)
                                        @if ($bulletin2['laMoyenne'] != $moyennePrec2)
                                            @php
                                                $rangEleve2 = $rang2;
                                            @endphp
                                        @endif
                                        @if ($bulletin2['eleve']->id == $bulletin['eleve']->id)
                                            <th class="is-vcentered is-size-7" colspan="2">Rang 2<sup>e</sup> Trimestre :
                                                {{ $rangEleve2 <= 10 ? $rangEleve2 : '' }}</th>
                                            <th class="is-vcentered is-size-7" colspan="3">Moyenne 2<sup>e</sup> Trimestre :
                                                {{ $bulletin2['laMoyenne'] }}</th>
                                            @php
                                                $moyenneAnnuelle = $moyenneAnnuelle + $bulletin2['laMoyenne'];
                                            @endphp
                                        @endif
                                        @php
                                            $moyennePrec2 = $bulletin2['laMoyenne'];
                                            $rang2++;
                                        @endphp
                                    @endforeach
                                @break
                            @endswitch
                        </tr>
                        @if ($periode == 'trimestre3')
                            <tr>
                                @if ($laMoyennePrec != $bulletin['laMoyenne'])
                                    @php
                                        $rangEleve = $rang;
                                    @endphp
                                @endif
                                <th class="is-vcentered is-size-7" colspan="2">Rang 3<sup>e</sup> Trimestre :
                                    {{ $rangEleve <= 10 ? $rangEleve : '' }}</th>
                                <th class="is-vcentered is-size-7" colspan="3">Moyenne 3<sup>e</sup> Trimestre :
                                    {{ $bulletin['laMoyenne'] }}</th>
                            </tr>
                        @endif
                        <tr>
                            @php
                                $rangAn = 1;
                                $rangAnEl = 1;
                                $moyAnPrec = 0.0;
                            @endphp
                            @foreach ($moyennesAnnuelles as $moyenneAnnuelleEl)
                                @if ($moyenneAnnuelleEl['eleve']->id == $bulletin['eleve']->id)
                                    @if ($moyenneAnnuelleEl['moyenneAnnuelle'] != $moyAnPrec)
                                        @php
                                            $rangAnEl = $rangAn;
                                        @endphp
                                    @endif
                                    <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Rang annuel :
                                        {{ $rangAnEl <= 10 ? $rangAnEl : '' }}</th>
                                    @php
                                        $moyAnPrec = $moyenneAnnuelleEl['moyenneAnnuelle'];
                                    @endphp
                                @endif
                                @php
                                    $rangAn++;
                                @endphp
                            @endforeach

                            @if ($classe->type == 'Primaire')
                                <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Moyenne annuelle :
                                    {{ round($moyenneAnnuelle / 3, 2) }}</th>
                            @else
                                @if ($periode == 'trimestre3')
                                    <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Moyenne annuelle
                                        :
                                        {{ round($moyenneAnnuelle / 3, 2) }}</th>
                                @elseif ($periode == 'semestre2')
                                    <th class="is-vcentered is-size-7" colspan="2" rowspan="2">Moyenne annuelle
                                        :
                                        {{ round($moyenneAnnuelle / 2, 2) }}</th>
                                @endif
                            @endif

                            <th>Retards : </th>
                        </tr>
                        <tr>
                            <th>Absences :
                                {{ count($bulletin['eleve']->absences->where('promo', env('SCHOOL_YEAR'))) }}</th>
                        </tr>
                    @endif
                    <tr>
                        <td>Félicitations</td>
                        <td colspan="2">Encouragement</td>
                        <td colspan="2">Tableau d'honneur</td>
                        <td>Avertissement</td>
                        <td colspan="2">Blame</td>
                    </tr>
                </tfoot>
            </table>
            <section class="">
                <div class="">
                    @if ($periode == 'semestre2' || $periode == 'trimestre3')
                        <table class="table is-bordered is-fullwidth has-text-centered is-size-7">
                            <thead>
                                <th>Passe en classe supérieure</th>
                                <th>Autorisé(e) à redoubler</th>
                                <th>Exclus(e) définitivement</th>
                            </thead>
                        </table>
                    @else
                        <table class="table is-bordered is-fullwidth has-text-centered is-size-7">
                            <thead>
                                <th>TB</th>
                                <th>B</th>
                                <th>AB</th>
                                <th>PAS</th>
                                <th>MED</th>
                                <th>MAL</th>
                                <th>TR MAL</th>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>Excellent</td>
                                    <td>Doit persévérer dans son effort</td>
                                    <td>Doit redoubler d'effort</td>
                                    <td>Progrès insuffisants</td>
                                    <td>Doit se mettre au travail</td>
                                    <td>Risque de redoubler</td>
                                    <td>Risque de ne pas être conservé</td>
                                </tr>
                            </tbody>
                        </table>
                    @endif
                </div>
                <div class="columns mx-0">
                    <div class="ml-5 is-italic column mt-4">Ce bulletin n'est délivré qu'une seule fois</div>
                    <div class="is-flex is-justify-content-flex-end mt-4 column">
                        <span class="title is-5 is-underlined">Le chef de l'établissement</span>
                    </div>
                </div>
            </section>

        </section>
        @php
            $rang += 1;
            $laMoyennePrec = $bulletin['laMoyenne'];
        @endphp
    @endforeach
    {{-- <script>
        function printDiv(divName) {
            var printContents = document.getElementById(divName).innerHTML;
            var originalContents = document.body.innerHTML;

            document.body.innerHTML = printContents;

            window.print();

            document.body.innerHTML = originalContents;
        }
    </script> --}}
</body>

</html>
