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

<input type="hidden" name="sproutBaseSettingsType" value="{{ className(settings) }}">

{{ forms.lightswitchField({
    label: "Enable Dynamic Sitemaps"|t,
    instructions: "Output a dynamic XML sitemap of all pages in your URL-Enabled Sections."|t('sprout-base-sitemaps'),
    name: 'enableDynamicSitemaps',
    toggle: 'settings-total-elements',
    on: settings.enableDynamicSitemaps,
    onLabel: "Enable"|t,
    first: true
}) }}

{{ forms.textField({
    label: "Total Elements Per Sitemap"|t('sprout-base-sitemaps'),
    instructions: "The number of items that display on each page of your sitemap. A lower number may be necessary for sitemaps with a large number of elements or limited server resources."|t('sprout-base-sitemaps'),
    name: 'totalElementsPerSitemap',
    placeholder: '500',
    size:'10',
    value: settings.totalElementsPerSitemap,
    errors: ''
}) }}

<hr>

{% if craft.app.isMultiSite %}
    {{ forms.lightswitchField({
        label: "Enable multi-lingual sitemaps"|t('sprout-base-sitemaps'),
        instructions: "Generate a single, multilingual sitemap.xml file for each Site Group. By default, a sitemap.xml is generated for every Site on your Craft website. If you are managing content in multiple languages, enable this setting to generate a single, multi-lingual sitemap.xml for all Sites in each Site Group. The sitemap.xml be created for the primary domain or the first domain that appears in the list of a specific Site Group."|t,
        name: 'enableMultilingualSitemaps',
        on: settings.enableMultilingualSitemaps,
        id: 'enableMultilingualSitemaps',
        onLabel: "Enable"|t
    }) }}

    {% set siteRows = [] %}
    {% set siteGroupRows = [] %}
    {% set siteErrors = settings.getErrors('siteSettings') %}
    {% set groupErrors = settings.getErrors('groupSettings') %}

    {% for site in craft.app.sites.getAllSites() %}
        {% set siteSettings = settings.siteSettings[site.id] ?? null %}

        {% set siteRows = siteRows|merge({
            (site.handle): {
                heading: site.name|t('site'),
                enabled: include('_includes/forms/lightswitch', {
                    name: 'siteSettings['~site.id~']',
                    on: siteSettings,
                    value: site.id,
                    small: true
                })
            }
        }) %}
    {% endfor %}

    {% for group in craft.app.sites.getAllGroups() %}
        {% set groupSettings = settings.groupSettings[group.id] ?? null %}

        {% set siteGroupRows = siteGroupRows|merge({
            (group.name): {
                heading: group.name|t('site'),
                enabled: include('_includes/forms/lightswitch', {
                    name: 'groupSettings['~group.id~']',
                    on: groupSettings,
                    value: group.id,
                    small: true
                })
            }
        }) %}
    {% endfor %}

    <div id="siteWrapper" {% if settings.enableMultilingualSitemaps %} class="hidden" {% endif %}>
        {{ forms.editableTableField({
            label: "Enable Sitemaps"|t('sprout-base-sitemaps'),
            instructions: "Enable each section you wish to include in your XML sitemaps."|t('sprout-base-sitemaps'),
            id: 'sites',
            name: 'sites',
            cols: {
                heading: {
                    type: 'heading',
                    heading: "Site"|t('sprout-base-sitemaps'),
                },
                enabled: {
                    type: 'heading',
                    heading: "Enabled"|t('sprout-base-sitemaps'),
                    class: 'thin'~(not craft.app.getIsMultiSite() ? ' hidden')
                }
            },
            rows: siteRows,
            staticRows: true,
            errors: siteErrors|unique
        }) }}
    </div>

    <div id="groupWrapper" {% if not settings.enableMultilingualSitemaps %} class="hidden" {% endif %}>
        {{ forms.editableTableField({
            label: "Enable Sitemaps"|t('sprout-base-sitemaps'),
            instructions: "Enable each group you wish to include in your XML sitemaps."|t('sprout-base-sitemaps'),
            id: 'groups',
            name: 'groups',
            cols: {
                heading: {
                    type: 'heading',
                    heading: "Group"|t('sprout-base-sitemaps'),
                },
                enabled: {
                    type: 'heading',
                    heading: "Enabled"|t('sprout-base-sitemaps'),
                    class: 'thin'~(not craft.app.getIsMultiSite() ? ' hidden')
                }
            },
            rows: siteGroupRows,
            staticRows: true,
            errors: groupErrors|unique
        }) }}
    </div>

    <hr>

{% endif %}

{{ forms.lightswitchField({
    label: "Enable Custom Pages"|t,
    instructions: "Add a section on the Sitemaps tab to manage Custom Pages that exist outside of any URL-Enabled Sections."|t,
    name: 'enableCustomSections',
    on: (settings.enableCustomSections is empty ? false : true),
    onLabel: "Enable"|t
}) }}


{% do view.registerAssetBundle("barrelstrength\\sproutbasesitemaps\\web\\assets\\general\\GeneralAsset") %}
