{#
/**
 * Copyright (C) 2023 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - https://xibosignage.com
 *
 * 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 %}

{% block formTitle %}
    {% trans "Edit Application" %}
{% endblock %}

{% block formButtons %}
    {% trans "Cancel" %}, XiboDialogClose()
    {% trans "Save" %}, $("#applicationFormSubmit").submit()
{% endblock %}

{% block callBack %}copyFromSecretInput{% 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="#general" role="tab" data-toggle="tab"><span>{% trans "General" %}</span></a></li>
                <li class="nav-item tabForAuthCode"><a class="nav-link" href="#advanced" role="tab" data-toggle="tab"><span>{% trans "Advanced" %}</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#scopes" role="tab" data-toggle="tab"><span>{% trans "Sharing" %}</span></a></li>
            </ul>
            <form id="applicationFormSubmit" class="XiboForm form-horizontal" method="put" action="{{ url_for("application.edit", {id: client.key}) }}">
                <div class="tab-content">
                    <div class="tab-pane active" id="general">
                        {% set title %}{% trans "Application Name" %}{% endset %}
                        {{ forms.input("name", title, client.name) }}

                        {% set title %}{% trans "Client Id" %}{% endset %}
                        {{ forms.disabled("clientId", title, client.key, "", "", "disabled") }}

                        {% set title %}{% trans "Client Secret" %}{% endset %}
                        {% set buttonTitle %}{% trans "Copy to Clipboard" %}{% endset %}
                        {{ forms.inputWithButton("clientSecret", title, client.secret, "", "", "readonly='readonly'", "", "copy-button", buttonTitle, "button", "") }}
                                                    
                        {% set title %}{% trans "Reset Secret?" %}{% endset %}
                        {% set helpText %}{% trans "Reset your client secret to prevent access from any existing application." %}{% endset %}
                        {{ forms.checkbox("resetKeys", title, 0, helpText) }}

                        {% set message %}{% trans "Selecting only one of the Authorisation Code or Client Credentials grants improves security by allowing us to revoke access tokens more effectively." %}{% endset %}
                        {{ forms.message(message, "alert alert-info") }}

                        {% set title %}{% trans "Authorization Code?" %}{% endset %}
                        {% set helpText %}{% trans "Allow the Authorization Code Grant for this Client?" %}{% endset %}
                        {{ forms.checkbox("authCode", title, client.authCode, helpText) }}

                        {% set title %}{% trans "Client Credentials?" %}{% endset %}
                        {% set helpText %}{% trans "Allow the Client Credentials Grant for this Client?" %}{% endset %}
                        {{ forms.checkbox("clientCredentials", title, client.clientCredentials, helpText) }}

                        {% set title %}{% trans "Is Confidential?" %}{% endset %}
                        {% set helpText %}{% trans "Can this Application keep a secret?" %}{% endset %}
                        {{ forms.checkbox("isConfidential", title, client.isConfidential, helpText) }}

                        {% set title %}{% trans "New Redirect URI" %}{% endset %}
                        {% set helpText %}{% trans "White listed redirect URI's that will be allowed, only application for Authorization Code Grants" %}{% endset %}
                        {{ forms.input("redirectUri[]", title, "", helpText) }}

                        <div class="form-group row">
                            <span class="control-label col-sm-2">{{ "Existing Redirect URI"|trans }}:</span>
                        </div>

                        {% for url in client.redirectUris %}
                            {{ forms.input("redirectUri[]", "", url.redirectUri) }}
                        {% endfor %}
                    </div>
                    <div class="tab-pane" id="advanced">
                        {% set message %}{% trans "Below information will be displayed for User on Application authorization page" %}{% endset %}
                        {{ forms.message(message, "alert alert-info") }}

                        {% set title %}{% trans "Application Description" %}{% endset %}
                        {{ forms.textarea("description", title, client.description, title) }}

                        {% set title %}{% trans "Logo" %}{% endset %}
                        {% set helpText %}{% trans "Url pointing to the logo" %}{% endset %}
                        {{ forms.input("logo", title, client.logo, helpText) }}

                        {% set title %}{% trans "Cover Image" %}{% endset %}
                        {% set helpText %}{% trans "Url pointing to the Cover Image" %}{% endset %}
                        {{ forms.input("coverImage", title, client.coverImage, helpText) }}

                        {% set title %}{% trans "Company Name" %}{% endset %}
                        {{ forms.input("companyName", title, client.companyName) }}

                        {% set title %}{% trans "Terms URL" %}{% endset %}
                        {% set helpText %}{% trans "Url pointing to the terms for this Application" %}{% endset %}
                        {{ forms.input("termsUrl", title, client.termsUrl, helpText) }}

                        {% set title %}{% trans "Privacy Url" %}{% endset %}
                        {% set helpText %}{% trans "Url pointing to the privacy policy for this Application" %}{% endset %}
                        {{ forms.input("privacyUrl", title, client.privacyUrl, helpText) }}
                    </div>
                    <div class="tab-pane" id="scopes">
                        {% set message %}{% trans "Select sharing to grant to this application (scopes)." %}{% endset %}
                        {{ forms.message(message, 'alert alert-info') }}
                        {% set message2 %}{% trans "Scopes grant the Application access to specific routes, all GET,POST and PUT calls for the selected scopes, will be available to use by this Application." %}{% endset %}
                        {{ forms.message(message2, 'alert alert-info') }}
                        {% set message3 %}{% trans "The delete scopes are separate, without these Application will not have access to delete any existing content" %}{% endset %}
                        {{ forms.message(message3, 'alert alert-info') }}

                        <div class="form-group row">
                            <span class="control-label col-sm-2">{{ "Scopes"|trans }}:</span>
                        </div>

                        {% for scope in scopes %}
                            {% set title %}{{ scope.description }}{% endset %}
                            {% set id %}scope_{{ scope.id }}{% endset %}
                            {{ forms.checkbox(id, title, scope.getUnmatchedProperty('selected')) }}
                        {% endfor %}

                        {% set title %}{% trans "Owner" %}{% endset %}
                        {% set helpText %}{% trans "Set the owner of this Application. Leave empty to keep the current owner. If you are not an admin you will not be able to reverse this action" %}{% endset %}
                        {% set attributes = [
                            { name: "data-width", value: "100%" },
                            { 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-anchor-element", value: "#applicationFormSubmit"}
                        ] %}
                        {{ forms.dropdown("userId", "single", title, "", null, "userId", "userName", helpText, "pagedSelect", "", "", "", attributes) }}
                    </div>
                </div>
            </form>
        </div>
    </div>
{% endblock %}