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";

@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 = [
            {
                name: 'pilotageReference',
                text: 'trip.pilot_reference',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'pilotageReference',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'vessel',
                text: 'trip.vessel',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'vessel',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'startLocation',
                text: 'trip.start_location',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'startLocation',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'endLocation',
                text: 'trip.end_location',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'endLocation',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'startDate',
                text: 'trip.start_date',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'startDate',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
            {
                name: 'endDate',
                text: 'trip.end_date',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'endDate',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
            {
                name: 'completed',
                text: 'trip.completed',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'completed',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } 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.tripService.tripsGetCollection(
            index,
            pageSize,
            term ? Number(term) : undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }

}
