{% extends "_layouts/cp" %}
{% set title = "Category Groups"|t('app') %}
{% set readOnly = not craft.app.config.general.allowAdminChanges %}

{% do view.registerAssetBundle('craft\\web\\assets\\admintable\\AdminTableAsset') -%}

{% do view.registerTranslations('app', [
    "Name",
    "Handle",
    "Manage categories",
    "No category groups exist yet.",
]) %}

{% block actionButton %}
    {% if not readOnly %}
        {% set buttonLabel = "New category group"|t('app') %}
        <a href="{{ url('settings/categories/new') }}" class="btn submit add icon" aria-label="{{ buttonLabel }}">{{ buttonLabel }}</a>
    {% endif %}
{% endblock %}

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

{% if readOnly %}
    {% set contentNotice = readOnlyNotice() %}
{% endif %}

{% block content %}
    <div id="categorygroups-vue-admin-table"></div>
{% endblock %}

{% set tableData = [] %}
{% for group in categoryGroups %}
    {% set tableData = tableData|merge([{
        id: group.id,
        title: group.name|t('site'),
        url: url('settings/categories/' ~ group.id),
        name: group.name|t('site')|e,
        handle: group.handle,
        manageCategories: url('categories/'~group.handle),
    }]) %}
{% endfor %}

{% js %}
    var columns = [
        {
            name: '__slot:title',
            title: Craft.t('app', 'Name'),
        },
        {
            name: '__slot:handle',
            title: Craft.t('app', 'Handle'),
        },
        {
            name: 'manageCategories',
            title: "",
            callback: function(value) {
                return '<a href="'+value+'">' + Craft.escapeHtml(Craft.t('app', "Manage categories")) + '</a>';
            }
        },
    ];

    let config = {
        columns: columns,
        container: '#categorygroups-vue-admin-table',
        emptyMessage: Craft.t('app', 'No category groups exist yet.'),
        tableData: {{ tableData|json_encode|raw }}
    };

    {% if not readOnly %}
        config['deleteAction'] = 'categories/delete-category-group';
    {% endif %}

    new Craft.VueAdminTable(config);
{% endjs %}
