{% 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 mapToPerson = form.settings.integrations[handle].mapToPerson ?? '' %}
{% set mapToDeal = form.settings.integrations[handle].mapToDeal ?? '' %}
{% set mapToLead = form.settings.integrations[handle].mapToLead ?? '' %}
{% set mapToOrganization = form.settings.integrations[handle].mapToOrganization ?? '' %}
{% set mapToNote = form.settings.integrations[handle].mapToNote ?? '' %}

<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 Person' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Persons' }),
            id: 'mapToPerson',
            name: 'mapToPerson',
            on: mapToPerson,
            toggle: 'map-to-person',
        }) }}

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

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

        {{ forms.lightswitchField({
            label: 'Map to Deal' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Deals' }),
            id: 'mapToDeal',
            name: 'mapToDeal',
            on: mapToDeal,
            toggle: 'map-to-deal',
        }) }}

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

            <ul v-if="!isEmpty(get(form, 'settings.integrations.{{ handle }}.errors.dealFieldMapping'))" class="errors" v-cloak>
                <li v-for="(error, index) in get(form, 'settings.integrations.{{ handle }}.errors.dealFieldMapping')" :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: 'Leads' }),
            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 Organization' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Organizations' }),
            id: 'mapToOrganization',
            name: 'mapToOrganization',
            on: mapToOrganization,
            toggle: 'map-to-organization',
        }) }}

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

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

        {{ forms.lightswitchField({
            label: 'Map to Note' | t('formie'),
            instructions: 'Whether to map form data to {name} {label}.' | t('formie', { name: displayName, label: 'Notes' }),
            id: 'mapToNote',
            name: 'mapToNote',
            on: mapToNote,
            toggle: 'map-to-note',
        }) }}

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

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