{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template block_xp/completion-rules-scope-switcher

    Scope switcher.

    Example context (json):
    {
    }
}}
<div class="xp-border-0 xp-border-solid xp-border-b xp-border-gray-200 xp-mb-6 xp-pb-1">
    <div class="xp-flex xp-flex-wrap xp-gap-x-2 xp-gap-y-1">
        <div class="xp-grow">
            <h4 class="xp-m-0 xp-leading-none">
                <span class="xp-block xp-text-2xs xp-font-normal xp-uppercase">{{#str}} rulesscope, block_xp {{/str}}</span>
                <span class="xp-block">{{ scopename }}{{#scopeurl}}<a href="{{.}}" target="_blank" class="xp-inline-block xp-ml-2 xp-text-base xp-text-inherit"
                            title="{{#str}} go, core {{/str}}">
                        <span class="xp-sr-only">{{#str}} go, core {{/str}}</span>
                        <i class="fa fa-external-link" aria-hidden="true"></i>
                    </a>{{/scopeurl}}
                </span>
            </h4>
        </div>
        <div class="xp-flex xp-gap-2 xp-items-end">
            <div class="xp-grow xp-shrink-0 xp-flex xp-gap-1 xp-flex-wrap">
                <button class="btn btn-sm btn-secondary xp-truncate" id="block_xp-course-selector-{{uniqid}}">
                    {{#isincourse}}{{#str}} changecourse, block_xp {{/str}}{{/isincourse}}
                    {{^isincourse}}{{#str}} changetocourse, block_xp {{/str}}{{/isincourse}}
                </button>
                {{#isincourse}}
                    <a href="{{ sitewideurl }}" class="btn btn-sm btn-secondary xp-truncate" data-action="backtositewide">{{#str}} changetositewide, block_xp {{/str}}</a>
                {{/isincourse}}
            </div>
            <div class="xp-grow-0 xp-shrink-0 [&_.icon]:xp-m-0 xp-pb-0.5">
            {{#helpicon}}
                {{> core/help_icon }}
            {{/helpicon}}
            </div>
        </div>
    </div>
</div>
{{#js}}
require(['block_xp/course-selector'], function(CourseSelector) {
    var trigger = document.getElementById('block_xp-course-selector-{{uniqid}}');
    var courseUrlTemplate = "{{ courseurltemplate }}";
    trigger.addEventListener('click', function() {
        CourseSelector.openCourseSelector(function(course) {
            window.location.href = courseUrlTemplate.replace('CONTEXTID', course.contextid) + window.location.hash;
        });
    });

    var backtositewide = document.querySelector('[data-action="backtositewide"]');
    if (backtositewide) {
        backtositewide.addEventListener('click', function(e) {
            e.preventDefault();
            window.location.href = e.currentTarget.getAttribute('href') + window.location.hash;
        });
    }
});
{{/js}}
