<script type="text/javascript">
    $(document).ready(function(){

        resize();

        $(window).resize(function(){
            resize();
        });

        var $form = $("form#accept-demand");
        var $mainBtnAction = $(".btn-all-action", $form);
        var $basesBtnAction = $(".btn-single-action", $form);

        $(".btn-all-action, .btn-single-action").on("uncheck-all", function(event) {
            $("button", $(event.currentTarget)).removeClass("active");
        }).on("uncheck-deny", function(event) {
            var $btnGroup = $(event.currentTarget);
            var $denyButton = $(".deny-checker", $btnGroup);

            if ($denyButton.hasClass("active")) {
                $denyButton.removeClass("active");
                $(".deny-checker", $mainBtnAction).removeClass("active");
            }
        }).on("check-deny", function(event) {
            var $btnGroup = $(event.currentTarget);
            var $denyButton = $(".deny-checker", $btnGroup);

            if (false === $denyButton.hasClass("active")) {
                $btnGroup.trigger("uncheck-all");
                $denyButton.addClass("active");

                var nbActiveButton = $(".deny-checker.active", $basesBtnAction).length;
                var totalBtn = $(".deny-checker", $basesBtnAction).length;

                if (nbActiveButton === totalBtn) {
                    $(".deny-checker", $mainBtnAction).addClass("active");
                }
            }
        }).on("check-accept", function(event) {
            var $btnGroup = $(event.currentTarget);
            var $acceptBtn = $(".accept-checker", $btnGroup);

            if (false === $acceptBtn.hasClass("active"))  {
                $btnGroup.trigger('uncheck-deny');
                $acceptBtn.addClass("active");

                var nbActiveButton = $(".accept-checker.active", $basesBtnAction).length;
                var totalBtn = $(".accept-checker", $basesBtnAction).length;

                if (nbActiveButton === totalBtn) {
                    $(".accept-checker", $mainBtnAction).addClass("active");
                }
            }
        }).on("uncheck-accept", function(event) {
            var $btnGroup = $(event.currentTarget);
            var $acceptBtn = $(".accept-checker", $btnGroup);

            if ($acceptBtn.hasClass("active")) {
                $acceptBtn.removeClass("active");
                $(".accept-checker", $mainBtnAction).removeClass("active");

                $btnGroup.trigger("uncheck-hd");
                $btnGroup.trigger("uncheck-watermark");
            }
        }).on('check-hd', function(event) {
            var $btnGroup = $(event.currentTarget);
            var $hdBtn = $(".accept-hd-checker", $btnGroup);

            if (false === $hdBtn.hasClass("active")) {
                $hdBtn.addClass('active');

                var nbActiveButton = $(".accept-hd-checker.active", $basesBtnAction).length;
                var totalBtn = $(".accept-hd-checker", $basesBtnAction).length;

                if (nbActiveButton === totalBtn) {
                    $(".accept-hd-checker", $mainBtnAction).addClass("active");
                }

                $btnGroup.trigger('uncheck-deny').trigger('check-accept');
            }
        }).on('uncheck-hd', function(event) {
            var $btnGroup = $(event.currentTarget);
            var $hdBtn = $(".accept-hd-checker", $btnGroup);

            if ($hdBtn.hasClass("active")) {
                $hdBtn.removeClass("active");
                $(".accept-hd-checker", $mainBtnAction).removeClass("active");
            }
        }).on('check-watermark', function(event) {
            var $btnGroup = $(event.currentTarget);
            var $watermarkBtn = $(".accept-watermark-checker", $btnGroup);

            if (false === $watermarkBtn.hasClass("active")) {
                $watermarkBtn.addClass('active');

                var nbActiveButton = $(".accept-watermark-checker.active", $basesBtnAction).length;
                var totalBtn = $(".accept-watermark-checker", $basesBtnAction).length;

                if (nbActiveButton === totalBtn) {
                    $(".accept-watermark-checker", $mainBtnAction).addClass("active");
                }

                $btnGroup.trigger('uncheck-deny').trigger('check-accept');
            }
        }).on('uncheck-watermark', function(event) {
            var $btnGroup = $(event.currentTarget);
            var $watermarkBtn = $(".accept-watermark-checker", $btnGroup);

            if ($watermarkBtn.hasClass("active")) {
                $watermarkBtn.removeClass('active');
                $(".accept-watermark-checker", $mainBtnAction).removeClass("active");
            }
        });

        $.each([
            $('.deny-checker', $basesBtnAction),
            $('.accept-checker', $basesBtnAction),
            $('.accept-hd-checker', $basesBtnAction),
            $('.accept-watermark-checker', $basesBtnAction)
        ], function(i, buttons) {
            buttons.bind("click", function(event) {
                var $this = $(this);
                var $btnGroup = $this.closest('.btn-group');
                var event = $this.data('event');

                if ($this.hasClass("active")) {
                    $btnGroup.trigger("uncheck-"+ event);
                } else {
                    $btnGroup.trigger('check-' + event);
                }
            });
        });

        $.each([
            $('.deny-checker', $mainBtnAction),
            $('.accept-checker', $mainBtnAction),
            $('.accept-hd-checker', $mainBtnAction),
            $('.accept-watermark-checker', $mainBtnAction)
        ], function(i, buttons) {
            buttons.bind("click", function(event) {
                var $this = $(this);
                var event = $this.data('event');

                if ($this.hasClass("active")) {
                    $mainBtnAction.trigger("uncheck-"+ event);
                    $basesBtnAction.trigger("uncheck-"+ event);
                } else {
                    $mainBtnAction.trigger('check-' + event);
                    $basesBtnAction.trigger('check-' + event);
                }
            });
        });

        $(".submit", $form).bind("click", function (event) {
            $(".btn.active", $basesBtnAction).each(function() {
                $("<input>").attr("type", "hidden").attr("name", this.name).val(this.value).appendTo($form);
            });

            $form.submit();
        });
    });
</script>

<div class="page-header">
     <h1>{% trans 'admin:: demandes en cours' %}</h1>
</div>

{% if app['request'].query.get('success') == '1' %}
        <div class="alert alert-success">
            <a class="close" data-dismiss="alert" href="#">×</a>
            {% trans 'Requests for the application registrations were recorded successfully' %}
        </div>
{% endif %}

{% if table['user'] | length > 0 %}
    <form id="accept-demand" action="{{ path('users_submit_demands') }}" method="post">
        <div class="btn-group btn-all-action">
            <button data-event="deny" class="btn deny-checker" type="button">
                <img title="{% trans 'admin:: refuser l\'acces' %}" src="/skins/icons/delete.gif"/>
            </button>
            <button data-event="accept" class="btn accept-checker" type="button">
                <img title="{% trans 'admin:: donner les droits de telechargement et consultation de previews' %}" src="/skins/icons/cmdok.gif"/>
            </button>
            <button data-event="hd" class="btn accept-hd-checker" type="button">
                <span title="{% trans 'admin:: donner les droits de telechargements de preview et hd' %}">HD</span>
            </button>
            <button data-event="watermark" class="btn accept-watermark-checker" type="button">
                <span title="{% trans 'admin:: watermarquer les documents' %}">W</span>
            </button></td>
        </div>

        <table class="table table-condensed" style="margin:0; table-layout:fixed;">
            <thead>
                <tr>
                    <th>{% trans 'Informations' %}</th>
                    <th>{% trans 'admin::collection' %}</th>
                    <th>{% trans 'Models' %}</th>
                </tr>
            </thead>
        </table>

        <div class="registration-wrapper PNB" style="top:160px;bottom: 50px;overflow: auto">
        <div  id="tab_demandes">
            {% set tableColls = table['coll'] %}
            {% for row in table['user'] %}
                <div class="well well-small">
                    <table class="table" style="table-layout: fixed;">
                        <tr>
                            <span>
                                {{ app['date-formatter'].getPrettyString(row["date_modif"]) }}
                            </span>
                        </tr>
                        <tr>
                            <td>
                                {% set colls = tableColls[row['usr_id']] %}
                                <dl class="dl-horizontal">
                                    <dt>{% trans 'admin::compte-utilisateur identifiant' %}</dt>
                                    <dd>{{ row['usr_login'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur nom' %} / {% trans 'admin::compte-utilisateur prenom' %}</dt>
                                    <dd>{{ row['usr_nom'] }}  {{ row['usr_prenom'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur email' %}</dt>
                                    <dd>{{ row['usr_mail'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur societe' %}</dt>
                                    <dd>{{ row['societe'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur poste' %}</dt>
                                    <dd>{{ row['fonction'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur activite' %}</dt>
                                    <dd>{{ row['activite'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur telephone' %}</dt>
                                    <dd>{{ row['tel'] }}</dd>
                                    <dt>{% trans 'admin::compte-utilisateur adresse' %}</dt>
                                    <dd>{{ row['adresse'] }}</dd>
                                    <dd>{{ row['cpostal'] }}</dd>
                                    <dd>{{ row['ville'] }}</dd>
                                    <dd>{{ row['pays'] }}</dd>
                                </dl>
                            </td>
                            <td>
                                {% for basId in colls %}
                                    <span style="font-weight:bold;font-size:14px;word-wrap: break-word;">
                                        {{ basId| bas_labels(app) }}
                                    </span>
                                    {% set btn_value = row['usr_id'] ~ "_" ~  basId %}
                                    <div class="btn-group btn-single-action" style="margin:auto auto 5px 0;">
                                        <button data-event="deny" value="{{ btn_value }}" type="button" name="deny[]" class="btn deny-checker" title="{% trans 'admin:: refuser l\'acces' %}">
                                            <img title="{% trans 'admin:: refuser l\'acces' %}" src='/skins/icons/delete.gif' />
                                        </button>
                                        <button data-event="accept" value="{{ btn_value }}" type="button" name="accept[]" class="btn accept-checker" title="{% trans 'admin:: donner les droits de telechargement et consultation de previews' %}">
                                            <img title="{% trans 'admin:: donner les droits de telechargement et consultation de previews' %}" src='/skins/icons/cmdok.gif' />
                                        </button>
                                        <button data-event="hd" value="{{ btn_value }}" type="button" name="accept_hd[]" class="btn accept-hd-checker" title="{% trans 'admin:: donner les droits de telechargements de preview et hd' %}">
                                            <span title="{% trans 'admin:: donner les droits de telechargements de preview et hd' %}">HD</span>
                                        </button>
                                        <button data-event="watermark" value="{{ btn_value }}" type="button" name="watermark[]" class="btn accept-watermark-checker" title="{% trans 'admin:: watermarquer les documents' %}">
                                            <span title="{% trans 'admin:: watermarquer les documents' %}">W</span>
                                        </button>
                                    </div>
                                {% endfor %}
                            </td>
                            <td>
                                <div>{% trans 'admin:: appliquer le modele' %}</div>
                                <select name="template[]" id="model_{{ row['usr_id'] }}" onchange="modelChecker('{{ row['usr_id'] }}')">
                                    <option value="">{% trans 'None' %}</option>
                                    {% for model in models %}
                                        <option value="{{ row['usr_id'] }}_{{ model['usr_id'] }}">{{ model['usr_login'] }}</option>
                                    {% endfor %}
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
            {% endfor %}
        </div>
        </div>

        <div class="PNB" style="top:auto;height: 30px;">
            <button type="button" class="btn btn-primary submit">{% trans 'boutton::valider' %}</button>
        </div>
    </form>
 {% else %}
    {% trans 'No application for registration has been recorded' %}
 {% endif %}
