{% requireAdmin %}

{% extends "settings/users/_layout" %}
{% set selectedNavItem = 'settings' %}

{% import "_includes/forms" as forms %}


{% if settings is not defined %}
    {% set settings = craft.app.projectConfig.get('users') ?? [] %}
{% endif %}

{# set defaults #}
{% set settings = {
    photoVolumeUid: null,
    photoSubpath: null,
    requireEmailVerification: true,
    allowPublicRegistration: false,
    defaultGroup: null,
}|merge(settings) %}

{% set allVolumes = craft.app.volumes.getAllVolumes() %}
{% set volumeList = [] %}
{% for volume in allVolumes %}
    {% set volumeList = volumeList|merge([{'value': volume.uid, 'label': volume.name}]) %}
{% endfor %}

{% macro assetLocationInput(volumeOptions, volumeUid, subpath) %}
    {% from "_includes/forms" import select, text %}
    <div class="flex">
        <div>
            {{ select({
                id: 'photoVolumeUid',
                name: 'photoVolumeUid',
                options: volumeOptions,
                value: volumeUid,
            }) }}
        </div>
        <div class="flex-grow">
            {{ text({
                id: 'photoSubpath',
                class: 'ltr',
                name: 'photoSubpath',
                value: subpath,
                placeholder: "path/to/subfolder"|t('app')
            }) }}
        </div>
    </div>
{% endmacro %}

{% from _self import assetLocationInput %}

{% block content %}
    <form id="settings-form" method="post" class="centered" accept-charset="UTF-8" data-saveshortcut>
        <input type="hidden" name="action" value="user-settings/save-user-settings">
        {{ csrfInput() }}

        {% if volumeList %}
            {{ forms.field({
                first: true,
                label: "User Photo Location"|t('app'),
                instructions: "Where do you want to store user photos? Note that the subfolder path can contain variables like <code>{username}</code>."|t('app')
            }, assetLocationInput(volumeList, settings.photoVolumeUid, settings.photoSubpath)) }}
        {% else %}
            {{ forms.field({
                first: true,
                label: "User Photo Volume"|t('app')
            }, '<p class="error">' ~ "No volumes exist yet."|t('app') ~ '</p>') }}
        {% endif %}

        {% if CraftEdition == CraftPro %}
            {{ forms.checkboxField({
                label: "Verify email addresses?"|t('app'),
                instructions: "Should new email addresses be verified before getting saved to user accounts? (This also affects new user registration.)"|t('app'),
                name: 'requireEmailVerification',
                checked: settings.requireEmailVerification,
            }) }}

            {{ forms.checkboxField({
                label: "Allow public registration?"|t('app'),
                name: 'allowPublicRegistration',
                checked: settings.allowPublicRegistration,
                toggle: 'publicRegistrationSettings'
            }) }}

            <div id="publicRegistrationSettings" class="nested-fields{% if not settings.allowPublicRegistration %} hidden{% endif %}">
                {% set groups = [{ label: "None"|t('app'), value: '' }] %}
                {% for group in craft.app.userGroups.getAllGroups() %}
                    {% set groups = groups|merge([{ label: group.name, value: group.uid }]) %}
                {% endfor %}

                {{ forms.selectField({
                    label: "Default User Group"|t('app'),
                    instructions: "Choose a user group that publicly-registered members will be added to by default."|t('app'),
                    name: 'defaultGroup',
                    options: groups,
                    value: settings.defaultGroup
                }) }}
            </div>
        {% endif %}

        <div class="buttons">
            <input type="submit" class="btn submit" value="{{ 'Save'|t('app') }}">
        </div>
    </form>
{% endblock %}
