{% extends "SsoBackendBundle:Token:base.html.twig" %}

{% form_theme form ':Forms:jquery.collection.html.twig' %}

{% block title %}Update Users - CSV Import Configuration{% endblock %}

{% block stylesheets %}
    {{ parent() }}
    <style type="text/css">
        .ws_user_csv_configuration_rows-collection-actions a {
            margin-right: 5px;
        }
    </style>
{% endblock %}

{% block body %}
    <h1>Update Users - CSV Import Configuration</h1>
    <h3>For seamless integration of SSO into your project</h3>
    <p>
        Import users including encrypted passwords from previous databases using its hash algorithm. See the form below
        for available password hash algorithms. If your algorithm is not supported, please contact us.
    </p>
    <p>
        Please configure the import for each column. Be sure to have prepared the application attributes and roles of
        the applications used in your csv file. You have to flag exactly one column as the primary key. This column will
        be used to identify the user record. Press <span class="btn btn-xs btn-danger">Next</span> to go the upload page to update the users.
    </p>
    <p>Maximum upload file size: {{ maxUploadSize }}</p>
    <div id="csv_configure_form_container" class="well">
        {{ form_start(form) }}
        <div class="row">
            <div class="col-md-3">
                {{ form_row(form.columnDelimiter) }}
            </div>
            <div class="col-md-3">
                {{ form_row(form.textEnclosure) }}
            </div>
            <div class="col-md-3">
                {{ form_row(form.encryptType) }}
            </div>
            <div class="col-md-3">
                {{ form_row(form.autoCreateUser) }}
                {{ form_row(form.activateCreatedUser) }}
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                {{ form_rest(form) }}
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <ul class="record_actions">
                    <li>
                        <a class="btn btn-success" href="{{ path('_admin_backend_webservice_user') }}">
                            Back to the list
                        </a>
                    </li>
                    <li>
                        <button type="submit" class="btn btn-danger"
                                onclick="$('#ws_user_csv_configuration_processForm').val(1);">
                            Next
                        </button>
                    </li>
                </ul>
            </div>
        </div>
        {{ form_end(form) }}
    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script>
        (function () {
            var submitForm = false;
            var collectionConfig = {
                add: '<a href="#" class="btn btn-default"><span class="glyphicon glyphicon-plus-sign"></span> Add column</a>',
                remove: '<a href="#" class="btn btn-default" style="display: none;"><span class="glyphicon glyphicon-trash"></span> Remove column</a>',
                allow_up: false,
                allow_down: false,
                position_field_selector: '.dynamic-position-form',
                before_remove: function (collection, element) {
                    return $('#ws_user_csv_configuration_rows > .form-group').length > 2;
                }
            };
            $('.ws_user_csv_configuration_rows-collection-remove').hide();
            $(".form-collection").collection(collectionConfig);
            $('#csv_configure_form_container')
                .delegate('.dynamic-form', 'change', function () {
                    var $form = $(this).closest('form');
                    $.ajax({
                        url: $form.attr('action'),
                        type: $form.attr('method'),
                        data: $form.serialize(),
                        success: function (html) {
                            var formMarkup = $(html).find('#csv_configure_form_container form');
                            if (!formMarkup.length) {
                                window.location.reload();
                                return;
                            }
                            $form.replaceWith(formMarkup);
                            $(".form-collection").collection(collectionConfig);
                        }
                    });
                });
        })();
    </script>
{% endblock %}
