import {Component, Input} from '@angular/core';
import {FormGroup} from "@angular/forms";
import {DatePipe} from "@angular/common";

@Component({
  selector: 'field-datetime-single',
  templateUrl: 'field-datetime-single.component.html',
  styleUrls: ['field-datetime-single.component.scss']
})
export class FieldDatetimeSingleComponent {
  @Input() field!: any;
  @Input() formGroup!: FormGroup;

  constructor(private datePipe: DatePipe) {
  }

  ngOnInit() {
    setTimeout(() => {this.value = this.initialValue});
  }


  get initialValue() {
    return this.field?.value?.data[0];
  }
  get control() {
    return this.formGroup.controls[this.field.name];
  }
  get value() {
    return this.control?.value;
  }
  set value(value) {
    this.control?.setValue(this.datePipe.transform(value, 'yyyy-MM-dd HH:mm'));
  }

}
