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

{% block stylesheets %}
    {{ parent() }}
    <style>
        .row.display-flex {
            display: flex;
            flex-wrap: wrap;
        }

        .thumbnail {
            height: 100%;
        }
    </style>
{% endblock %}

{% block title %}{{ parent() }} - Generate Unique Ids{% endblock %}

{% block body %}
    <h1>Generate Unique Employee Ids</h1>
    <div id="generate-success" class="alert alert-success" style="display: none">
        Employee-ids have been generated successfully.
    </div>
    <div id="generate-failure" class="alert alert-warning" style="display: none">
        Error while generating employee-ids!
    </div>
    <div id="remove-success" class="alert alert-success" style="display: none">
        Previously reserved employee-ids have been removed successfully.
    </div>
    <div id="remove-failure" class="alert alert-warning" style="display: none">
        Error while removing reserved employee-ids!
    </div>
    <p>Currently available ids: <span id="generated-ids" class="badge">{{ count }}</span></p>
    {% if is_granted('ROLE_SERVICE_ADMIN') %}
        <div class="row display-flex">
            <div class="col-xs-10 col-md-5" style="margin-bottom: 1rem;">
                <div class="thumbnail">
                    <div class="caption">
                        <h3>Re-Index</h3>
                        <p>Re-Index list of unique ids.</p>
                        <p><strong>Do not proceed unless you know what you are doing!</strong></p>
                        <p>
                            <button class="btn btn-danger" data-loading-text="Generating..." id="generate-btn">Run
                            </button>
                        </p>
                    </div>
                </div>
            </div>
            <div class="col-xs-10 col-md-5" style="margin-bottom: 1rem;">
                <div class="thumbnail">
                    <div class="caption">
                        <h3>Remove reserved</h3>
                        <p>Remove employee-ids generated with previous generator.</p>
                        <p><strong>Do not proceed unless you know what you are doing!</strong></p>
                        <p>
                            <button class="btn btn-warning" data-loading-text="Removing..." id="remove-btn">Run</button>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}

{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script>
        (function () {
            $("#generate-btn").on("click", function () {
                var btn = $(this).button('loading');
                $(".alert").hide();
                $.post('{{ path('attribute_unique_generator_generate') }}', function (response) {
                    $("#generate-success").show();
                    $("#generated-ids").text(response.count);
                }).fail(function () {
                    $("#generate-failure").show();
                }).always(function () {
                    btn.button('reset');
                });
            });
            $("#remove-btn").on("click", function () {
                var btn = $(this).button('loading');
                $(".alert").hide();
                $.post('{{ path('attribute_unique_generator_remove_reserved') }}', function (response) {
                    $("#remove-success").show();
                    $("#generated-ids").text(response.count);
                }).fail(function () {
                    $("#remove-failure").show();
                }).always(function () {
                    btn.button('reset');
                });
            });
        })();
    </script>
{% endblock %}
