import {Component, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {LocationJsonld, ZoneJsonld, ZoneService} from "@app/core/api/v1";
import {Router} from "@angular/router";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
import {Sort} from "@angular/material/sort";
import {ROUTE_LOCATIONS, ROUTE_ZONES} from "@app/app-routing.module";
import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";
import {ZoneFormComponent} from "@app/_views/zone/zone-form/zone-form.component";

@Component({
  selector: 'app-zone-list',
  templateUrl: './zone-list.component.html',
  styleUrl: './zone-list.component.scss'
})
export class ZoneListComponent {
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;
    protected readonly ZoneFormComponent = ZoneFormComponent;
    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private zoneService: ZoneService,
        private router: Router,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = SearchSelectComponent.getDefaultColDefZones();
    }

    ngOnInit() {

    }

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

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

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