{% 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 calendarId = form.settings.integrations[handle].calendarId ?? '' %}

<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="{{ calendarId }}">
    <template v-slot="{ get, isEmpty, input, settings, sourceId, loading, refresh, error, errorMessage, getSourceFields, getCollectionFields }">
        <div class="field">
            <div class="heading">
                <label id="sourceId-label" for="sourceId" class="required">{{ 'Calender' | t('formie') }}</label>

                <div class="instructions">
                    <p>{{ 'Select a calendar to map content to. This will reflect the available fields to map to.' | t('formie') }}</p>
                </div>
            </div>

            <div class="input ltr">
                <div class="select">
                    <select :value="sourceId" @input="input('sourceId', $event.target.value)" name="calendarId" required>
                        <option value="">{{ 'Select an option' | t('formie') }}</option>

                        <option v-for="(option, index) in get(settings, 'elements')" :key="index" :value="option.id">
                            ${ option.name }
                        </option>
                    </select>
                </div>
            </div>

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

        <div v-if="error" class="error" style="margin-top: 10px;" v-cloak>
            <span data-icon="alert"></span>
            <span v-html="errorMessage"></span>
        </div>

        {{ forms.elementSelectField({
            label: 'Default Event Author' | t('formie'),
            instructions: 'Select a user to be the default author for the created event. An event must always have an author.' | t('formie'),
            id: 'defaultAuthorId',
            name: 'defaultAuthorId',
            elementType: 'craft\\elements\\User',
            selectionLabel: 'Choose a User' | t('formie'),
            limit: 1,
            required: true,
            elements: integration.getAuthor(form),
        }) }}

        <integration-field-mapping
            label="{{ 'Event Attribute Mapping' | t('formie') }}"
            instructions="{{ 'Choose how your form fields should map to your {label} attributes.' | t('formie', { label: 'Event' }) }}"
            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="{{ 'Event Field Mapping' | t('formie') }}"
            instructions="{{ 'Choose how your form fields should map to your {label} fields.' | t('formie', { label: 'Event' }) }}"
            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>
            {{ forms.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 %}

            {{ forms.lightswitchField({
                label: 'Update Events' | t('formie'),
                instructions: 'Whether this integration should update an existing event if found, or always create a new event. ' | 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(sourceId, {{ integration.getUpdateAttributes() | json_encode }})"
            ></integration-field-mapping>
        </div>
    </template>
</integration-form-settings>
