{#
SPDX-FileCopyrightText: 2026 Martin Kirchgessner

SPDX-License-Identifier: AGPL-3.0-or-later
#}

{% extends 'layout.jinja' %}
{% set title = "Edit user account" if user else "Create user account" %}
{% block title %}{{ title }}{% endblock title %}

{% from 'macros.jinja' import password_field %}
{% from 'users/macros.jinja' import role_input %}
{% block content %}
<section class="content">
<div class="container">
<h1 aria-hidden="true" id="page_title">
    {{ title }}
</h1>

<form aria-labelledby="page_title">
    <div class="field is-horizontal">
        <div class="field-label">
            <label for="username" class="label">Username</label>
        </div>
        <div class="field-body">
            <div class="field">
                <div class="control">
                    <input type="text" name="username" id="username" class="input" value="{{ user.username if user else '' }}" required>
                </div>
            </div>
        </div>
    </div>

    <div class="field is-horizontal">
        <div class="field-label">
            <label for="notes" class="label">Notes</label>
        </div>
        <div class="field-body">
            <div class="field">
                <div class="control">
                    <textarea name="notes" id="notes" class="textarea">{{ user.notes if user else '' }}</textarea>
                </div>
            </div>
        </div>
    </div>

    {{ password_field("Change password" if user else "Password") }}

    <fieldset>
        <h2>Roles</h2>
        {{ role_input(user, 'can_admin', "Can do everything, in perticular edit settings and user accounts.") }}
        {{ role_input(user, 'can_stream', "Can takeover programs by sending a live stream. The username and password must be provided by the steaming application.") }}
        {{ role_input(user, 'can_live', "Enables actions on the &quot;Live&quot; page: skip the current sound, change current metadata.") }}
        {{ role_input(user, 'can_carts', "Can edit carts and the sounds they contain") }}
        {{ role_input(user, 'can_autodj', "Can edit music library and auto-DJ schedule") }}
        {{ role_input(user, 'can_not_audio', "When the radio outputs to a sound card and you want to use the interface on the Radiotomate server, assign this role to disable players in the interface. Player will be replaced by a 🔇 so browsing the interface will not interfere with the sound stream.") }}
    </fieldset>

    {% set submitValue = "Save" if user else "Add" %}
    <div class="field is-horizontal">
        <div class="field-label"></div>
        <div class="field-body">
            <div class="field is-grouped">
                <div class="control">
                    <button
                        type="submit" class="button is-primary"
                        {% if user %}
                            hx-put="{{ url_for('users.edit', user_id=user.id)}}"
                        {% else %}
                            hx-post="{{ url_for('users.add')}}"
                        {% endif %}
                        aria-label="{{ submitValue }}"
                    >
                        {{ submitValue }}
                    </button>
                </div>
                <div class="control">
                    <a
                        class="button is-danger"
                        href="{{ url_for('users.index') }}"
                        aria-label="Cancel"
                        >
                        Cancel
                    </a>
                </div>
            </div>
        </div>
    </div>
</form>
</section>
</div>
{% endblock content %}
