{#
/**
 * 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 "form-base.twig" %}
{% import "forms.twig" as forms %}
{% import "inline.twig" as inline %}

{% block formTitle %}
    {% set campaignName = campaign.campaign %}
    {% trans %}Edit Campaign "{{ campaignName }}"{% endtrans %}
{% endblock %}

{% block formButtons %}
    {% trans "Cancel" %}, XiboDialogClose()
    {% trans "Save" %}, campaignFormSubmit($('#campaignEditForm'))
{% endblock %}

{% block callBack %}campaignAssignLayoutsFormOpen{% endblock %}

{% block formHtml %}
    <div class="row">
        <div class="col-md-12">
            <ul class="nav nav-tabs" role="tablist">
                <li class="nav-item"><a class="nav-link active" href="#tab-general" role="tab" data-toggle="tab"><span>{% trans "General" %}</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#tab-refs" role="tab" data-toggle="tab"><span>{% trans "Reference" %}</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#tab-layouts" role="tab" data-toggle="tab"><span>{% trans "Layouts" %}</span></a></li>
            </ul>
            <form id="campaignEditForm" class="XiboForm form-horizontal" method="put"
                  action="{{ url_for("campaign.edit", {"id": campaign.campaignId}) }}"
                  data-gettag="{{ url_for("tag.getByName") }}">
                <div class="tab-content">
                    <div class="tab-pane active" id="tab-general">
                        {% if currentUser.featureEnabled('folder.view') %}
                        <div class="form-group row">
                            <label class="col-sm-2 control-label">{% trans "Current Folder" %}</label>
                            <div class="col-sm-10" style="padding-top: 7px">
                                <span id="originalFormFolder"></span>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label class="col-sm-2 control-label">{% trans "Move to Selected Folder:" %}</label>
                            <div class="col-sm-10">
                                <button type="button" class="btn btn-info" id="select-folder-button" data-toggle="modal" data-target="#folder-tree-form-modal">{% trans "Select Folder" %}</button>
                                <span id="selectedFormFolder"></span>
                            </div>
                        </div>
                        {{ forms.hidden('folderId', campaign.folderId) }}
                        {% endif %}

                        {% set title %}{% trans "Name" %}{% endset %}
                        {% set helpText %}{% trans "The Name for this Campaign" %}{% endset %}
                        {{ forms.input("name", title, campaign.campaign, helpText) }}

                        {% if currentUser.featureEnabled("tag.tagging") %}
                            {% set title %}{% trans "Tags" %}{% endset %}
                            {% set helpText %}{% trans "Tags for this Campaign - Comma separated string of Tags or Tag|Value format. If you choose a Tag that has associated values, they will be shown for selection below." %}{% endset %}
                            {{ forms.inputWithTags("tags", title, campaign.getTagString(), helpText, 'tags-with-value') }}

                            <p id="loadingValues" style="margin-left: 17%"></p>

                            {% set title %}{% trans "Tag value" %}{% endset %}
                            {{ forms.dropdown("tagValue", "single", title, "", options, "key", "value") }}

                            <div id="tagValueContainer">
                                {% set title %}{% trans "Tag value" %}{% endset %}
                                {% set helpText %}{% trans "Please provide the value for this Tag and confirm by pressing enter on your keyboard." %}{% endset %}
                                {{ forms.input("tagValueInput", title, "", helpText) }}
                            </div>

                            <div id="tagValueRequired" class="alert alert-info">
                                <p>{% trans "This tag requires a set value, please select one from the Tag value dropdown or provide Tag value in the dedicated field." %}</p>
                            </div>
                        {% endif %}

                        {% set title %}{% trans "Enable cycle based playback" %}{% endset %}
                        {% set helpText %}{% trans "When cycle based playback is enabled only 1 Layout from this Campaign will be played each time it is in a Schedule loop. The same Layout will be shown until the 'Play count' is achieved." %}{% endset %}
                        {{ forms.checkbox("cyclePlaybackEnabled", title, campaign.cyclePlaybackEnabled, helpText, "campaign-type-list", null, false, "input-cycle-playback-enabled") }}

                        {% set title %}{% trans "Play count" %}{% endset %}
                        {% set helpText %}{% trans "In cycle based playback, how many plays should each Layout have before moving on?" %}{% endset %}
                        {{ forms.number("playCount", title, campaign.playCount, helpText, "cycle-based-playback campaign-type-list") }}

                        {% set title %}{% trans "List play order" %}{% endset %}
                        {% set helpText %}{% trans "When this campaign is scheduled alongside another campaign with the same display order, how should the layouts in both campaigns be ordered?" %}{% endset %}
                        {% set options = [
                            { id: "round", name: "Round-robin"|trans },
                            { id: "block", name: "Block"|trans },
                        ] %}
                        {{ forms.dropdown("listPlayOrder", "single", title, campaign.listPlayOrder, options, "id", "name", helpText, "campaign-type-list no-cycle-based-playback") }}
                    </div>
                    <div class="tab-pane" id="tab-refs">
                        {{ forms.message("Add reference fields if needed"|trans) }}

                        {% set title %}{% trans "Reference 1" %}{% endset %}
                        {{ forms.input("ref1", title, campaign.ref1, null) }}

                        {% set title %}{% trans "Reference 2" %}{% endset %}
                        {{ forms.input("ref2", title, campaign.ref2, null) }}

                        {% set title %}{% trans "Reference 3" %}{% endset %}
                        {{ forms.input("ref3", title, campaign.ref3, null) }}

                        {% set title %}{% trans "Reference 4" %}{% endset %}
                        {{ forms.input("ref4", title, campaign.ref4, null) }}

                        {% set title %}{% trans "Reference 5" %}{% endset %}
                        {{ forms.input("ref5", title, campaign.ref5, null) }}
                    </div>
                    <div class="tab-pane" id="tab-layouts">
                        <div id="assignLayouts"></div>
                        {{ forms.hidden("manageLayouts", 0) }}

                        <div class="row">
                            <div class="col-md-12 card p-3 mb-3 bg-light">
                                <div id="LayoutAssign" class="card p-3 mb-3 bg-light" data-url="{{ url_for("campaign.assign.layout", {id: campaign.campaignId}) }}">
                                    <div>
                                        <ul id="LayoutAssignSortable" data-layouts="{{layouts|json_encode()}}"></ul>
                                    </div>
                                </div>

                                <div class="XiboGrid" id="{{ random() }}" data-grid-name="layoutAssignView">
                                    <div class="layoutAssignFilterOptions XiboFilter">
                                        <div class="form-inline">
                                            {% set title %}{% trans "ID" %}{% endset %}
                                            {{ inline.number("campaignId", title) }}

                                            {% set title %}{% trans "Name" %}{% endset %}
                                            {{ inline.input("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 %}

                                            {% if currentUser.featureEnabled("folder.view") %}
                                                {% set title %}{% trans "Folder Filter" %}{% endset %}
                                                {% set attributes = [
                                                    { name: "data-width", value: "100%" },
                                                    { name: "data-search-url", value: url_for("folders.search") },
                                                    { name: "data-filter-options", value: '{"gridView":1}' },
                                                    { name: "data-search-term", value: "folderName" },
                                                    { name: "data-id-property", value: "folderId" },
                                                    { name: "data-text-property", value: "text" },
                                                    { name: "data-initial-key", value: "folderId" },
                                                    { name: "data-allow-clear", value: "true" },
                                                    { name: "data-placeholder--id", value: null },
                                                    { name: "data-placeholder--value", value: "" }
                                                ] %}
                                                {{ inline.dropdown("folderId", "single", title, "", null, "", "", 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 "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") }}
                                        </div>
                                    </div>

                                    <div class="XiboData card pt-3">
                                        <table id="layoutAssignments" class="table table-striped"
                                               style="width:100%"
                                               data-state-preference-name="campaignLayoutAssignGrid">
                                            <thead>
                                            <tr>
                                                <th>{% trans "ID" %}</th>
                                                <th>{% trans "Name" %}</th>
                                                <th>{% trans "Status" %}</th>
                                                <th></th>
                                            </tr>
                                            </thead>
                                            <tbody>

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