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,
    ShippingCompanyJsonld,
    ShippingCompanyService,
} from "@app/core/api/v1";
import {Router} from "@angular/router";
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 {Sort} from "@angular/material/sort";
import {ROUTE_LOCATIONS, ROUTE_SHIPPING_COMPANIES} from "@app/app-routing.module";
import {
    ShippingCompanyFormComponent
} from "@app/_views/shipping-company/shipping-company-form/shipping-company-form.component";

@Component({
  selector: 'app-shipping-company-list',
  templateUrl: './shipping-company-list.component.html',
  styleUrl: './shipping-company-list.component.scss'
})
export class ShippingCompanyListComponent {
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected readonly ShippingCompanyFormComponent = ShippingCompanyFormComponent;
    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private shippingCompanyService: ShippingCompanyService,
        private router: Router,
        protected appHelperService: AppHelperService,
    ) {

        this.listColDefinitions = [
            {
                name: 'name',
                text: 'common.name',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,

            } as ListColDefinition,
            {
                name: 'code',
                text: 'common.code',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'code',
                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,
        ];
    }

    ngOnInit() {

    }

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

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

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