{#
/**
 * sitemap plugin for Craft CMS 3.x
 *
 * sitemap index.twig
 *
 * @author    Johan Zandstra
 * @copyright Copyright (c) 2017 Johan Zandstra
 * @link      https://github.com/Dolphiq/craft3-plugin-sitemap
 * @package   Sitemap
 * @since     1.0.0
 */
#}

{% extends "_layouts/cp" %}
{% import "_includes/forms" as forms %}



{% set fullPageForm = true %}

{% do view.registerAssetBundle("dolphiq\\sitemap\\assetbundles\\sitemap\\SitemapAsset") %}
{% do view.registerAssetBundle("dolphiq\\sitemap\\assetbundles\\indexcpsection\\IndexCPSectionAsset") %}


{# The title of this CP section #}
{% set title = "Sitemap settings" %}

{# The URL to this plugin's base CP section #}
{% set pluginCpUrl = url('sitemap') %}

{# Get a URL to an image in our AssetBundle #}
{% set iconUrl = view.getAssetManager().getPublishedUrl('@dolphiq/sitemap/assetbundles/indexcpsection/dist', true) ~ '/img/Index-icon.svg' %}


{# The content of the CP Section#}
{% block content %}
    <input type="hidden" name="action" value="sitemap/settings/save-sitemap">

    {{ redirectInput('settings/sitemap') }}

    {% set sitemapSectionsRows = [] %}

    {% if allStructures|length %}
        {% for section in allStructures %}


            {% set sitemapSectionsRows = sitemapSectionsRows|merge({
                ('id:' ~ section.id): {
                heading: section.heading,
                type: section.type,
                enabled: include('_includes/forms/lightswitch', {
                name: 'sitemapSections[id:'~section.id~'][enabled]',
                on: section.enabled,
                value: 1,
                small: true
                }),
                priority: section.priority,
                changefreq : section.changefreq,
                entries : section.elementCount
                }
            }) %}
        {% endfor %}
    {% endif %}

    {% set sitemapErrors = [] %}



    {{ forms.editableTableField({
        label: "Enable structures"|t('app'),
        instructions: "Choose which entry sections should be listed in the sitemap, and configure the crawl settings. **Note: the sections should have URLs to get listed here.**"|t('app'),
        id: 'sitemapSections',
        name: 'sitemapSections',
        cols: {
            heading: {
                type: 'heading',
                heading: "Include in Sitemap"|t('app'),

            },

            enabled: {
                type: 'heading',
                class: 'thin',
                name: 'test',
                value: '1',
                small: true
            },
            type: {
                type: 'heading',
                heading: 'Type'
            },
            changefreq: {
                heading : 'Change frequency',
                type: 'select',
                info: "The value \"always\" should be used to describe documents that change each time they are accessed. The value \"never\" should be used to describe archived URLs.",
                options: [
                    {label: 'always', value: 'always'},
                    {label: 'hourly', value: 'hourly'},
                    {label: 'daily', value: 'daily'},
                    {label: 'weekly', value: 'weekly'},
                    {label: 'monthly', value: 'monthly'},
                    {label: 'yearly', value: 'yearly'},
                    {label: 'never', value: 'never'}
                ],
                small: true
            },
            priority: {
                heading : 'Priority',
                type: 'select',
                info: "The priority of this URL relative to other URLs on your site. This value does not affect how your pages are compared to pages on other sites.
The default priority of a page is 0.5.<br>
Please note that the priority you assign to a page is not likely to influence the position of your URLs in a search engine's result pages. Search engines may use this information when selecting between URLs on the same site."|t('app'),
                options: [
                    {label: '0.1', value: 0.1},
                    {label: '0.2', value: 0.2},
                    {label: '0.3', value: 0.3},
                    {label: '0.4', value: 0.4},
                    {label: '0.5', value: 0.5},
                    {label: '0.6', value: 0.6},
                    {label: '0.7', value: 0.7},
                    {label: '0.8', value: 0.8},
                    {label: '0.9', value: 0.9},
                    {label: '1.0', value: 1.0}
                ],
                small: true
            },
            entries: {
                heading : 'Entries',
                info: "The number of underlying entries (published and enabled), with a public url",
                type: 'heading',
            }
        },
        rows: sitemapSectionsRows,
        staticRows: true,
        errors: sitemapErrors|unique
    }) }}

    <hr>


    {% set sitemapCategoryRows = [] %}

    {% if allStructures|length %}
        {% for category in allCategories %}


            {% set sitemapCategoryRows = sitemapCategoryRows|merge({
            ('id:' ~ category.id): {
            heading: category.heading,
            type: category.type,
            enabled: include('_includes/forms/lightswitch', {
            name: 'sitemapCategories[id:'~category.id~'][enabled]',
            on: category.enabled,
            value: 1,
            small: true
            }),
            priority: category.priority,
            changefreq : category.changefreq,
            entries : category.elementCount
            }
            }) %}
        {% endfor %}
    {% endif %}

    {% set sitemapErrors = [] %}



    {{ forms.editableTableField({
        label: "Enable categories"|t('app'),
        instructions: "Choose which categories should be listed in the sitemap, and configure the crawl settings. **Note: the categories should have URLs to get listed here.**"|t('app'),
        id: 'sitemapCategories',
        name: 'sitemapCategories',
        cols: {
            heading: {
                type: 'heading',
                heading: "Include in Sitemap"|t('app'),

            },

            enabled: {
                type: 'heading',
                class: 'thin',
                name: 'test',
                value: '1',
                small: true
            },
            type: {
                type: 'heading',
                heading: 'Type'
            },
            changefreq: {
                heading : 'Change frequency',
                type: 'select',
                info: "The value \"always\" should be used to describe documents that change each time they are accessed. The value \"never\" should be used to describe archived URLs.",
                options: [
                {label: 'always', value: 'always'},
                {label: 'hourly', value: 'hourly'},
                {label: 'daily', value: 'daily'},
                {label: 'weekly', value: 'weekly'},
                {label: 'monthly', value: 'monthly'},
                {label: 'yearly', value: 'yearly'},
                {label: 'never', value: 'never'}
                ],
                small: true
            },
            priority: {
                heading : 'Priority',
                type: 'select',
                info: "The priority of this URL relative to other URLs on your site. This value does not affect how your pages are compared to pages on other sites.
The default priority of a page is 0.5.<br>
Please note that the priority you assign to a page is not likely to influence the position of your URLs in a search engine's result pages. Search engines may use this information when selecting between URLs on the same site."|t('app'),
                options: [
                {label: '0.1', value: 0.1},
                {label: '0.2', value: 0.2},
                {label: '0.3', value: 0.3},
                {label: '0.4', value: 0.4},
                {label: '0.5', value: 0.5},
                {label: '0.6', value: 0.6},
                {label: '0.7', value: 0.7},
                {label: '0.8', value: 0.8},
                {label: '0.9', value: 0.9},
                {label: '1.0', value: 1.0}
                ],
                small: true
            },
            entries: {
                heading : 'Entries',
                info: "The number of underlying entries (published and enabled), with a public url",
                type: 'heading',
            }
        },
        rows: sitemapCategoryRows,
        staticRows: true,
        errors: sitemapErrors|unique
    }) }}

    <hr>
    The sitemap to submit to the search engines can be found on <b><a target="_blank" href="/sitemap.xml">/sitemap.xml</a></b>
{% endblock %}

{% js %}

var $sitemapRows = $('#sitemapSections').children('tbody').children(),
$lightswitches = $sitemapRows.children('th:nth-child(2)').children('.lightswitch');

function updateSitemapSections() {
    console.log('updateSitemapSections');
    $lightswitches.each(function() {
        if ($(this).data('lightswitch').on) {
            $(this).parent().nextAll('td').removeClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '0');
        } else {
            $(this).parent().nextAll('td').addClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '-1');
        }
    });
}

$lightswitches.on('change', updateSitemapSections);



// categories
var $sitemapRows = $('#sitemapCategories').children('tbody').children(),
$lightswitches = $sitemapRows.children('th:nth-child(2)').children('.lightswitch');

function updateSitemapCategotries() {
    console.log('updateSitemapCategotries');
    $lightswitchesCategories.each(function() {
        if ($(this).data('lightswitch').on) {
            $(this).parent().nextAll('td').removeClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '0');
        } else {
            $(this).parent().nextAll('td').addClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '-1');
        }
    });
}

$lightswitchesCategories.on('change', updateSitemapCategotries);


Garnish.$doc.ready(function() {
    updateSitemapSections();
    updateSitemapCategotries();
});


{% endjs %}
