import {Component, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {TripFormComponent} from "@app/_views/trip/trip-form/trip-form.component";
import {TripService} 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 {Observable} from "rxjs";
import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";

@Component({
  selector: 'app-trip-list',
  templateUrl: './trip-list.component.html',
  styleUrl: './trip-list.component.scss'
})
export class TripListComponent {

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

    protected readonly tripFormComponent = TripFormComponent;
    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private tripService: TripService,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = SearchSelectComponent.getDefaultColDefTrips();
    }

    ngOnInit() {

    }

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

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.tripService.tripsGetCollection(
            index,
            pageSize,
            term ? Number(term) : undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }

    get deleteItemFunction(): (id: string) => Observable<any> {
        return (id: string) => this.tripService.tripsIdDelete(id);
    }
}
