@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            @include('admin.sidebar')
            <div class="col-md-9">
                <div class="card">
                    <h3 class="card-header">Créer un nouveau Jury</h3>
                    <div class="card-body">
                        @if ($type == 'select-filiere')
                        <a href="{{ url('/admin/jury') }}" title="Back">
                            <button class="btn btn-warning btn-sm">
                                <i class="fa fa-arrow-left" aria-hidden="true"></i>
                                Retour
                            </button>
                        </a>
                        @else
                        <a href="{{ url('/admin/jury/create') }}" title="Back">
                            <button class="btn btn-warning btn-sm">
                                <i class="fa fa-arrow-left" aria-hidden="true"></i>
                                Choix filiere
                            </button>
                        </a>
                        @endif
                        <br />
                        <br />
                        @if ($errors->any())
                            <ul class="alert alert-danger">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        @endif
                        @if ($type == 'select-filiere')
                        <form
                            method="POST"
                            action="{{ url('/admin/jury') }}"
                            accept-charset="UTF-8"
                            style="display:inline"
                        >
                            {{ csrf_field() }}
                            <div class="form-group {{ $errors->has('id_filiere') ? 'has-error' : ''}}">
                                <label for="id_filiere" class="control-label">{{ 'Filière' }}</label>
                                <select name="id_filiere" class="form-control" id="id_filiere" required="required">
                                @foreach ($filieres as $filiere)
                                    <option
                                        value="{{ $filiere->idFiliere }}" {{ (isset($jury->id_filiere) && $jury->id_filiere == $filiere->idFiliere) ? 'selected' : ''}}
                                    >
                                        {{ $filiere->intituleFilieres }}
                                    </option>
                                @endforeach
                                </select>
                                <input type="hidden" name="_type" value="select-filiere">
                                {!! $errors->first('id_filiere', '<p class="help-block">:message</p>') !!}
                            </div>
                            <div class="form-group">
                                <label for="id_niveau" class="control-label">{{ 'Niveau formation' }}</label>
                                <select name="id_niveau" class="form-control" id="id_niveau" required="required">
                                    <option value="2"> Master </option>
                                    <option value="0"> Master Grade Ingénieur</option>
                                    <option value="1"> Licence </option>
                                </select>
                            </div>
                            <button type="submit" class="btn no-delete btn-primary btn-sm"><i class="fa fa-check-o" aria-hidden="true"></i> Choisir </button>
                        </form>
                        @else
                        <form
                            id="createJuryForm"
                            method="POST"
                            action="{{ url('/admin/jury') }}"
                            accept-charset="UTF-8"
                            class="form-horizontal"
                            enctype="multipart/form-data"
                        >
                            {{ csrf_field() }}
                            <input type="hidden" name="_type" value="submission">
                            @include ('admin.jury.form', [
                                'formMode' => 'create',
                                'filieres' => $filieres,
                                'membres_jury' => $membres_jury,
                                'etudiants' => $etudiants,
                                'salles' => $salles,
                                'signataires' => $signataires,
                                'type' => $type,
                                'selected_filiere' => $selected_filiere
                            ])
                        </form>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="{{ asset('ample-admin-new/plugins/bower_components/jquery/dist/jquery.min.js')}}"></script>
<script type="text/javascript">
    $('#createJuryForm').submit(function( event ) {
        event.preventDefault();
        var action = $(this).attr("action");
        var request_method = $(this).attr("method");
        var form_data = $(this).serialize(); //Encode form elements for submission

        $.ajax({
            url : action,
            type: request_method,
            data : form_data
        }).done(function(response) {
            swal({
                title: response.message,
                icon: response.status,
                confirmButtonText:
                'Continue<i class="fa fa-arrow-right"></i>',
            });
            if (response.status == "success") {
                setTimeout(function(){ 
                    window.location = "{{ url('/admin/jury') }}";
                }, 2000);
            }
            if (response.id_jury_conflituel) {
                setTimeout(function(){ 
                    var host = "{{ url('') }}";
                    var url = "/admin/jury/" + response.id_jury_conflituel
                    console.log('host :', host, ',  url:', url)
                    window.open(host  + url);
                    window.focus()
                }, 500);
            }
        });
    });
</script>
@endsection
