{% requireAdmin %}

{% extends "_layouts/cp" %}
{% set title = "Routes"|t('app') %}

{% block actionButton %}
    <div id="add-route-btn" class="btn submit add icon">{{ "New route"|t('app') }}</div>
{% endblock %}

{% set crumbs = [
    { label: "Settings"|t('app'), url: url('settings') }
] %}

{% do view.registerAssetBundle("craft\\web\\assets\\routes\\RoutesAsset") %}

{% do view.registerTranslations('app', [
    "Global",
    "New route order saved.",
    "Couldn’t save new route order.",
    "If the URI looks like this",
    "Add a token",
    "Load this template",
    "Edit Route",
    "Create a new route",
    "Route Saved.",
    "Couldn’t save route.",
    "Are you sure you want to delete this route?",
    "Route deleted.",
]) %}


{% set routes = craft.routes.getProjectConfigRoutes() %}


{% block content %}
    <p id="noroutes"{% if routes %} class="hidden"{% endif %}>
        {{ "No routes exist yet."|t('app') }}
    </p>

    <div id="routes">
        {% for route in routes %}
            <div class="route" data-uid="{{ route.uid }}"{% if route.siteUid %} data-site-uid="{{ route.siteUid }}"{% endif %}>
                <div class="uri-container">
                    {%- spaceless %}
                        {% if craft.app.getIsMultiSite() %}
                            <span class="site">{{ route.siteUid ? craft.app.sites.getSiteByUid(route.siteUid).name|t('site') : "Global"|t('app') }}</span>
                        {% endif %}
                        <span class="uri">{{ route.uriDisplayHtml|raw }}</span>
                    {% endspaceless -%}
                </div>
                <div class="template">{{ route.template }}</div>
            </div>
        {% endfor %}
    </div>
{% endblock %}


{% js %}
    Craft.routes.tokens = {
        {% for name, pattern in tokens %}
            {% if not loop.first %},{% endif %}
            "{{ name|e('js') }}": "{{ pattern|e('js') }}"
        {% endfor %}
    };
{% endjs %}
