import {AfterViewInit, Component, ViewChild} from '@angular/core';

import {SelectionType} from '@swimlane/ngx-datatable';

import {ItemsDatatableComponent} from 'm2-components';
import {CdDemoComponent} from '../../../abstract/CdDemoComponent';
import {CdHelperService} from '../../../../../service/cd-helper.service';


@Component({
    template: `
        <div class="cd-demo-widget h-100">
            <items-datatable
                [selectionType]="SelectionType.multi"
                (itemSelected)="onItemSelected($event)"
            >
            </items-datatable>
        </div>
    `
})
export class CdItemsTableDemoComponent extends CdDemoComponent implements AfterViewInit {

    constructor(
        private cdHelperService: CdHelperService
    ) {
        super();
    }

    @ViewChild(ItemsDatatableComponent) itemsDatatableComponent: ItemsDatatableComponent;

    SelectionType = SelectionType;


    ngAfterViewInit() {
        super.ngAfterViewInit();

        this.initTable();
    }


    async initTable(): Promise<void> {
        const nodeId = await this.cdHelperService.getAnyNodeId();
        if (nodeId != null) {
            this.itemsDatatableComponent.setupTable(true, nodeId);
        }
    }

    onItemSelected(itemId: number): void {
        // do smth...
    }

}
