import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {ListComponent} from "@app/_components/list/list.component";
import {LocationJsonld, LocationService} from "@app/core/api/v1";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {FilterBarComponent} from "@app/_components/filter-bar/filter-bar.component";
import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
import {ROUTE_LOCATIONS} from "@app/app-routing.module";
import {Sort} from "@angular/material/sort";
import {LocationFormComponent} from "@app/_views/location/location-form/location-form.component";
import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";

@Component({
  selector: 'app-location-list',
  templateUrl: './location-list.component.html',
  styleUrl: './location-list.component.scss'
})
export class LocationListComponent implements OnInit, AfterViewInit {

    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected readonly LocationFormComponent = LocationFormComponent;
    protected listColDefinitions!: ListColDefinition[];

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

        this.listColDefinitions = SearchSelectComponent.getDefaultColDefLocations();
    }

    ngOnInit() {

    }

    ngAfterViewInit(): void {
        this.listComponent.getData();
    }

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.locationService.locationsGetCollection(
            index,
            pageSize,
            term,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }

    getCustomDetailLink(element: LocationJsonld) {
        return ROUTE_LOCATIONS + '/' + this.appHelperService.extractId(element?.id);
    }
}
