{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block title %}{{ "Applications"|trans }} | {% endblock %}

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-success XiboFormButton" title="{% trans "Add an Application" %}" href="{{ url_for("application.add.form") }}"> <i class="fa fa-plus-circle" aria-hidden="true"></i> {% trans "Add Application" %}</button>
        {% if currentUser.isSuperAdmin() %}
            <button class="btn btn-success XiboFormButton" title="{% trans "Add a DOOH Application" %}" href="{{ url_for("application.addDooh.form") }}"> <i class="fa fa-plus-circle" aria-hidden="true"></i> {% trans "Add DOOH Application" %}</button>
        {% endif %}
    </div>
{% endblock %}


{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Applications" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboFilter">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="applications" class="table table-striped">
                        <thead>
                            <tr>
                                <th>{% trans "Name" %}</th>
                                <th>{% trans "Owner" %}</th>
                                <th class="rowMenu"></th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javaScript %}
    <script type="text/javascript">
    
        {% autoescape "js" %}
            var copyToClipboardTrans = "{{ "Copy to Clipboard"|trans }}";
            var couldNotCopyTrans = "{{ "Could not copy"|trans }}";
            var copiedTrans = "{{ "Copied!"|trans }}";
        {% endautoescape %}

        var table = $("#applications").DataTable({ "language": dataTablesLanguage,
            serverSide: true,
            stateSave: true,
            responsive: true,
            stateDuration: 0,
            stateLoadCallback: dataTableStateLoadCallback,
            stateSaveCallback: dataTableStateSaveCallback,
            filter: false,
            searchDelay: 3000,
            "order": [[ 0, "asc"]],
            ajax: "{{ url_for("application.search") }}",
            "columns": [
                { "data": "name", "render": dataTableSpacingPreformatted },
                { "data": "owner" },
                {
                    "orderable": false,
                    responsivePriority: 1,
                    "data": dataTableButtonsColumn
                }
            ]
        });

        table.on('draw', dataTableDraw);
        table.on('processing.dt', dataTableProcessing);
        dataTableAddButtons(table, $('#applications_wrapper').find('.col-md-6').eq(1));
        
        function copyFromSecretInput() {
            // Initialize the tooltip.
            $('#copy-button').tooltip();

            $('#copy-button').bind('click', function() {
                
              var input = $('#clientSecret');
              
              // Select the input to copy 
              input.focus();
              input.select();
              
              // Try to copy to clipboard and give feedback
              try {
                var success = document.execCommand('copy');
                if (success) {
                  $('#copy-button').trigger('copied', [copiedTrans]);
                } else {
                  $('#copy-button').trigger('copied', [couldNotCopyTrans]);
                }
              } catch (err) {
                $('#copy-button').trigger('copied', [couldNotCopyTrans]);
              }
              
              // Unselect the input
              input.blur();
            });

            // Handler for updating the tooltip message.
            $('#copy-button').bind('copied', function(event, message) {
                const $self = $(this);
                $(this).tooltip('hide')
                    .attr('data-original-title', message)
                    .tooltip('show');

                setTimeout(function() {
                    $self.tooltip('hide').attr('data-original-title', copyToClipboardTrans);
                }, 1000);
            });
        }
    </script>
{% endblock %}