{#
/**
  * Copyright (C) 2024 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 User" %}
{% endblock %}

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

{% block callBack %}userFormOpen{% 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 "Details" %}</span></a></li>
                {% if currentUser.featureEnabled("folder.userHome") %}
                    <li class="nav-item"><a class="nav-link" href="#home-folder" role="tab" data-toggle="tab"><span>{% trans "Home Folder" %}</span></a></li>
                {% endif %}
                <li class="nav-item"><a class="nav-link" href="#reference" role="tab" data-toggle="tab"><span>{% trans "Reference" %}</span></a></li>
                {% if currentUser.userTypeId == 1 %}
                    <li class="nav-item"><a class="nav-link" href="#notifications" role="tab" data-toggle="tab"><span>{% trans "Notifications" %}</span></a></li>
                {% endif %}
                <li class="nav-item"><a class="nav-link" href="#optionsTab" role="tab" data-toggle="tab"><span>{% trans "Options" %}</span></a></li>
            </ul>
            <form id="userEditForm" class="UserForm form-horizontal" data-user-id="{{ user.userId }}" method="put" action="{{ url_for("user.edit", {id: user.userId}) }}">
                <div class="tab-content">
                    <div class="tab-pane active" id="general">
                        {% set title %}{% trans "Username" %}{% endset %}
                        {% set helpText %}{% trans "The Username of the user." %}{% endset %}
                        {{ forms.input("userName", title, user.userName, helpText, "", "required maxlength='50'") }}

                        {% set title %}{% trans "Email" %}{% endset %}
                        {% set helpText %}{% trans "The Email Address for this user." %}{% endset %}
                        {{ forms.email("email", title, user.email, helpText) }}

                        {% if currentUser.userTypeId == 1 %}
                            {# This is an admin user and can therefore change the users password and two factor setting#}
                            {% set title %}{% trans "New Password" %}{% endset %}
                            {% set helpText %}{% trans "The new Password for this user." %}{% endset %}
                            {{ forms.password("newPassword", title, "", helpText) }}

                            {% set title %}{% trans "Retype New Password" %}{% endset %}
                            {% set helpText %}{% trans "Repeat the new Password for this user." %}{% endset %}
                            {{ forms.password("retypeNewPassword", title, "", helpText) }}

                            {% set title %}{% trans "Reset Two Factor Authentication" %}{% endset %}
                            {% set helpText %}{% trans "Once ticked the two factor authentication will be set to ‘Off’ for this User Profile with any stored secret codes cleared. The User can now set up two factor authentication from the User Profile as before." %}{% endset %}
                            {{ forms.checkbox("disableTwoFactor", title, 0, helpText) }}
                        {% endif %}

                        {% set title %}{% trans "Homepage" %}{% endset %}
                        {% set helpText %}{% trans "Homepage for this user. This is the page they will be taken to when they login." %}{% endset %}
                        <div class="form-group row">
                            <label class="col-sm-2 control-label" for="homePageId">{{ title }}</label>
                            <div class="col-sm-10">
                                <select name="homePageId" class="form-control homepage-select" required data-search-url="{{ url_for("user.homepages.search") }}">
                                    <option value="{{ options.homepage.homepage }}" selected>{{ options.homepage.title }}</option>
                                </select>
                                <span class="help-block">{{ helpText }}</span>
                            </div>
                        </div>

                        {% if currentUser.isSuperAdmin() %}
                            {% set title %}{% trans "User Type" %}{% endset %}
                            {% set helpText %}{% trans "What is this users type?" %}{% endset %}
                            {{ forms.dropdown("userTypeId", "single", title, user.userTypeId, options.userTypes, "userTypeId", "userType", helpText) }}
                        {% endif %}

                        {% set title %}{% trans "Library Quota" %}{% endset %}
                        {% set helpText %}{% trans "The quota that should be applied. Enter 0 for no quota." %}{% endset %}
                        <div class="form-group row">
                            <label class="col-sm-2 control-label" for="libraryQuota">{{ title }}</label>
                            <div class="col-sm-6">
                                <input class="form-control" name="libraryQuota" type="number" id="libraryQuota" min="0" value="{{ user.libraryQuota }}" />
                                <span class="help-block">{{ helpText }}</span>
                            </div>
                            <div class="col-sm-4">
                                <select name="libraryQuotaUnits" class="form-control">
                                    <option value="kb">KiB</option>
                                    <option value="mb">MiB</option>
                                    <option value="gb">GiB</option>
                                </select>
                            </div>
                        </div>

                        {% set title %}{% trans "Retired?" %}{% endset %}
                        {% set helpText %}{% trans "Is this user retired?" %}{% endset %}
                        {{ forms.checkbox("retired", title, user.retired, helpText) }}
                    </div>
                    {% if currentUser.featureEnabled("folder.userHome") %}
                        <div class="tab-pane" id="home-folder">
                        <p>{{ "Set a home folder to use as the default folder for new content."|trans }}</p>
                            {{ forms.hidden("homeFolderId", user.homeFolderId) }}
                            <div id="container-form-folder-tree" class="card card-body bg-light"></div>
                        </div>
                    {% endif %}
                    <div class="tab-pane" id="reference">
                        {% set title %}{% trans "First Name" %}{% endset %}
                        {% set helpText %}{% trans "The User's First Name." %}{% endset %}
                        {{ forms.input("firstName", title, user.firstName, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Last Name" %}{% endset %}
                        {% set helpText %}{% trans "The User's Last Name." %}{% endset %}
                        {{ forms.input("lastName", title, user.lastName, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Phone Number" %}{% endset %}
                        {% set helpText %}{% trans "The User's Phone Number." %}{% endset %}
                        {{ forms.input("phone", title, user.phone, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Reference 1" %}{% endset %}
                        {% set helpText %}{% trans "A reference field for custom user data" %}{% endset %}
                        {{ forms.input("ref1", title, user.ref1, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Reference 2" %}{% endset %}
                        {% set helpText %}{% trans "A reference field for custom user data" %}{% endset %}
                        {{ forms.input("ref2", title, user.ref2, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Reference 3" %}{% endset %}
                        {% set helpText %}{% trans "A reference field for custom user data" %}{% endset %}
                        {{ forms.input("ref3", title, user.ref3, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Reference 4" %}{% endset %}
                        {% set helpText %}{% trans "A reference field for custom user data" %}{% endset %}
                        {{ forms.input("ref4", title, user.ref4, helpText, "", "maxlength='254'") }}

                        {% set title %}{% trans "Reference 5" %}{% endset %}
                        {% set helpText %}{% trans "A reference field for custom user data" %}{% endset %}
                        {{ forms.input("ref5", title, user.ref5, helpText, "", "maxlength='254'") }}
                    </div>
                    {% if currentUser.userTypeId == 1 %}
                        <div class="tab-pane" id="notifications">
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>{{ "Notification Type"|trans }}</th>
                                    <th>
                                        {% trans "Enabled?" %}
                                        <i class="fa fa-info-circle" title="{{ "Check this box to give this User access to this Notification." }}"></i>
                                    </th>
                                    <th>
                                        {% trans "Inherited?" %}
                                        <i class="fa fa-info-circle" title="{{ "If checked this User already has access to this Notification due to the User Groups they belong to." }}"></i>
                                    </th>
                                </tr>
                                </thead>

                                <tbody class="feature-group">
                                    <tr class="feature-row">
                                        <td>
                                            {{ "System"|trans }}<br/>
                                            {{ "Should this User receive Layout notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isSystemNotification" {% if user.isSystemNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('system', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Display"|trans }}<br/>
                                            {{ "Should this User receive Display notifications for Displays they have permission to see?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isDisplayNotification" {% if user.isDisplayNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('display', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "DataSet"|trans }}<br/>
                                            {{ "Should this User receive DataSet notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isDataSetNotification" {% if user.isDataSetNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('dataset', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Layout"|trans }}<br/>
                                            {{ "Should this User receive Layout notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isLayoutNotification" {% if user.isLayoutNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('layout', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Library"|trans }}<br/>
                                            {{ "Should this User receive Library notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isLibraryNotification" {% if user.isLibraryNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('library', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Reports"|trans }}<br/>
                                            {{ "Should this User receive Report notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isReportNotification" {% if user.isReportNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('report', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Schedule"|trans }}<br/>
                                            {{ "Should this User receive Schedule notification emails?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isScheduleNotification" {% if user.isScheduleNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('schedule', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                    <tr class="feature-row">
                                        <td>
                                            {{ "Custom"|trans }}<br/>
                                            {{ "Should this User receive notifications emails for Notifications manually created in CMS?"|trans }}
                                        </td>
                                        <td>
                                            <input type="checkbox" name="isCustomNotification" {% if user.isCustomNotification == 1 %}checked{% endif %}>
                                        </td>
                                        <td>
                                            <input type="checkbox" class="inherit-group" disabled {% if user.isNotificationEnabled('custom', true) %}checked{% endif %}>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    {% endif %}

                    <div class="tab-pane" id="optionsTab">
                        {% set title %}{% trans "Hide navigation?" %}{% endset %}
                        {% set helpText %}{% trans "Should the navigation side bar be hidden for this User?" %}{% endset %}
                        {{ forms.checkbox("hideNavigation", title, user.getOptionValue("hideNavigation", "0"), helpText) }}

                        {% set title %}{% trans "Hide User Guide?" %}{% endset %}
                        {% set helpText %}{% trans "Should this User see the new user guide when they log in? This will be set to hidden if the User has dismissed the guide themselves." %}{% endset %}
                        {{ forms.checkbox("newUserWizard", title, user.newUserWizard, helpText) }}

                        {% set title %}{% trans "Force Password Change" %}{% endset %}
                        {% set helpText %}{% trans "Should this User be forced to change password next time they log in?" %}{% endset %}
                        {{ forms.checkbox("isPasswordChangeRequired", title, user.isPasswordChangeRequired, helpText) }}
                    </div>
                </div>
            </form>
        </div>
    </div>
{% endblock %}