{% set nbReceived = records.received().count() %}
{% set nbEligibleDocuments = records.count() %}

{% if nbEligibleDocuments > 0 %}
    {% if nbReceived != records.count() %}
        <div class="well-small" style="text-align:center;">
            <span class="label label-info">{{ "You do not have rights to remove all selected documents. Are you sure ?" | trans }}</span>
        </div>
    {% endif %}

    <form style="margin: 0; height: 110px;" method="POST" action="{{ path('record_delete') }}">
        <input type="hidden" value="{{ records.serializedList() }}" name="lst" />
        {% if goingToTrash %}
            <div class="well-small label-important"
                 style="background-color: #ffef22;color:#000;padding:5px;display:block;word-wrap:break-word;font-weight: bold;height: 100%;">
                <div class="dialog-left-section">
                    <img src="/assets/common/images/icons/Icone-collection-corbeille.png" width="57" height="101"/>
                </div>
                <div class="dialog-right-section" style="margin-top: 10px;">
                    <span>{{ "prod:app trash: move-to-trash" | trans }}</span>
                    {% if records.stories().count() %}
                        <label class="checkbox">
                            <input type="checkbox" id="del_children" name="del_children"
                                   value="1"> {{ "Also delete records that rely on groupings." | trans }}
                        </label>
                    {% endif %}
                    <div class="form-actions" style="background-color:transparent;">
                        <button type="button" class="btn btn-danger submiter">{{ "Delete" | trans }}
                            ({{ nbEligibleDocuments }})
                        </button>
                        <button type="button" class="btn cancel">{{ "Cancel" | trans }}</button>
                        <span class="form-action-loader" style="display:none;">
                            <img src="/assets/common/images/icons/loader000.gif"/>
                        </span>
                    </div>
                </div>
            </div>
        {% else %}
            <div class="well-small label-important"
                 style="background-color: #ed1c24;color:#000;padding:5px;display:block;word-wrap:break-word;font-weight: bold;height: 100%;">
                <div class="dialog-left-section">
                    <img src="/assets/common/images/icons/Icone-vider-corbeille.png" width="57" height="101"/>
                </div>
                <div class="dialog-right-section">
                    <span>{{ "These records will be definitely deleted and can not be recovered" | trans }}</span>
                    {% if records.stories().count() %}
                        <label class="checkbox">
                            <input type="checkbox" id="del_children" name="del_children"
                                   value="1"> {{ "Also delete records that rely on groupings." | trans }}
                        </label>
                    {% endif %}
                    <div class="form-actions" style="background-color:transparent;">
                        <button type="button" class="btn btn-danger submiter">{{ "Delete" | trans }}
                            ({{ nbEligibleDocuments }})
                        </button>
                        <button type="button" class="btn cancel">{{ "Cancel" | trans }}</button>
                        <span class="form-action-loader" style="display:none;">
                        <img src="/assets/common/images/icons/loader000.gif"/>
                    </span>
                    </div>
                </div>
            </div>
        {% endif %}
    </form>

    <script type="text/javascript">
        var $dialog = p4.Dialog.get(1);
        var $dialogBox = $dialog.getDomElement();
        var $closeButton = $("button.ui-dialog-titlebar-close", $dialogBox.parent());
        var $cancelButton = $("button.cancel", $dialogBox);

        $cancelButton.bind("click", function(){
            $dialog.Close();
        });

        $("button.submiter", $dialogBox).bind("click", function(){
            var $this = $(this);
            var form = $(this).closest("form");
            var loader = form.find("form-action-loader");

            $.ajax({
                type: form.attr("method"),
                url: form.attr("action"),
                data: form.serializeArray(),
                dataType: "json",
                beforeSend:function(){
                    $this.prop('disabled', true);
                    $closeButton.prop('disabled', true);
                    $cancelButton.prop('disabled', true);
                    $dialog.setOption('closeOnEscape', false);
                    loader.show();
                },
                success: function(data){
                    $dialog.Close();
                    $.each(data,function(i,n){
                        var imgt = $('#IMGT_'+n),
                        chim = $('.CHIM_'+n),
                        stories = $('.STORY_'+n);
                        $('.doc_infos', imgt).remove();
                        imgt.unbind("click").removeAttr("ondblclick").removeClass("selected").draggable("destroy").removeClass("IMGT").find("img").unbind();
                        imgt.find(".thumb img").attr("src","/assets/common/images/icons/deleted.png").css({
                            width:'100%',
                            height:'auto',
                            margin: '0 10px',
                            top: '0'
                        });
                        chim.parent().slideUp().remove();
                        imgt.find(".status,.title,.bottom").empty();

                        p4.Results.Selection.remove(n);
                        if(stories.length > 0)
                        {
                            p4.WorkZone.refresh();
                        }
                        else
                        {
                            p4.WorkZone.Selection.remove(n);
                        }
                    });
                    viewNbSelect();
                },
                complete: function(){
                    $this.prop('disabled', false);
                    $closeButton.prop('disabled', false);
                    $cancelButton.prop('disabled', false);
                    $dialog.setOption('closeOnEscape', true);
                    loader.hide();
                }
            });
        });
    </script>
{% elseif nbReceived == 0 %}
    <div class="well-small" style="text-align:center;">
        <span class="label label-important">{{ "No document selected" | trans }}</span>
    </div>
{% else %}
    <div class="well-small" style="text-align:center;">
        <span class="label label-info">{{ "You do not have rights to remove selected documents" | trans }}</span>
    </div>
{% endif %}