{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

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

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        {% if currentUser.featureEnabled("layout.add") %}
            <button class="btn btn-success layout-add-button"
                    title="{% trans "Add a new Layout and jump to the layout editor." %}"
                    href="{{ url_for("layout.add") }}">
                <i class="fa fa-plus-circle" aria-hidden="true"></i> {% trans "Add Layout" %}
            </button>
            <button class="btn btn-info" id="layoutUploadForm" title="{% trans "Import a Layout from a ZIP file." %}" href="#"> <i class="fa fa-cloud-download" aria-hidden="true"></i> {% trans "Import" %}</button>
        {% endif %}
        <button class="btn btn-primary" id="refreshGrid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
{% endblock %}

{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Layouts" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-type="layout" data-grid-name="layoutView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <ul class="nav nav-tabs" role="tablist">
                            <li class="nav-item"><a class="nav-link active" href="#general-filter" role="tab" data-toggle="tab" aria-selected="true"><span>{% trans "General" %}</span></a></li>
                            <li class="nav-item"><a class="nav-link" href="#advanced-filter" role="tab" data-toggle="tab" aria-selected="false"><span>{% trans "Advanced" %}</span></a></li>
                        </ul>
                        <form class="form-inline d-block">
                            <div class="tab-content">
                                <div class="tab-pane active" id="general-filter" role="tabpanel">
                                    {% set title %}{% trans "ID" %}{% endset %}
                                    {{ inline.number("campaignId", title) }}

                                    {% set title %}{% trans "Name" %}{% endset %}
                                    {{ inline.inputNameGrid('layout', title) }}

                                    {% if currentUser.featureEnabled("tag.tagging") %}
                                        {% set title %}{% trans "Tags" %}{% endset %}
                                        {% set exactTagTitle %}{% trans "Exact match?" %}{% endset %}
                                        {% set logicalOperatorTitle %}{% trans "When filtering by multiple Tags, which logical operator should be used?" %}{% endset %}
                                        {% set helpText %}{% trans "A comma separated list of tags to filter by. Enter a tag|tag value to filter tags with values. Enter --no-tag to filter all items without tags. Enter - before a tag or tag value to exclude from results." %}{% endset %}
                                        {{ inline.inputWithTags("tags", title, null, helpText, null, null, null, "exactTags", exactTagTitle, logicalOperatorTitle) }}
                                    {% endif %}

                                    {% set title %}{% trans "Code" %}{% endset %}
                                    {{ inline.input('codeLike', title) }}

                                    {% if currentUser.featureEnabled("displaygroup.view") %}
                                        {% set title %}{% trans "Display Group" %}{% endset %}
                                        {% set helpText %}{% trans "Show Layouts active on the selected Display / Display Group" %}{% endset %}
                                        {% set attributes = [
                                            { name: "data-width", value: "200px" },
                                            { name: "data-allow-clear", value: "true" },
                                            { name: "data-placeholder--id", value: null },
                                            { name: "data-placeholder--value", value: "" },
                                            { name: "data-search-url", value:  url_for("displayGroup.search") },
                                            { name: "data-filter-options", value: '{"isDisplaySpecific":-1}' },
                                            { name: "data-search-term", value: "displayGroup" },
                                            { name: "data-id-property", value: "displayGroupId" },
                                            { name: "data-text-property", value: "displayGroup" },
                                            { name: "data-initial-key", value: "displayGroupId" },
                                        ] %}
                                        {{ inline.dropdown("activeDisplayGroupId", "single", title, "", null, "displayGroupId", "displayGroup", helpText, "pagedSelect", "", "", "", attributes) }}
                                    {% endif %}

                                    {% set title %}{% trans "Owner" %}{% endset %}
                                    {% set helpText %}{% trans "Show items owned by the selected User." %}{% endset %}
                                    {% set attributes = [
                                        { name: "data-width", value: "200px" },
                                        { name: "data-allow-clear", value: "true" },
                                        { name: "data-placeholder--id", value: null },
                                        { name: "data-placeholder--value", value: "" },
                                        { name: "data-search-url", value: url_for("user.search") },
                                        { name: "data-search-term", value: "userName" },
                                        { name: "data-search-term-tags", value: "tags" },
                                        { name: "data-id-property", value: "userId" },
                                        { name: "data-text-property", value: "userName" },
                                        { name: "data-initial-key", value: "userId" },
                                    ] %}
                                    {{ inline.dropdown("userId", "single", title, "", null, "userId", "userName", helpText, "pagedSelect", "", "", "", attributes) }}

                                    {% set title %}{% trans "Owner User Group" %}{% endset %}
                                    {% set helpText %}{% trans "Show items owned by users in the selected User Group." %}{% endset %}
                                    {% set attributes = [
                                        { name: "data-width", value: "200px" },
                                        { name: "data-allow-clear", value: "true" },
                                        { name: "data-placeholder--id", value: null },
                                        { name: "data-placeholder--value", value: "" },
                                        { name: "data-search-url", value: url_for("group.search") },
                                        { name: "data-search-term", value: "group" },
                                        { name: "data-id-property", value: "groupId" },
                                        { name: "data-text-property", value: "group" },
                                        { name: "data-initial-key", value: "userGroupId" },
                                    ] %}
                                    {{ inline.dropdown("ownerUserGroupId", "single", title, "", null, "groupId", "group", helpText, "pagedSelect", "", "", "", attributes) }}

                                    {% set title %}{% trans "Orientation" %}{% endset %}
                                    {% set option1 = "All"|trans %}
                                    {% set option2 = "Landscape"|trans %}
                                    {% set option3 = "Portrait"|trans %}
                                    {% set values = [{id: '', value: option1}, {id: 'landscape', value: option2}, {id: 'portrait', value: option3}] %}
                                    {{ inline.dropdown("orientation", "single", title, '', values, "id", "value") }}

                                    {{ inline.hidden("folderId") }}
                                </div>
                                <div class="tab-pane" id="advanced-filter" role="tabpanel">
                                    {% set title %}{% trans "Retired" %}{% endset %}
                                    {% set option1 = "No"|trans %}
                                    {% set option2 = "Yes"|trans %}
                                    {% set values = [{id: 0, value: option1}, {id: 1, value: option2}] %}
                                    {{ inline.dropdown("retired", "single", title, 0, values, "id", "value") }}

                                    {% set title %}{% trans "Show" %}{% endset %}
                                    {% set option1 = "All"|trans %}
                                    {% set option2 = "Only Used"|trans %}
                                    {% set option3 = "Only Unused"|trans %}
                                    {% set values = [{id: 1, value: option1}, {id: 2, value: option2}, {id: 3, value: option3}] %}
                                    {{ inline.dropdown("layoutStatusId", "single", title, 1, values, "id", "value") }}

                                    {% set title %}{% trans "Description" %}{% endset %}
                                    {% set option1 = "All"|trans %}
                                    {% set option2 = "1st line"|trans %}
                                    {% set option3 = "Widget List"|trans %}
                                    {% set values = [{id: 1, value: option1}, {id: 2, value: option2}, {id: 3, value: option3}] %}
                                    {{ inline.dropdown("showDescriptionId", "single", title, 2, values, "id", "value") }}

                                    {% if currentUser.featureEnabled("library.view") %}
                                        {% set title %}{% trans "Media" %}{% endset %}
                                        {{ inline.input("mediaLike", title) }}
                                    {% endif %}

                                    {% set title %}{% trans "Layout ID" %}{% endset %}
                                    {{ inline.number("layoutId", title) }}

                                    {% set title %}{% trans "Modified Since" %}{% endset %}
                                    {{ inline.date("modifiedSinceDt", title) }}
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="grid-with-folders-container">
                    <div class="grid-folder-tree-container p-3" id="grid-folder-filter">
                        <input id="jstree-search" class="form-control" type="text" placeholder="{% trans "Search" %}">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" id="folder-tree-clear-selection-button">
                            <label class="form-check-label" for="folder-tree-clear-selection-button" title="{% trans "Search in all folders" %}">{% trans "All Folders" %}</label>
                        </div>
                        <div class="folder-search-no-results d-none">
                            <p>{% trans 'No Folders matching the search term' %}</p>
                        </div>
                        <div id="container-folder-tree"></div>
                    </div>

                    <div class="folder-controller d-none">
                        <button type="button" id="folder-tree-select-folder-button" class="btn btn-outline-secondary" title="{% trans "Open / Close Folder Search options" %}"><i class="fas fa-folder fa-1x"></i></button>
                        <div id="breadcrumbs" class="mt-2 pl-2"></div>
                    </div>

                    <div id="datatable-container">
                        <div class="XiboData card py-3">
                            <table id="layouts" class="table table-striped responsive nowrap" data-content-type="layout" data-content-id-name="layoutId" data-state-preference-name="layoutGrid" style="width: 100%;">
                                <thead>
                                    <tr>
                                        <th>{% trans "ID" %}</th>
                                        <th>{% trans "Name" %}</th>
                                        <th>{% trans "Status" %}</th>
                                        <th>{% trans "Description" %}</th>
                                        <th>{% trans "Duration" %}</th>
                                        {% if currentUser.featureEnabled("tag.tagging") %}<th>{% trans "Tags" %}</th>{% endif %}
                                        <th>{% trans "Orientation" %}</th>
                                        <th>{% trans "Thumbnail" %}</th>
                                        <th>{% trans "Owner" %}</th>
                                        <th>{% trans "Sharing" %}</th>
                                        <th>{% trans "Valid?" %}</th>
                                        <th>{% trans "Stats?" %}</th>
                                        <th>{% trans "Created" %}</th>
                                        <th>{% trans "Modified" %}</th>
                                        <th>{% trans "Layout ID" %}</th>
                                        <th>{% trans "Code" %}</th>
                                        <th class="rowMenu"></th>
                                    </tr>
                                </thead>
                                <tbody>

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

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
        var table;
        $(document).ready(function() {
            {% if not currentUser.featureEnabled("folder.view") %}
            disableFolders();
            {% endif %}

            table = $("#layouts").DataTable({
                language: dataTablesLanguage,
                lengthMenu: [10, 25, 50, 100, 250, 500],
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                stateDuration: 0,
                responsive: true,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                dataType: 'json',
                order: [[1, "asc"]],
                ajax: {
                    url: "{{ url_for("layout.search") }}",
                    data: function (d) {
                        $.extend(d, $("#layouts").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                columns: [
                    {"data": "campaignId", responsivePriority: 1},
                    {
                        "data": "layout",
                        responsivePriority: 2,
                        "render": dataTableSpacingPreformatted
                    },
                    {
                        "name": "publishedStatus",
                        responsivePriority: 2,
                        "data": function (data, type) {
                            if (data.publishedDate != null) {
                                var now = moment();
                                var published = moment(data.publishedDate);
                                var differenceMinutes = published.diff(now, 'minutes');
                                var momentDifference = moment(now).to(published);

                                if (differenceMinutes < -5) {
                                    return data.publishedStatus.concat(" - ", translations.publishedStatusFailed);
                                } else {
                                    return data.publishedStatus.concat(" - ", translations.publishedStatusFuture + " " + momentDifference);
                                }
                            } else {
                                return data.publishedStatus;
                            }

                        }
                    },
                    {
                        "name": "description",
                        "data": null,
                        responsivePriority: 10,
                        "render": {"_": "description", "display": "descriptionFormatted", "sort": "description"}
                    },
                    {
                        "name": "duration",
                        responsivePriority: 3,
                        "data": function (data, type) {
                            if (type != "display")
                                return data.duration;

                            return dataTableTimeFromSeconds(data.duration, type);
                        }
                    },
                    {% if currentUser.featureEnabled("tag.tagging") %}{
                        "sortable": false,
                        "visible": false,
                        responsivePriority: 3,
                        "data": dataTableCreateTags
                    },{% endif %}
                    { data: 'orientation', responsivePriority: 10, visible: false},
                    {
                        responsivePriority: 5,
                        data: 'thumbnail',
                        render: function(data, type, row) {
                          if (type !== 'display') {
                            return row.layoutId;
                          }
                          if (data) {
                            return '<a class="img-replace" data-toggle="lightbox" data-type="image" href="' + data + '">' +
                                '<img class="img-fluid" src="' + data + '" alt="{{ "Thumbnail"|trans }}" />' +
                                '</a>';
                          } else {
                            var addUrl = '{{ url_for("layout.thumbnail.add", {id: ":id"}) }}'.replace(':id', row.layoutId);
                            return '<a class="img-replace generate-layout-thumbnail" data-type="image" href="' + addUrl + '">' +
                                '<img class="img-fluid" src="{{ theme.uri("img/thumbs/placeholder.png") }}" alt="{{ "Add Thumbnail"|trans }}" />' +
                                '</a>';
                          }
                          return '';
                        },
                        sortable: false
                    },
                    {"data": "owner", responsivePriority: 4},
                    {
                        "data": "groupsWithPermissions",
                        responsivePriority: 4,
                        "render": dataTableCreatePermissions
                    },
                    {
                        "name": "status",
                        responsivePriority: 3,
                        "data": function (data, type) {
                            if (type != "display")
                                return data.status;

                            var icon = "";
                            if (data.status == 1)
                                icon = "fa-check";
                            else if (data.status == 2)
                                icon = "fa-exclamation";
                            else if (data.status == 3)
                                icon = "fa-cogs";
                            else
                                icon = "fa-times";

                            return '<span class="fa ' + icon + '" title="' + (data.statusDescription) + ((data.statusMessage == null) ? "" : " - " + (data.statusMessage)) + '"></span>';
                        }
                    },
                    {
                        "name": "enableStat",
                        responsivePriority: 4,
                        "data": function (data) {

                            var icon = "";
                            if (data.enableStat == 1)
                                icon = "fa-check";
                            else
                                icon = "fa-times";

                            return '<span class="fa ' + icon + '" title="' + (data.enableStatDescription) + '"></span>';
                        }
                    },
                    {
                        "data": "createdDt",
                        responsivePriority: 6,
                        "render": dataTableDateFromIso,
                        "visible": false
                    },
                    {
                        data: "modifiedDt",
                        responsivePriority: 6,
                        render: dataTableDateFromIso,
                        visible: true
                    },
                    {
                      data: "layoutId",
                      visible: false,
                      responsivePriority: 4
                    },
                    {"data": "code", "visible":false, responsivePriority: 4},
                    {
                        "orderable": false,
                        responsivePriority: 1,
                        "data": dataTableButtonsColumn
                    }
                ]
            });

            table.on('draw', dataTableDraw);
            table.on('draw', { form: $("#layouts").closest(".XiboGrid").find(".FilterDiv form") }, dataTableCreateTagEvents);
            table.on('draw', function(e, settings) {
              $('#' + e.target.id + ' .generate-layout-thumbnail').on('click', function(e) {
                e.preventDefault();
                var $anchor = $(this);
                $.ajax({
                  url: $anchor.attr('href'),
                  method: 'POST',
                  success: function() {
                    $anchor.find('img').attr('src', $anchor.attr('href'));
                    $anchor.removeClass('generate-layout-thumbnail').attr('data-toggle', 'lightbox');
                  }
                });
              });
            });
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#layouts_wrapper').find('.dataTables_buttons'));

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

            // Bind to the layout add button
            $('button.layout-add-button').on('click', function() {
              let currentWorkingFolderId =
                $("#layouts")
                  .closest(".XiboGrid")
                  .find(".FilterDiv form")
                  .find('#folderId').val()
                // Submit the URL provided as a POST request.
                $.ajax({
                    type: 'POST',
                    url: $(this).attr('href'),
                    cache: false,
                    data : {folderId : currentWorkingFolderId},
                    dataType: 'json',
                    success: function(response, textStatus, error) {
                        if (response.success && response.id) {
                            XiboRedirect('{{ url_for("layout.designer", {id: ':id'}) }}'.replace(':id', response.id));
                        } else {
                            if (response.login) {
                                LoginBox(response.message);
                            } else {
                                SystemMessage(response.message ?? '{{ "Unknown Error"|trans }}', false);
                            }
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        SystemMessage(xhr.responseText, false);
                    },
                });
            });
        });

        $("#layoutUploadForm").click(function(e) {
            e.preventDefault();
            var currentWorkingFolderId = $('#folderId').val();

            // Open the upload dialog with our options.
            openUploadForm({
                url: "{{ url_for("layout.import") }}",
                title: "{{ "Upload Layout"|trans }}",
                videoImageCovers: false,
                buttons: {
                    main: {
                        label: "{{ "Done"|trans }}",
                        className: "btn-primary btn-bb-main",
                        callback: function () {
                            table.ajax.reload();
                            XiboDialogClose();
                        }
                    }
                },
                templateOptions: {
                    layoutImport: true,
                    updateInAllChecked: {% if settings.LIBRARY_MEDIA_UPDATEINALL_CHECKB == 1 %}true{% else %}false{% endif %},
                    deleteOldRevisionsChecked: {% if settings.LIBRARY_MEDIA_DELETEOLDVER_CHECKB == 1 %}true{% else %}false{% endif %},
                    trans: {
                        addFiles: "{{ "Add Layout Export ZIP Files"|trans }}",
                        startUpload: "{{ "Start Import"|trans }}",
                        cancelUpload: "{{ "Cancel Import"|trans }}",
                        replaceExistingMediaMessage: "{{ "Replace Existing Media?"|trans }}",
                        importTagsMessage: "{{ "Import Tags?"|trans }}",
                        useExistingDataSetsMessage: "{{ "Use existing DataSets matched by name?"|trans }}",
                        dataSetDataMessage: "{{ "Import DataSet Data?"|trans }}",
                        fallbackMessage: "{{ "Import Widget Fallback Data?"|trans }}",
                        selectFolder: "{{ "Select Folder"|trans }}",
                        selectFolderTitle: "{{ "Change Current Folder location"|trans }}",
                        selectedFolder: "{{ "Current Folder"|trans }}:",
                        selectedFolderTitle: "{{ "Upload files to this Folder"|trans }}"
                    },
                    upload: {
                        maxSize: {{ libraryUpload.maxSize }},
                        maxSizeMessage: "{{ libraryUpload.maxSizeMessage }}",
                        validExt: "zip"
                    },
                    currentWorkingFolderId: currentWorkingFolderId,
                    folderSelector: true
                },
                formOpenedEvent: function () {
                    // Configure the active behaviour of the checkboxes
                    $("#useExistingDataSets").on("click", function () {
                        $("#importDataSetData").prop("disabled", ($(this).is(":checked")));
                    });
                },
                uploadDoneEvent: function (data) {
                    XiboDialogClose();
                    table.ajax.reload();
                }
            });
        });

        function layoutExportFormSubmit() {
            var $form = $("#layoutExportForm");
            window.location = $form.attr("action") + "?" + $form.serialize();

            setTimeout(function() {
                XiboDialogClose();
            }, 1000);
        }

        function assignLayoutToCampaignFormSubmit() {
            var form = $("#layoutAssignCampaignForm");

            var url = form.prop("action").replace(":id", form.find("#campaignId").val());

            $.ajax({
                type: form.attr("method"),
                url: url,
                data: {layoutId: form.data().layoutId},
                cache: false,
                dataType:"json",
                success: XiboSubmitResponse
            });
        }

        function setEnableStatMultiSelectFormOpen(dialog) {
            var $input = $('<input type=checkbox id="enableStat" name="enableStat"> {{ "Enable Stats Collection?"|trans }} </input>');
            var $helpText = $('<span class="help-block">{{ "Check to enable the collection of Proof of Play statistics for the selected items."|trans }}</span>');

            $input.on('change', function() {
                dialog.data().commitData = {enableStat: $(this).val()};
            });

            $(dialog).find('.modal-body').append($input);
            $(dialog).find('.modal-body').append($helpText);
        }

        function layoutPublishFormOpen() {
          // Nothing to do here, but we use the same form on the layout designer and have a callback registered there
        }

        function layoutEditFormSaved() {
          // Nothing to do here.
        }
    </script>
{% endblock %}
