{% extends '@CadolesCore/base.html.twig' %}

{% block pagewrapper %}
{{ form_start(form) }}
    {% if moderegistration!="none" %}
        <h1 class="page-header">
        {% if mode=="send" %}
        Confirmation à l'Utilisateur = {{ registration.username }}
        {% elseif mode=="submit" %}
        Inscription
        {% elseif mode=="delete" %}
        Suppression Inscription = {{ registration.username }}
        {% endif %}
        </h1>

        {{ form_widget(form.submit) }} 

        {% if form.captcha is defined %}
            {{ form_label(form.captcha) }}
            {{ form_widget(form.captcha) }}
        {% endif %}

        {% if mode!="submit" %}
        {% if mode=="send" %} 
            {{ form_widget(form.save) }}
            <a class="btn btn-default" href="mailto:{{registration.email}}?subject= {{app.session.get('appname')}} : à propos de votre inscription">Envoyer un mail</a>
        {%endif%}
        
        <a class="btn btn-default" href={{ path('cadoles_core_config_registration') }}>Annuler</a>
        {% endif %}

        <br><br>
        

        {% if app.session.flashbag.has('error') %}
            <div class='alert alert-danger' style='margin: 5px 0px'>
                <strong>Erreur</strong><br>
                {% for flashMessage in app.session.flashbag.get('error') %}
                    {{ flashMessage | raw }}<br>
                {% endfor %}
            </div>
        {% endif %}

        {% if app.session.flashbag.has('notice') %}
            <div class='alert alert-info' style='margin: 5px 0px'>
                <strong>Information</strong><br>
                {% for flashMessage in app.session.flashbag.get('notice') %}
                    {{ flashMessage | raw }}<br>
                {% endfor %}
            </div>
        {% endif %}

        {% if mode=="send" %}
            <div class='alert alert-info' style='margin: 5px 0px'>
                <strong>Information</strong><br>
                Cette action permet de générer une nouvelle clé de validation et de renouveller la date d'expiration de cette clé.<br>
                Un email sera envoyé à l'utilisateur
            </div>
            <br>
        {% endif %}

        <div class="row">
            {% if mode=="send" %}
            <div class="col-sm-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-key fa-fw"></i> Notes Administrateur
                    </div>           
                    <div class="panel-body">     
                        {{ form_widget(form.note) }}
                    </div>
                </div>
            </div>
            {%endif%}

            <div class="col-sm-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-key fa-fw"></i> Connexion
                    </div>

                    <div class="panel-body">
                        {{ form_label(form.username) }}
                        {{ form_widget(form.username) }}

                        <div class='alert alert-info' style='font-size: 80%;padding: 5px;margin-top: -10px;'>
                            Formatez votre login sous la forme prenom.nom dans la mesure du possible<br>
                            Taille minimum = 5 caractères<br>
                            Caractères interdits = accent, espace, caractères spéciaux sauf @ . - _<br>
                        </div>

                        {% if form.password is defined %}
                            {{ form_row(form.password) }}

                            <div class='alert alert-info' style='font-size: 80%;padding: 5px;margin-top: -10px;'>
                                Le mot de passe doit comporter au moins {{ minpassword }} caractères, avec obligatoirement des lettres, chiffres et caractères spéciaux.
                            </div>                        
                        {% endif %}
                    </div>
                </div>

                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-pencil fa-fw"></i> Informations
                    </div>

                    <div class="panel-body">
                        {% if form.firstname is defined %} {{ form_row(form.firstname) }} {% endif %}
                        {{ form_row(form.lastname) }}
                        {% if form.email is defined %} 
                            {{ form_row(form.email) }} 
                            <div class='alert alert-info alert-email' style='display:none; font-size: 80%;padding: 5px;margin-top: -10px;'>
                                Votre mail ne fait pas partie des mails professonniels pré-inscrits dans {{app.session.get("appname")}}.<br>
                                S'il s'agit de votre mail professionnel vous devez compléter le formulaire "Motivations" pour que l'administrateur de {{app.session.get("appname")}} valide votre demannde d'inscription.
                            </div>                            
                        {% endif %}
                        {% if form.visible is defined %} {{ form_row(form.visible) }} {% endif %}
                    </div>
                </div>

                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-pencil fa-fw"></i> Motivations
                    </div>

                    <div class="panel-body">
                        {{ form_row(form.motivation) }}
                    </div>
                </div>
            </div>

            <div class="col-sm-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-sitemap fa-fw"></i> Organisation
                    </div>
                    <div class="panel-body">
                        {% if form.job is defined %} {{ form_row(form.job) }} {% endif %}
                        {% if form.position is defined %} {{ form_row(form.position) }} {% endif %}
                        {{ form_row(form.niveau01) }}
                        <div id="niveau01other">{{ form_row(form.niveau01other) }}</div>
                        {% if form.niveau02 is defined %} {{ form_row(form.niveau02) }} {% endif %}
                    </div>
                </div>

                <div id="paneloption" class="panel panel-primary">
                    <div class="panel-heading">
                        <i class="fa fa-user fa-fw"></i> Optionnel
                    </div>

                    <div class="panel-body">
                        {% if form.usualname is defined and form.gender is defined%}
                        <div class="row">
                        
                        <div class="col-xs-8">
                        {{ form_row(form.usualname) }} 
                        </div>
                        <div class="col-xs-4">
                        {{ form_row(form.gender) }}
                        </div>
                        </div>
                        {% elseif form.usualname is defined %} {{ form_row(form.usualname) }} 
                        {% elseif form.gender is defined %} {{ form_row(form.gender) }} 
                        {% endif %}

                        {% if form.givensname is defined %} {{ form_row(form.givensname) }} {% endif %}
                        {% if form.telephonenumber is defined %} {{ form_row(form.telephonenumber) }} {% endif %}
                        {% if form.postaladress is defined %} {{ form_row(form.postaladress) }} {% endif %}

                        {% if form.birthdate is defined and form.birthdate is defined%}
                        <div class="row">
                        <div class="col-xs-4">
                        {{ form_row(form.birthdate) }}
                        </div>
                        <div class="col-xs-8">
                        {{ form_row(form.birthcountry) }}
                        </div>
                        </div>
                        {% elseif form.birthdate is defined %} {{ form_row(form.birthdate) }} 
                        {% elseif form.birthcountry is defined %} {{ form_row(form.birthcountry) }} 
                        {% endif %}

                        <div id="blockcity">
                        {% if form.birthplace is defined %} {{ form_row(form.birthplace) }} {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{{ form_end(form) }}
{% endblock %}

{% block localjavascript %}


    $(document.body).on("change","#registration_birthcountry",function(){
        hideshow();
    });

    $(document.body).on("change","#registration_niveau01",function(){
        hideshow();
    });

    $(document.body).on("change","#registration_niveau02",function(){
        $.ajax({
            method: "POST",
            url: "{{ path('cadoles_core_ajax_niveau02_read') }}",
            data: "id="+$("#registration_niveau02").val(),
            success: function(data, dataType)
            {
                $("#registration_postaladress").val(data[0]);
            },

            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                
            }
        });        
    });

    $(document.body).on("focusout","#registration_email",function(){
        $.ajax({
            method: "POST",
            url: "{{ path('cadoles_core_ajax_iswhitelist') }}",
            data: {
                email:$("#registration_email").val(),
            },
            success: function(data) {
                if(data=="KO") {
                    $("#registration_motivation").attr("required","required");
                    $(".alert-email").show();
                    $("label[for='registration_motivation']").text("Motivation *");
                }
                else {
                    $("#registration_motivation").removeAttr("required");
                    $(".alert-email").hide();
                    $("label[for='registration_motivation']").text("Motivation");
                }
            }            
        });        

    });

    function hideshow() {
        if($("#registration_birthcountry").val()==269)
            $("#blockcity").show();
        else {
            $("#blockcity").hide();
            $("#registration_birthplace").val([]).trigger('change');
        }

        niveau01=$("#registration_niveau01 option:selected").text().toLowerCase();
        if(niveau01.startsWith("autre")) {
            $("#niveau01other").show();
            $("#registration_niveau01other").attr("required","required");
        }
        else {
            $("#registration_niveau01other").val("");
            $("#niveau01other").hide();
            $("#registration_niveau01other").removeAttr("required");
        }
    }

    $(document).ready(function() {
        if(!$("#paneloption").find(".panel-body").find("label").length) $("#paneloption").hide();
        hideshow();
    });
{% endblock %}
