import {Component, Input, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {TripJsonld, UserTripEventService} 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";

@Component({
  selector: 'app-user-trip-event-list',
  templateUrl: './user-trip-event-list.component.html',
  styleUrl: './user-trip-event-list.component.scss'
})
export class UserTripEventListComponent {
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;
    @Input() public trip?: TripJsonld;

    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private userTripEventService: UserTripEventService,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = [
            {
                name: 'eventName',
                text: 'model.event',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                subResource: 'event',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'user',
                text: 'model.user',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'user',
                field: 'fullName',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'location',
                text: 'model.location',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'name',
                subResource: 'location',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'zone',
                text: 'model.zone',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'zoneName',
                subResource: 'location',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'date',
                text: 'basic.date',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'date',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
            {
                name: 'createdAt',
                text: 'common.created_at',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'createdAt',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
        ];
    }

    ngOnInit() {

    }

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

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.userTripEventService.userTripEventsGetCollection(
            index,
            pageSize,
            undefined,
            undefined,
            this.trip !== undefined ? this.trip.dbId! : undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }
}
