import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';

@Component({
    selector: 'app-date-range-picker',
    templateUrl: './date-range-picker.component.html',
    styleUrl: './date-range-picker.component.css',
    standalone: false
})
export class DateRangePickerComponent implements OnInit {
  startDate: Date | null;
  endDate: Date | null;
  
  startDateControl: FormControl;
  endDateControl: FormControl;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _dialogRef: MatDialogRef<DateRangePickerComponent>
  ) {
    this.startDate = null;
    this.endDate = null;

    this.startDateControl = new FormControl(this.startDate);
    this.endDateControl = new FormControl(this.endDate);
  }

  ngOnInit(): void {
    if(this._data.startDate != null){
      this.startDate = this._data.startDate;
      this.startDateControl.setValue(this.startDate);
    }

    if(this._data.endDate != null){
      this.endDate = this._data.endDate;
      this.endDateControl.setValue(this.endDate);
    } 
  }

  assignDates(){
    let returnObj = {
      startDate: this.startDateControl.value,
      endDate: this.endDateControl.value
    };

    let returnStr = JSON.stringify(returnObj);
    this._dialogRef.close(returnStr);
  }
}
