import CRUDBase from '../CRUDBase.js'

class AnnouncementResource extends CRUDBase {
    constructor() {
        super()

        this.endpoint = '/api/v1/system/announcements'
        this.required_fields = ['user_ids', 'group_ids', 'title', 'message', 'type']
        this.permission_base = 'v1:system:announcements'

        this.item = 'System Announcement'
        this.plural = 'System Announcements'

        this.listing_definition = {
            display: `
        System announcements are administrative messages that you want all or some of your users to see. These messages can be delivered via e-mail, as a message after login, or as a system banner announcement.
        `,
            columns: [
                {
                    name: 'Title',
                    field: 'title',
                },
                {
                    name: 'Message',
                    field: 'message',
                    renderer: (message) => String(message).slice(0, 150),
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Create New',
                    color: 'success',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'delete',
                    icon: 'fa fa-times',
                    color: 'danger',
                    confirm: true,
                },
            ],
        }

        this.form_definition = {
            fields: [
                {
                    name: 'Title',
                    field: 'title',
                    type: 'text',
                },
                {
                    name: 'Message',
                    field: 'message',
                    type: 'textarea',
                },
                {
                    name: 'Users',
                    field: 'user_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'auth/User',
                        display: (user) => `${user.last_name}, ${user.first_name} (${user.uid})`,
                        value: 'id',
                    },
                },
                {
                    name: 'Groups',
                    field: 'group_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'auth/Group',
                        display: (group) => `${group.name}`,
                        value: 'id',
                    },
                },
                {
                    name: 'Type',
                    field: 'type',
                    type: 'select',
                    options: [
                        {display: 'Login Intercept', value: 'login'},
                        {display: 'E-Mail', value: 'email'},
                        {display: 'System Banner', value: 'banner'},
                    ],
                },
            ],
            handlers: {
                insert: {
                    action: 'redirect',
                    next: '/dash/c/listing/system/Announcement',
                },
            }
        }
    }
}

const system_announcement = new AnnouncementResource()
export { system_announcement }
