import CRUDBase from '../CRUDBase.js'

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

        this.endpoint = '/api/v1/ldap/machine-groups'
        this.required_fields = ['name']
        this.permission_base = 'v1:ldap:machine_groups'

        this.item = 'Computer Group'
        this.plural = 'Computer Groups'

        this.listing_definition = {
            columns: [
                {
                    name: 'Group Name',
                    field: 'name',
                },
                {
                    name: '# Computers',
                    field: 'machine_ids',
                    renderer: machine_ids => Array.isArray(machine_ids) ? machine_ids.length : 0,
                },
                {
                    name: 'Description',
                    field: 'description',
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Create New',
                    color: 'success',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'update',
                    icon: 'fa fa-edit',
                    color: 'primary',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'delete',
                    icon: 'fa fa-times',
                    color: 'danger',
                    confirm: true,
                },
            ],
        }

        this.form_definition = {
            // back_action: {
            //    text: 'Back',
            //    action: 'back',
            // },
            fields: [
                {
                    name: 'Group Name',
                    field: 'name',
                    placeholder: 'DNS Servers',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Description',
                    field: 'description',
                    type: 'textarea',
                },
                {
                    name: 'IAM Target',
                    field: 'id',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                    help: `(LDAP use) Allows restricting users to only those that can access this computer group. (filter: iamTarget)`,
                },
                {
                    name: 'Computers',
                    field: 'machine_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'ldap/Machine',
                        display: machine => `${machine.name}${machine.host_name ? ' (' + machine.host_name + ')' : ''}`,
                        value: 'id',
                    },
                },
            ],
        }
    }
}

const ldap_machinegroup = new MachineGroupResource()
export { ldap_machinegroup }
