import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ModalDialogParams } from '@nativescript/angular';
import { DatePicker, Page } from '@nativescript/core';

@Component({
    selector: 'modal-date-picker',
    moduleId: module.id,
    templateUrl: './modal-view-date-picker.component.html',
    styleUrls: ['./modal-view-date-picker.component.scss']
})
export class ModalDatePickerComponent implements OnInit {
    @ViewChild('datePicker', { static: true }) dp: ElementRef;

    constructor(private _params: ModalDialogParams, private page: Page) {
    }

    ngOnInit(): void {
        const picker = <DatePicker>this.dp.nativeElement;
        if (this._params.context.minDate) {
            picker.minDate = this._params.context.minDate;
        }
        picker.date = new Date(this._params.context.year, this._params.context.month - 1, this._params.context.day);
    }

    // onPickerLoaded(eventData: EventData): void {
    //     let picker = <DatePicker>eventData.object;
    //     if (this._params.context.minDate) {

    // 		picker.minDate = this._params.context.minDate;
    // 		console.log("min " + picker.minDate.toISOString());
    //     }
    //     picker.date = new Date(this._params.context.year, this._params.context.month-1, this._params.context.day);
    // }

    onOK() {
        const datePicker = <DatePicker>this.dp.nativeElement;
        this._params.closeCallback(datePicker.date);
    }

    onClose(): void {
        this._params.closeCallback(null);
    }
}
