import {ICellEditorAngularComp} from 'ag-grid-angular';
import {AfterViewInit, Component, ViewChild} from '@angular/core';
import {ICellEditorParams} from 'ag-grid-community';
import {IonDatetime} from '@ionic/angular';

@Component({
    selector: 'cell-editor-select',
    template: `<ion-datetime
                #picker [displayFormat]="format" [(ngModel)]="value" style="padding: 0 0 0 5px;" (ionChange)="finishEdit($event)"
               ></ion-datetime>`,
})
export class DatetimeEditorComponent implements ICellEditorAngularComp, AfterViewInit {
    public params: ICellEditorParams;
    public value: string;
    public format = 'YYYY-MM-DD h:mm a';
    @ViewChild('picker') picker: IonDatetime;

    agInit(params: ICellEditorParams): void {
        this.params = params;
        this.value = this.params.value;
        // @ts-ignore
        if ( this.params.colDef.additionalData.format ) {
            // @ts-ignore
            this.format = this.params.colDef.additionalData.format;
        }
    }

    getValue(): any {
        return this.value;
    }

    ngAfterViewInit(): void {
        this.picker.open();
    }

    finishEdit($event) {
        this.params.stopEditing();
    }
}
