@extends('layouts.base')

@section('title', 'Starter Page')

@section('css')
    {{-- include page's specific styles here  --}}

@endsection

@section('content')
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info p-4">
                    <h4 class="mb-0 text-white">Veuillez sélectionner les données s'il vous plait.</h4>
                </div>
                <div class="card-body">
                    <form method="POST" class="form-horizontal" id="submit" action="{{ route('dossier') }}" target="_blank">
                        @csrf
                        <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 value="">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</label>
                            <div class="col-md-6">
                                <select class="form-select mr-sm-2 p-2" id="acadYear" name="acadYear">
                                    @foreach ($data['academique'] as $acad)
                                        <option value="{{ $acad->annee_acad }}">{{ $acad->periode }}</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">Niveau formation</label>
                            <div class="col-md-6">

                                <select class="form-select mr-sm-2 p-2" id="formation" name="formation">
                                    <option value="">Selectionner la niveau de formation</option>
                                    @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="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>
@endsection

@section('script')
    {{-- include page's specific scripts here --}}
    <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>
        let checkValidation = (id) => {
            $(id).addClass('is-valid');
            return true;
        }

        $('#filiere')?.change(() => {
            checkValidation('#filiere');
        });


        $('#etudeYear')?.change(() => {
            checkValidation('#etudeYear');
        });


        $('#formation')?.change(() => {
            checkValidation('#formation');
        });

        $('#acadYear')?.change(() => {
            checkValidation('#acadYear');
        });

        $('#submit').submit((e) => {
            if (![
                    checkValidation('#filiere'),
                    checkValidation('#acadYear'),
                    checkValidation('#formation'),
                    checkValidation('#etudeYear'),
                ].every(value => value === true)) {
                e.preventDefault();
            }
        });
        @if (Session::get('error'))
            Swal.fire({
                icon: 'error',
                title: "Erreur",
                html: "<p>{!! Session::get('message') !!}</p>"
            });
        @endif
    </script>
@endsection
