// Localized group types
const GROUP_TYPES = {
    "teaching group": "<%= t('group_type.teaching group') %>",
    "course group": "<%= t('group_type.course group') %>",
    "year class": "<%= t('group_type.year class') %>",
    "administrative group": "<%= t('group_type.administrative group') %>",
    "archive users": "<%= t('group_type.archive users') %>",
    "other groups": "<%= t('group_type.other groups') %>",
};

// Table column definitions
const COLUMN_DEFINITIONS = {
    abbr: {
        key: "abbr",
        title: "<%= t('columns.groups.abbreviation') %>",
        type: ST.ColumnType.STRING,
    },

    created: {
        key: "created",
        title: "<%= t('columns.created') %>",
        type: ST.ColumnType.UNIXTIME,
        flags: ST.ColumnFlag.DESCENDING_DEFAULT,
    },

    eid: {
        key: "eid",
        title: "<%= t('columns.groups.eid') %>",
        type: ST.ColumnType.STRING,
    },

    id: {
        key: "id",
        title: "<%= t('columns.puavoid') %>",
        type: ST.ColumnType.NUMERIC,
    },

    modified: {
        key: "modified",
        title: "<%= t('columns.modified') %>",
        type: ST.ColumnType.UNIXTIME,
        flags: ST.ColumnFlag.DESCENDING_DEFAULT,
    },

    members_count: {
        key: "members_count",
        title: "<%= t('columns.groups.members_count') %>",
        type: ST.ColumnType.NUMERIC,
    },

    name: {
        key: "name",
        title: "<%= t('columns.groups.name') %>",
        type: ST.ColumnType.STRING,
        transform: entry => {
            return [
                `<a href="${entry.link}">${ST.escapeHTML(entry.name)}</a>`,
                entry.name
            ];
        }
    },

<% if @is_organisation %>
    school: {
        key: "school",
        title: "<%= t('columns.school') %>",
        type: ST.ColumnType.STRING,
        transform: entry => {
            // Makes filtering by school name work as expected, but the exported CSV
            // will contain display names. Can't have everything.
            return [
                entry.school[1],
                entry.school[1]
            ];
        },
    },
<% end %>

    type: {
        key: "type",
        title: "<%= t('columns.groups.type') %>",
        type: ST.ColumnType.STRING,
        transform: entry => {
            let type = null;

            if (entry.type in GROUP_TYPES) {
                type = GROUP_TYPES[entry.type];

                if (entry.type == 'course group')
                    type = `<span class="stCourseGroup">${type}</span>`;
            } else {
                type = `<span class="missingData"><%= t('missing_data') %></span>`;
            }

            return [type, entry.type];
        },
        missing: {
            display: `<span class="missingData"><%= t('missing_data') %></span>`,
            sort: "",
            filter: "",
        }
    },

    notes: {
        key: "notes",
        title: "<%= t('columns.notes') %>",
        type: ST.ColumnType.STRING,
        transform: entry => [ST.escapeHTML(entry.notes).replaceAll("\r", "").replaceAll("\n", "<br>"), entry.notes],
    },
};

// The default order for columns. "DEFAULT_COLUMNS" above does not have to be in this order.
const COLUMN_ORDER = [
<% if @is_organisation %>
    "school",
<% end %>
    "id",
    "name",
    "abbr",
    "type",
    "members_count",
    "eid",
    "created",
    "modified",
    "notes",
];

// Mass operation: remove all members from the group
class MassGroupRemoveMembers extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};

// Mass operation: lock all members
class MassGroupLockMembers extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};

// Mass operation: unlock all members
class MassGroupUnlockMembers extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};

// Mass operation: mark all members for deletion
class MassGroupMarkMembersForDeletion extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};

// Mass operation: remove member deletion marks
class MassGroupUnmarkMembersForDeletion extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};

<% if @permit_mass_group_deletion %>
// Mass operation: delete the group
class MassGroupDeletion extends ST.MassOperation {
    constructor(parent, container) { super(parent, container); }
    prepareItem(item) { return { state: "ready" }; }
};
<% end %>

<% if @permit_mass_group_change_type %>
// Mass operation: set type
class MassGroupSetType extends ST.MassOperation {
    constructor(parent, container)
    {
        super(parent, container);
        this.type = null;
    }

    buildInterface()
    {
        let html = "";

        html +=
`<table class="settingsChild"><tr>
<th><label for="group_type"><%= t('activeldap.attributes.group.puavoEduGroupType') %>:</label></th>
<td><select id="group_type">`;

        for (const [type, title] of Object.entries(GROUP_TYPES))
            html += `<option value="${type}">${title}</option>`;

        html += "</select></td></tr></table>";

        this.container.innerHTML = html;

        this.container.querySelector("select#group_type").addEventListener("change", e => this.type = e.target.value);
        this.type = this.container.querySelector("select#group_type").value;
    }

    start()
    {
        this.container.querySelector("select#group_type").disabled = true;
    }

    finish()
    {
        this.container.querySelector("select#group_type").disabled = false;
    }

    getOperationParameters()
    {
        return {
            type: this.type
        };
    }

    prepareItem(item)
    {
        if (item.type[ST.INDEX_FILTERABLE] == this.type)
            return { state: "skip" };

        return { state: "ready" };
    }
};
<% end %>

// JavaScript class fields support is so poor that I can't really
// define titles, IDs and other things in classes, they must be here
const MASS_OPERATIONS = [
<% if @permit_mass_group_change_type %>
    {
        operation: "set_type",
        title: "<%= t('groups.index.mass_operations.set_type.title') %>",
        haveSettings: true,
        cls: MassGroupSetType
    },
<% end %>
    {
        operation: "remove_members",
        title: "<%= t('link.remove_all_members_from_group') %>",
        cls: MassGroupRemoveMembers
    },
    {
        operation: "lock_members",
        title: "<%= t('link.lock_all_group_members') %>",
        cls: MassGroupLockMembers
    },
    {
        operation: "unlock_members",
        title: "<%= t('link.unlock_all_group_members') %>",
        cls: MassGroupUnlockMembers
    },
    {
        operation: "mark_members",
        title: "<%= t('link.mark_group_members_for_deletion') %>",
        cls: MassGroupMarkMembersForDeletion
    },
    {
        operation: "unmark_members",
        title: "<%= t('link.unmark_group_members_deletion') %>",
        cls: MassGroupUnmarkMembersForDeletion
    },
<% if @permit_mass_group_deletion %>
    {
        operation: "delete",
        title: "<%= t('link.delete_group') %>",
        cls: MassGroupDeletion
    },
<% end %>
];

function userActions(item)
{
    let html = "";

    const link = `/users/${item.school_id}/groups/${item.id[ST.INDEX_FILTERABLE]}`;

    html += `<a href="${link}/edit" class="btn"><%= t('link.edit') %></a> `;

<% if @permit_group_deletion %>
    let message = "<%= t('general_confirm') %>";

    html += `<a href="${link}" data-method="delete" data-confirm="${message}" rel="nofollow" class="btn btn-danger"><%= t('link.destroy') %></a>`
<% end %>

    return html;
}

function openCallback(item)
{
    return `/users/${item.school_id}/groups/${item.id[ST.INDEX_DISPLAYABLE]}`;
}

const MASS_SELECTS = [
    ["name", "<%= t('columns.groups.name') %>"],
    ["abbr", "<%= t('columns.groups.abbreviation') %>"],
    ["eid", "<%= t('columns.groups.eid') %>"],
    ["id", "<%= t('columns.puavoid') %>"],
];
