{% extends "sprout-base-sitemaps/_layouts/base" %}
{% import "_includes/forms" as forms %}
{% import 'sprout-base/_includes/sproutcp' as sprout %}

{% set sectionLayout = craft.app.request.getSegment(2) %}
{% set siteGroup = craft.app.sites.getGroupById(firstSiteInGroup.groupId) %}

{% block content %}
    <input type="hidden" name="siteId" value="{{ currentSite.id }}">

    <div class="sproutseo-section-header"
         style="display: flex;align-items: center;{% if enableMultilingualSitemaps and currentSite.id != firstSiteInGroup.id %}padding:18px 24px 17px;{% endif %}">
        <div style="flex-grow: 1;">
            <h3 style="text-transform: uppercase;">{{ "Sections"|t }}
                <span class="info">{{ "Manage all of your Section-level SEO for Search, Social Sharing, Sitemaps, and Structured Data."|t }}</span>
            </h3>
        </div>
        <div style="flex-shrink: 0;">
            {% if enableMultilingualSitemaps and currentSite.id != firstSiteInGroup.id %}
                <div class="buttons right">
                    <a class="btn submit icon"
                       href="{{ url(pluginHandle ~ '/sitemaps') ~ '/' ~ firstSiteInGroup.handle }}">{{ "Edit {group} Sitemap"|t('sprout-base-sitemaps', {
                            group: siteGroup.name
                        }) }}</a>
                </div>
            {% endif %}
        </div>
    </div>

    {% if not enableMultilingualSitemaps or (enableMultilingualSitemaps and currentSite.id == firstSiteInGroup.id) %}

        {% for urlEnabledSectionTypeKey, urlEnabledSectionType in urlEnabledSectionTypes %}

            {% for urlEnabledSectionKey, urlEnabledSection in urlEnabledSectionType.urlEnabledSections %}

                {% set sitemapSection = urlEnabledSection.sitemapSection %}

                {% if loop.first %}

                    {% set name = urlEnabledSectionType.getName() %}

                    <table class="data fullwidth sitemap-settings">
                    <thead>

                    {% block sitemapSectionTableRowHead %}{% endblock %}

                    </thead>
                    <tbody>

                {% endif %}
                {% if sitemapSection.name is defined %}
                    {% set elementTableName = urlEnabledSectionType.getElementTableName() %}
                    {% set sproutSeoHandle = elementTableName~':'~sitemapSection.handle %}

                    <tr class="{{ sitemapSection.isNew ? 'sitemapsection-isnew' : null }}"
                        data-rowid="{{ urlEnabledSectionKey }}"
                        data-id="{{ sitemapSection.id ? sitemapSection.id : null }}"
                        data-type="{{ urlEnabledSectionType.getType() }}"
                        data-url-enabled-section-id="{{ sitemapSection.urlEnabledSectionId }}"
                        data-uri="{{ sitemapSection.uri }}"
                        data-is-new="{{ sitemapSection.isNew }}"
                        data-plugin-handle="{{ craft.app.getRequest().getSegment(1) }}">

                        {% block sitemapSectionTableRow %}{% endblock %}

                    </tr>
                {% endif %}

                {% if loop.last %}

                    </tbody>
                    </table>
                    <br>

                {% endif %}

            {% endfor %}

        {% endfor %}

    {% else %}
        {% set boxBody %}
            <p>{{ "Multi-lingual sitemaps have a single sitemap edit page for all URL-Enabled Sites in a Site Group. Edit the <a href='{url}'>{group} Sitemap</a> to manage the multi-lingual sitemap for all sites in the site group."|t('sprout-base-sitemaps', {
                    group: siteGroup.name,
                    url: url(pluginHandle ~ '/sitemaps') ~ '/' ~ firstSiteInGroup.handle
                })|raw }}</p>
        {% endset %}

        {{ sprout.mediaBox({
            heading: "{group} Multi-lingual Sitemap"|t('sprout-base-sitemaps', {
                group: siteGroup.name
            }),
            body: boxBody,
            resourcePath: '@sproutbasesitemaps/icon.svg'
        }) }}
    {% endif %}

    {% if pluginSettings.enableCustomSections %}

        <div class="sproutseo-section-header"
             style="display: flex;align-items: center;margin-top:40px;">
            <div style="flex-grow: 1;">
                <h3 style="text-transform: uppercase;">{{ "Custom Pages"|t }}</h3>
            </div>
            <div style="flex-shrink: 0;">
                <div class="buttons right">
                    <a class="btn submit add icon"
                       href="{{ url(pluginHandle ~ '/sitemaps/new/' ~ currentSite.handle) }}">{{ "Custom URL"|t }}</a>
                </div>
            </div>
        </div>

        <table class="data fullwidth custom-pages sitemap-settings" id="custom-pages">
            <thead>

            {% block customSectionTableRowHead %}{% endblock %}

            </thead>
            <tbody>

            {% if customSections|length %}

                {% for customSection in customSections %}

                    {% set customSectionId = "customUrl-" ~ customSection.id %}

                    <tr data-rowid="{{ customSectionId }}"
                        data-id="{{ customSection.id }}"
                        data-type="{{ customSection.type }}"
                        data-name="{{ "Custom Page"|t('sprout-base-sitemaps') }}"
                        data-plugin-handle="{{ craft.app.getRequest().getSegment(1) }}">

                        {% block customSectionTableRow %}{% endblock %}

                    </tr>

                {% endfor %}

            {% else %}

                {% set boxBody %}
                    <p>{{ "Add a Custom URL for any page that doesn't exist in a URL-Enabled Section."|t('sprout-base-sitemaps')|raw }}</p>
                {% endset %}

                {{ sprout.mediaBox({
                    heading: "Custom Pages"|t('sprout-base-sitemaps'),
                    body: boxBody,
                    resourcePath: '@sproutbasesitemaps/icon.svg'
                }) }}

            {% endif %}

            </tbody>
        </table>


    {% endif %}

{% endblock %}

{% do view.registerAssetBundle("barrelstrength\\sproutbase\\web\\assets\\cp\\CpAsset") %}
{% do view.registerAssetBundle("barrelstrength\\sproutbasesitemaps\\web\\assets\\sitemaps\\SitemapsAsset") %}

{% js %}
    new Craft.SproutSeo.Sitemaps();

    new Craft.AdminTable({
    tableSelector: '#custom-pages',
    deleteAction: 'sprout-base-sitemaps/sitemaps/delete-sitemap-by-id',
    onDeleteObject: function(id) {
    if($('#custom-pages').find('tbody').find('tr').size() == 0)
    {
    $('#custom-pages').hide();
    }
    }
    });
{% endjs %}