{% extends "SsoDemoBundle:AttributeGroup:base.html.twig" %}

{% block title %}{{ parent() }} - Update {% endblock %}

{% block body %}
    <h1>AttributeGroup - Update</h1>

    {{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}

    <ul class="addTypes" data-prototype="{{ form_widget(form.AddTypes.vars.prototype, {'label': 'tesssst'})|escape }}">
    </ul>
    <ul class="deleteTypes" data-prototype="{{ form_widget(form.DeleteTypes.vars.prototype)|escape }}">
    </ul>
    <script>
        var $collectionHolderAdd, $collectionHolderDelete;
        var $addTagLink = $('<a class="btn btn-danger" href="#" class="add_tag_link">Add Type</a>');
        var $removeTagLink = $('<a class="btn btn-danger" href="#" class="delete_tag_link">Remove Type</a>');
        var $newLinkLi = $('<div></div>').append($addTagLink);
        var $newDeleteLinkLi = $('<div></div>').append($removeTagLink);

        jQuery(document).ready(function () {

            $collectionHolderAdd = $('#form_AddTypes');
            $collectionHolderDelete = $('#form_DeleteTypes');

            $collectionHolderAdd.append($newLinkLi);
            $collectionHolderDelete.append($newDeleteLinkLi);

            $collectionHolderAdd.data('index', $collectionHolderAdd.find(':input').length);
            $collectionHolderDelete.data('index', $collectionHolderDelete.find(':input').length);

            $addTagLink.on('click', function (e) {
                e.preventDefault();
                appendTagForm($collectionHolderAdd, $newLinkLi);
            });

            $removeTagLink.on('click', function (e) {
                e.preventDefault();
                appendTagForm($collectionHolderDelete, $newDeleteLinkLi);
            });

            function appendTagForm($collectionHolder, $newLinkLi) {
                var prototype = $collectionHolder.data('prototype');
                var index = $collectionHolder.data('index');
                var newForm = prototype.replace(/__name__/g, '').replace(/label__/g, '');
                $collectionHolder.data('index', index + 1);
                var $newFormLi = $('<div class="form-group"></div>').append(newForm);

                $newFormLi.append('<a href="#" class="remove-tag btn btn-danger">x</a>');
                $newLinkLi.before($newFormLi);

                $('.remove-tag').click(function (e) {
                    e.preventDefault();
                    $(this).parent().remove();
                    return false;
                });
            }

        });

    </script>

    {% if wsRequest %}
        <h3>Webservice Request</h3>
        <pre>{{ wsRequest }}</pre>
    {% endif %}

    {% if wsResponse %}
        <h3>Webservice Response</h3>
        <pre>{{ wsResponse }}</pre>
    {% endif %}

    <h3>Help</h3>
    <pre>{% include 'SsoHelpBundle:AttributeGroup:xml/update.html.twig' %}</pre>
{% endblock %}
