{% import '_includes/forms' as forms %}
{% import 'verbb-base/_macros' as macros %}

{% set handle = integration.handle %}
{% set displayName = integration.displayName() %}
{% set formSettings = integration.getFormSettings().getSettings() %}
{% set activateUser = form.settings.integrations[handle].activateUser ?? false %}
{% set mergeUserGroups = form.settings.integrations[handle].mergeUserGroups ?? false %}
{% set sendActivationEmail = form.settings.integrations[handle].sendActivationEmail ?? false %}
{% set groupUids = form.settings.integrations[handle].groupUids ?? [] %}

<field-select
    label="{{ 'Opt-In Field' | t('formie') }}"
    instructions="{{ 'Choose a field to opt-in to {name}. For example, you might only wish to subscribe users if they provide a value for a field of your choice - commonly, an Agree field.' | t('formie', { name: displayName }) }}"
    id="opt-in-field"
    name="optInField"
    :value="get(form, 'settings.integrations.{{ handle }}.optInField')"
></field-select>

<hr>

<integration-form-settings handle="{{ handle }}" :form-settings="{{ formSettings | json_encode }}" source="user">
    <template v-slot="{ get, isEmpty, input, settings, sourceId, loading, refresh, error, errorMessage, getSourceFields, getCollectionFields }">
        {{ macros.proxyField(fieldVariables, 'checkboxSelectField', {
            label: 'User Group' | t('formie'),
            instructions: 'Select user groups to assign this new user to.' | t('formie'),
            id: 'groupUids',
            name: 'groupUids',
            options: integration.getGroupOptions(),
            values: groupUids,
        }) }}

        {{ macros.proxyField(fieldVariables, 'lightswitchField', {
            label: 'Activate User' | t('formie'),
            instructions: 'Whether to activate the user automatically.' | t('formie'),
            name: 'activateUser',
            on: activateUser,
        }) }}
                                
        {{ macros.proxyField(fieldVariables, 'lightswitchField', {
            label: 'Send Activation Email' | t('formie'),
            instructions: 'Whether to send the activation email for the user automatically.' | t('formie'),
            name: 'sendActivationEmail',
            on: sendActivationEmail,
        }) }}

        <integration-field-mapping
            label="{{ 'User Attribute Mapping' | t('formie') }}"
            instructions="{{ 'Choose how your form fields should map to your {label} attributes.' | t('formie', { label: 'User' }) }}"
            name-label="{{ integration.displayName() }}"
            id="attribute-mapping"
            name="attributeMapping"
            :value="get(form, 'settings.integrations.{{ handle }}.attributeMapping')"
            :rows="{{ integration.getElementAttributes() | json_encode }}"
        ></integration-field-mapping>

        <integration-field-mapping
            label="{{ 'User Field Mapping' | t('formie') }}"
            instructions="{{ 'Choose how your form fields should map to your {label} fields.' | t('formie', { label: 'User' }) }}"
            name-label="{{ integration.displayName() }}"
            id="field-mapping"
            name="fieldMapping"
            :value="get(form, 'settings.integrations.{{ handle }}.fieldMapping')"
            :rows="getSourceFields('elements')"
        ></integration-field-mapping>

        <ul v-if="!isEmpty(get(form, 'settings.integrations.{{ handle }}.errors.fieldMapping'))" class="errors" v-cloak>
            <li v-for="(error, index) in get(form, 'settings.integrations.{{ handle }}.errors.fieldMapping')" :key="index">
                ${ error }
            </li>
        </ul>

        <div style="margin-bottom: 24px;" v-pre>
            {{ macros.proxyField(fieldVariables, 'lightswitchField', {
                label: 'Overwrite Content' | t('formie'),
                instructions: 'Whether to overwrite existing content, even if empty values are provided.' | t('formie'),
                name: 'overwriteValues',
                on: form.settings.integrations[handle].overwriteValues ?? false,
            }) }}

            {% set updateElement = form.settings.integrations[handle].updateElement ?? false %}

            {{ macros.proxyField(fieldVariables, 'lightswitchField', {
                label: 'Update Users' | t('formie'),
                instructions: 'Whether this integration should update an existing user if found, or always create a new user. ' | t('formie'),
                id: 'updateElement',
                name: 'updateElement',
                on: updateElement,
                toggle: 'update-element',
            }) }}
        </div>

        <div id="update-element" class="{% if not updateElement %}hidden{% endif %}" style="margin-bottom: 24px;">
            <integration-field-mapping
                label="{{ 'Update Element Mapping' | t('formie') }}"
                instructions="{{ 'Select the fields you want to use to check for existing elements. Formie will look for existing elements with the attributes chosen and the values provided in the submission.' | t('formie') }}"
                name-label="{{ integration.displayName() }}"
                id="update-element-mapping"
                name="updateElementMapping"
                :value="get(form, 'settings.integrations.{{ handle }}.updateElementMapping')"
                :rows="getCollectionFields('users', {{ integration.getUpdateAttributes() | json_encode }})"
            ></integration-field-mapping>
                                        
            {{ macros.proxyField(fieldVariables, 'lightswitchField', {
                label: 'Merge User Groups' | t('formie'),
                instructions: 'Whether existing user groups should be merged with any set above. Otherwise, any existing user groups will be overwritten.' | t('formie'),
                name: 'mergeUserGroups',
                on: mergeUserGroups,
            }) }}
        </div>

        <div style="margin-bottom: 24px;" v-pre>
            {% set updateSearchIndexes = form.settings.integrations[handle].updateSearchIndexes ?? true %}

            {{ macros.proxyField(fieldVariables, 'lightswitchField', {
                label: 'Update Search Index' | t('formie'),
                instructions: 'Whether this integration should update the search indexes to include content for this element. ' | t('formie'),
                id: 'updateSearchIndexes',
                name: 'updateSearchIndexes',
                on: updateSearchIndexes,
            }) }}
        </div>
    </template>
</integration-form-settings>
