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 {UserJsonld, UserTripService} 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-list',
  templateUrl: './user-trip-list.component.html',
  styleUrl: './user-trip-list.component.scss'
})
export class UserTripListComponent {
    @Input() public user?: UserJsonld;
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private userTripService: UserTripService,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = [
            {
                name: 'pilotageReference',
                text: 'trip.pilot_reference',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'trip',
                field: 'pilotageReference',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'customerReference',
                text: 'trip.customer_reference',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'trip',
                field: 'customerReference',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'userFirstname',
                text: 'users.firstname',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'firstName',
                subResource: 'user',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'userLastname',
                text: 'users.lastname',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'lastName',
                subResource: 'user',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'captainName',
                text: 'trip.captain_name',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'captainName',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'createdAt',
                text: 'common.created_at',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'createdAt',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
            {
                name: 'completed',
                text: 'user_trip.completed',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'completed',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'approved',
                text: 'user_trip.approved',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'approved',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'completedDate',
                text: 'user_trip.completed_at',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'completedDate',
                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.userTripService.userTripsGetCollection(
            index,
            pageSize,
            undefined,
         undefined,
            this.user ? this.user.id : undefined,
         undefined,
         //   term ? Number(term) : undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }
}
