{#
/*
 * OTS Signs Theme - Fonts Page
 * Based on Xibo CMS fonts-page.twig with OTS styling
 */
#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

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

{% block actionMenu %}{% endblock %}

{% block pageContent %}
    <div class="ots-static-page ots-displays-page">
        <div class="page-header ots-page-header">
            <h1>{% trans "Fonts" %}</h1>
            <p class="text-muted">{% trans "Manage fonts for your signage content." %}</p>
        </div>

        <div class="widget content-card ots-displays-card">
            <div class="widget-body ots-displays-body">
                <div class="XiboGrid" id="{{ random() }}" data-grid-name="fontView">
                    <div class="XiboFilter card mb-3 bg-light content-card ots-filter-card">
                        <div class="ots-filter-header">
                            <h3 class="ots-filter-title">{% trans "Filter Fonts" %}</h3>
                            <button type="button" class="ots-filter-toggle" id="ots-filter-collapse-btn" title="{% trans 'Toggle filter panel' %}">
                                <i class="fa fa-chevron-down"></i>
                            </button>
                        </div>
                        <div class="ots-filter-content collapsed" id="ots-filter-content">
                            <div class="FilterDiv card-body" id="Filter">
                                <form class="form-inline">
                                    {% set title %}{% trans "ID" %}{% endset %}
                                    {{ inline.number("id", title) }}

                                    {% set title %}{% trans "Name" %}{% endset %}
                                    {{ inline.inputNameGrid('name', title) }}
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="XiboData card pt-3 content-card ots-table-card">
                        <div class="ots-table-toolbar">
                            {% if currentUser.featureEnabled("font.add") %}
                                <button class="btn btn-sm btn-success ots-toolbar-btn" href="#" id="fontUploadForm" title="{% trans "Add a new Font" %}"><i class="fa fa-plus-circle" aria-hidden="true"></i></button>
                            {% endif %}
                            <button class="btn btn-sm btn-primary ots-toolbar-btn" id="refreshGrid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></button>
                        </div>
                        <table id="fonts" class="table table-striped" data-state-preference-name="fontGrid">
                            <thead>
                                <tr>
                                    <th>{% trans "ID" %}</th>
                                    <th>{% trans "name" %}</th>
                                    <th>{% trans "File Name" %}</th>
                                    <th>{% trans "Created" %}</th>
                                    <th>{% trans "Modified" %}</th>
                                    <th>{% trans "Modified By" %}</th>
                                    <th>{% trans "Size" %}</th>
                                    <th class="rowMenu"></th>
                                </tr>
                            </thead>
                            <tbody>

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

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
      var fontsTable;
      $(document).ready(function() {
        fontsTable = $("#fonts").DataTable({
          "language": dataTablesLanguage,
          dom: dataTablesTemplate,
          serverSide: true,
          stateSave: true,
          responsive: true,
          stateDuration: 0,
          stateLoadCallback: dataTableStateLoadCallback,
          stateSaveCallback: dataTableStateSaveCallback,
          filter: false,
          searchDelay: 3000,
          "order": [[1, "asc"]],
          ajax: {
            url: "{{ url_for("font.search") }}",
            data: function (d) {
              $.extend(d, $("#fonts").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
            }
          },
          "columns": [
            {"data": "id", responsivePriority: 2},
            {"data": "name", responsivePriority: 2},
            {"data": "fileName", responsivePriority: 4},
            {"data": "createdAt", responsivePriority: 3},
            {"data": "modifiedAt", responsivePriority: 3},
            {"data": "modifiedBy", responsivePriority: 3},
            {
              "name": "size",
              responsivePriority: 3,
              "data": null,
              "render": {"_": "size", "display": "fileSizeFormatted", "sort": "size"}
            },
            {
              "orderable": false,
              responsivePriority: 1,
              "data": dataTableButtonsColumn
            }
          ]
        });

        fontsTable.on('draw', dataTableDraw);
        fontsTable.on('processing.dt', dataTableProcessing);
        dataTableAddButtons(fontsTable, $('#fonts_wrapper').find('.dataTables_buttons'));

        $("#refreshGrid").click(function () {
            fontsTable.ajax.reload();
        });
      });

      $("#fontUploadForm").click(function(e) {
        e.preventDefault();

        openUploadForm({
          url: "{{ url_for("font.add") }}",
          title: "{% trans "Add Font" %}",
          initialisedBy: "font-upload",
          buttons: {
            main: {
              label: "{% trans "Done" %}",
              className: "btn-primary btn-bb-main",
              callback: function () {
                fontsTable.ajax.reload();
                XiboDialogClose();
              }
            }
          },
          templateOptions: {
            includeTagsInput: false,
            trans: {
              addFiles: "{% trans "Add files" %}",
              startUpload: "{% trans "Start upload" %}",
              cancelUpload: "{% trans "Cancel upload" %}"
            },
            upload: {
              maxSize: {{ libraryUpload.maxSize }},
              maxSizeMessage: "{{ libraryUpload.maxSizeMessage }}",
              validExt: "{{ validExt }}"
            },
          }
        });
      });
    </script>
{% endblock %}
