@extends('layout.template')
@section('intitule')
    <h5 class="font-weight-medium text-uppercase mb-0">INSERTION DES NOTES PAR ELEMENT CONSTITUTIF : SESSION DE REPRISE</h5>
@endsection

@section('breadcrumb')
    <li class="breadcrumb-item active" aria-current="page">Insertion notes</li>
@endsection

@section('styles')
    <link href="{{ asset('assets/extra-libs/toastr/dist/build/toastr.min.css') }}" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="{{ asset('assets/libs/select2/dist/css/select2.min.css') }}">
@endsection

@section('main')
    {{-- @if (Illuminate\Support\Facades\Session::has('noData'))
        <div class="p-5" style="height: 100% background-color:none">

            <div class="card" style="border: solid #cfcfcf 0.07em; box-shadow: -2px 2px 7px #cfcfcf">
                <div class="card-header bg-danger d-flex align-items-center" style="border-bottom: solid #cfcfcf 0.02em;">
                    <h4 class="card-title text-dark">GestStudent</h4>
                    <div class="card-actions ms-auto d-flex button-group">
                        <a class="mb-0 link d-flex align-items-center pe-0" style="color: #cfcfcf;" data-action="">
                            <i data-feather="x" class="feather-sm"></i>
                        </a>
                    </div>
                </div>
                <div class="card-body collapse show text-center">
                    <p class="card-text text-danger">
                        Aucun étudiant n'est inscrit dans la filière :
                        <span style="color: blue"> {{ Illuminate\Support\Facades\Session::get('intituleFilieres') }}</span>
                        <br> ou vous ne pouvez pas continuer une insertion de notes pour :
                        <span style="color: blue"> {{ Illuminate\Support\Facades\Session::get('intituleUE') }}</span>.
                        <br>Tous les étudiants ont certainement déjà validé cette unité d'enseignement
                    </p>
                </div>
                <hr>
                <div class="ms-auto ">
                    <a href={{ route('insertion.reprise') }}><button class="btn btn-info"
                            style="margin-right: 15px; margin-bottom: 15px">Retour</button></a>
                </div>
            </div>
        </div>
    @else --}}
    <div class="row justify-content-center mt-4" style="min-height: calc(85vh - 60px)">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header p-4"
                    style="background-color: {{ Session::get('RESULTAT_DATA')['RESULTAT_COULEUR_ETAB1'] }}; color: {{ Session::get('RESULTAT_DATA')['RESULTAT_COULEUR_TEXT1'] }};">
                    <h4 class="mb-0 text-white">Veuillez sélectionner les données s'il vous plait.</h4>
                </div>
                <div class="card-body">
                    <form class="form-horizontal" method="POST" action="{{ route('insertion.reprise.list') }}"
                        id="submit">
                        @csrf
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Plafonner</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="YesNo" name="plafond">
                                    <option value="0">NO : La note de l'UE n'est pas plafonnée à la note requise en
                                        cas
                                        de validation</option>
                                    <option value="1">YES : La note de l'UE est plafonnée à la note requise en cas
                                        de
                                        validation</option>
                                </select>
                            </div>
                        </div>

                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Filière</label>
                            <div class="col-md-6">
                                <select class="select2 p-2" style="width: 100%" id="filiere" name="idFiliere">
                                    <option>Selectionner la filière</option>
                                    @foreach ($data['filieres'] as $filiere)
                                        <option value="{{ $filiere->idFiliere }}">
                                            {{ $filiere->intituleFilieres }}({{ $filiere->sigleFilieres }})</option>
                                    @endforeach
                                </select>
                                <div class="invalid-feedback">
                                    La filière est requise
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Année Académique en
                                cours</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="acadYear" name="acadYear">
                                    <option value="{{ Session::get('RESULTAT_DATA')['RESULTAT_YEAR']->annee_acad }}">
                                        {{ Session::get('RESULTAT_DATA')['RESULTAT_YEAR']->periode }}</option>
                                </select>
                            </div>
                        </div>
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Année Académique de
                                reprise</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="acadYearReprise" name="acadYearReprise">
                                    <option value="2021">2020-2021</option>
                                    <option value="2020">2019-2020</option>
                                </select>
                            </div>
                        </div>
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Niveau formation</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="formation" name="formation">
                                    @foreach ($data['formations'] as $formation)
                                        <option value="{{ $formation->id }}">{{ $formation->niveauFormation }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Année d'Etude</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="etudeYear" name="etudeYear">
                                    <option>Sélectionner l'année d'étude</option>
                                    @foreach ($data['etudes'] as $etude)
                                        <option value="{{ $etude->id }}">{{ $etude->annee_etude }}</option>
                                    @endforeach
                                </select>
                                <div class="invalid-feedback">
                                    L'année d'étude est requise
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 pb-3 row border-bottom align-items-center">
                            <label for="" class="control-label text-md-end col-md-3">Choisir une UE</label>
                            <div class="col-md-6">
                                <select class="select2 p-2 select2-data-array" style="width: 100%" id="ue"
                                    name="ue">

                                </select>
                                <div class="invalid-feedback">
                                    L'UE est requise
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="row justify-content-center">

                                        <button type="submit" class="col-3 col-md-1 col-sm-2 me-3 btn btn-success">
                                            <i class="fa fa-check"></i> Submit</button>
                                        <button type="reset" class="col-4 col-md-2 col-sm-3 col-lg-1 me-3 btn "
                                            style="background-color: #e4e7ea">Cancel</button>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {{-- @endif --}}
@endsection

@section('scripts')
    <script src="{{ asset('assets/libs/select2/dist/js/select2.full.min.js') }}"></script>
    <script src="{{ asset('assets/libs/select2/dist/js/select2.min.js') }}"></script>
    <script src="{{ asset('dist/js/pages/forms/select2/select2.init.js') }}"></script>
    <script src="{{ asset('assets/extra-libs/toastr/dist/build/toastr.min.js') }}"></script>
    <script src="{{ asset('assets/extra-libs/toastr/toastr-init.js') }}"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.29/dist/sweetalert2.all.min.js"></script>
    <script>
        $(() => {
            let request = async () => {
                if (!(isNaN($("#filiere").val()) || isNaN($("#acadYear").val()) || isNaN($(
                            "#formation").val()) ||
                        isNaN($("#etudeYear").val()))) {
                    let url = "{{ route('ue') }}";
                    let token = document
                        .querySelector('meta[name="csrf-token"]')
                        .getAttribute("content");

                    let options = {
                        headers: {
                            "Content-Type": "application/json",
                            Accept: "application/json, text-plain, */*",
                            "X-Requested-With": "XMLHttpRequest",
                            "X-CSRF-TOKEN": token,
                        },
                        method: "post",
                        credentials: "same-origin",
                        body: JSON.stringify({
                            idFiliere: $("#filiere").val(),
                            acad_year: $("#acadYearReprise")
                                .val(), //acad_year: $("#acadYearReprise").val(),
                            formation: $("#formation").val(),
                            etude_year: $("#etudeYear")
                                .val() //- ($("#acadYear").val() - $("#acadYearReprise").val())
                        }),
                    };
                    await fetch(url, options)
                        .then((response) => response.json())
                        .then((response) => {
                            if (response.success) {
                                let data = response.data;
                                if (data.length) {
                                    let selectData = [];
                                    for (let semestre in data.ues) {
                                        let ues = [];
                                        for (let ue in data.ues[semestre]) {
                                            ues.push({
                                                id: ue,
                                                text: data.ues[semestre][ue]
                                            });
                                        }
                                        selectData.push({
                                            text: '============================== Semestre ' +
                                                semestre +
                                                ' ==============================',
                                            children: ues
                                        });
                                    }
                                    $(".select2-data-array").select2("destroy");
                                    $(".select2-data-array").select2({
                                        placeholder: "Choisir un élément constitutif",
                                        pagination: {
                                            "more": true
                                        },
                                        data: selectData
                                    });
                                } else
                                    toaster("warning", "Pas d'EC pour les données saisies.");
                            } else
                                toaster("warning",
                                    "Erreur de connection, veuillez recharger la page.");
                        })
                        .catch((e) => {
                            console.log(e)
                            toaster("error",
                                "Une erreur est survenue, veuillez recharger la page.");
                        });
                }
            };
            $('#filiere').change(request);
            $('#acadYear').change(request);
            $('#formation').change(request);
            $('#etudeYear').change(request);
            $(".select2-data-array").select2({
                placeholder: "Choisir un élément constitutif",
                language: {
                    noResults: () => "Traitement tableau de spécification en cours..."
                }
            });
            $('#submit').submit((e) => {
                if (![checkValidation('#YesNo'),
                        checkValidation('#filiere'),
                        checkValidation('#acadYear'),
                        checkValidation('#formation'),
                        checkValidation('#etudeYear'),
                        checkValidation('#ue'),
                        checkValidation('#acadYearReprise')
                    ].every(value => value === true)) {
                    e.preventDefault();
                }
            });
            @if (Session::get('noData'))
                let title = '<p>Aucun étudiant n\'est inscrit dans la filière : <span style="color:red">';
                title += "{{ Session::get('intituleFilieres') }}";
                title +=
                    '</span> </p> <p>Ou</p> <p>Vous ne pouvez pas continuer une une insertion de notes de rattrapage pour: <span style="color:red">';
                title += "{{ Session::get('intituleUE') }}";
                title += '</span>. Certainement tous les étudiants ont validés cette UE';
                title += "</p>";
                Swal.fire({
                    icon: 'error',
                    title: 'Oops...',
                    html: title
                });
            @endif
        });
    </script>
@endsection
