jQuery(document).ready(function () {
    initSovSortableList();
});

function initSovSortableList() {

    if ($('.sov-sortable').length > 0) {
        $('.sov-sortable tbody').sortable({
            placeholder: "ui-state-highlight"
        });

        $('.sov-sortable tbody').on("sortupdate", function (event, ui) {

            prototype = $('#form_entities').data('prototype');

            $('.sov-sortable tr.sov-draggable').each(function (index, li) {

                // instead be a number based on how many items we have
                var newForm = prototype.replace(/__name__/g, index);

                // Replace '__name__' in the prototype's HTML to
                $(li).find('div:last-child').remove();
                $(li).find('td:first').append('<div class="hidden">'+newForm+'</div>');
                $(li).find('#form_entities_' + index + '_id').val($(li).data('id'));
              /*  if ($('.sov-sortable').data('parent-position') !== '') {
                    //Ajout d'un 0 initial pour les nuémros <10
                    indexAsString = index.toString().padStart(2, '0');
                    val = $('.sov-sortable').data('parent-position') + '.' + indexAsString
                } else {*/
                    val = index;
                //}
                $(li).find('#form_entities_' + index + '_position').val(val);
            });

        });
    }
}

