{% 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 mapToPeople = form.settings.integrations[handle].mapToPeople ?? '' %}
{% set mapToLead = form.settings.integrations[handle].mapToLead ?? '' %}
{% set mapToOpportunity = form.settings.integrations[handle].mapToOpportunity ?? '' %}
{% set mapToTask = form.settings.integrations[handle].mapToTask ?? '' %}

<field-select
    label="{{ 'Opt-In Field' | t('formie') }}"
    instructions="{{ 'Choose a field to opt-in to {name}. For example, you might only wish to record user data 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 }}">
    <template v-slot="{ get, isEmpty, input, settings, sourceId, loading, refresh, error, errorMessage, getSourceFields }">
        <div class="field">
            <button class="btn" :class="{ 'fui-loading fui-loading-sm': loading }" data-icon="refresh" @click.prevent="refresh">{{ 'Refresh Integration' | t('formie') }}</button>
        </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.lightswitchField({
            label: 'Map to People' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'People' }),
            id: 'mapToPeople',
            name: 'mapToPeople',
            on: mapToPeople,
            toggle: 'map-to-people',
        }) }}

        <div id="map-to-people" class="{{ not mapToPeople ? 'hidden' }}">
            <integration-field-mapping
                label="{{ 'People Field Mapping' | t('formie') }}"
                instructions="{{ 'Choose how your form fields should map to your {name} {label} fields.' | t('formie', { name: displayName, label: 'People' }) }}"
                name-label="{{ integration.displayName() }}"
                id="people-field-mapping"
                name="peopleFieldMapping"
                :value="get(form, 'settings.integrations.{{ handle }}.peopleFieldMapping')"
                :rows="get(settings, 'people')"
            ></integration-field-mapping>

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

        {{ forms.lightswitchField({
            label: 'Map to Lead' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Lead' }),
            id: 'mapToLead',
            name: 'mapToLead',
            on: mapToLead,
            toggle: 'map-to-lead',
        }) }}

        <div id="map-to-lead" class="{{ not mapToLead ? 'hidden' }}">
            <integration-field-mapping
                label="{{ 'Lead Field Mapping' | t('formie') }}"
                instructions="{{ 'Choose how your form fields should map to your {name} {label} fields.' | t('formie', { name: displayName, label: 'Lead' }) }}"
                name-label="{{ integration.displayName() }}"
                id="lead-field-mapping"
                name="leadFieldMapping"
                :value="get(form, 'settings.integrations.{{ handle }}.leadFieldMapping')"
                :rows="get(settings, 'lead')"
            ></integration-field-mapping>

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

        {{ forms.lightswitchField({
            label: 'Map to Opportunity' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Opportunitys' }),
            id: 'mapToOpportunity',
            name: 'mapToOpportunity',
            on: mapToOpportunity,
            toggle: 'map-to-opportunity',
        }) }}

        <div id="map-to-opportunity" class="{{ not mapToOpportunity ? 'hidden' }}">
            <integration-field-mapping
                label="{{ 'Opportunity Field Mapping' | t('formie') }}"
                instructions="{{ 'Choose how your form fields should map to your {name} {label} fields.' | t('formie', { name: displayName, label: 'Opportunity' }) }}"
                name-label="{{ integration.displayName() }}"
                id="opportunity-field-mapping"
                name="opportunityFieldMapping"
                :value="get(form, 'settings.integrations.{{ handle }}.opportunityFieldMapping')"
                :rows="get(settings, 'opportunity')"
            ></integration-field-mapping>

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

        {{ forms.lightswitchField({
            label: 'Map to Task' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Tasks' }),
            id: 'mapToTask',
            name: 'mapToTask',
            on: mapToTask,
            toggle: 'map-to-task',
        }) }}

        <div id="map-to-task" class="{{ not mapToTask ? 'hidden' }}">
            <integration-field-mapping
                label="{{ 'Task Field Mapping' | t('formie') }}"
                instructions="{{ 'Choose how your form fields should map to your {name} {label} fields.' | t('formie', { name: displayName, label: 'Task' }) }}"
                name-label="{{ integration.displayName() }}"
                id="task-field-mapping"
                name="taskFieldMapping"
                :value="get(form, 'settings.integrations.{{ handle }}.taskFieldMapping')"
                :rows="get(settings, 'task')"
            ></integration-field-mapping>

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