import { Component, OnInit } from '@angular/core';
import { LocationJsonld, LocationService } from "@app/core/api/v1";
import { AppHelperService } from "@app/_helpers/app-helper.service";
import { ActivatedRoute } from "@angular/router";
import { FormMode, FormSubmitEvent } from "@app/_components/_abstract/abstract-data-form-component";
import {ModalStatus} from "@app/_helpers/modal.states";

@Component({
    selector: 'app-location-detail',
    templateUrl: './location-detail.component.html'
})
export class LocationDetailComponent implements OnInit {
    protected location!: LocationJsonld;
    protected readonly FormMode = FormMode;

    constructor(
        private locationService: LocationService,
        protected appHelperService: AppHelperService,
        private route: ActivatedRoute
    ) {}

    ngOnInit() {
        this.route.params.subscribe(params => {
            this.locationService.locationsIdGet(params['id']).subscribe(
                data => {
                    this.location = data;
                }
            );
        });
    }

    onFormUpdate(event: FormSubmitEvent<LocationJsonld>) {
        if (event.status === ModalStatus.Submitted && event.data) {
            this.location = event.data;
        }
    }
}