import CRUDBase from '../CRUDBase.js'
import { session } from '../../service/Session.service.js'

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

        this.endpoint = '/api/v1/ldap/clients'
        this.required_fields = ['name', 'uid', 'password']
        this.permission_base = 'v1:ldap:clients'

        this.item = 'LDAP Client'
        this.plural = 'LDAP Clients'


        this.listing_definition = {
            display: `
        LDAP Clients are special user accounts that external applications can use to bind to ${session.get('app.name')}'s built-in LDAP server to allow these applications to authenticate users.
        <br><br>
        These special accounts are permitted to bind to the LDAP server, but are not allowed to sign-in to ${session.get('app.name')}.
        `,
            columns: [
                {
                    name: 'Client Name',
                    field: 'name',
                },
                {
                    name: 'User ID',
                    field: 'uid',
                },
            ],
            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 = {
            fields: [
                {
                    name: 'Provider Name',
                    field: 'name',
                    placeholder: 'Awesome External App',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'User ID',
                    field: 'uid',
                    placeholder: 'some_username',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Password',
                    field: 'password',
                    required: ['insert'],
                    type: 'password',
                },
            ],
        }
    }

    async server_config() {
        const results = await axios.get('/api/v1/ldap/config')
        if (results && results.data && results.data.data) return results.data.data
    }
}

const ldap_client = new ClientResource()
export { ldap_client }
