import { Injector, Component, ViewChild } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { PostalCodeServiceProxy, PostalCodeListDto } from "@shared/service-proxies/service-proxies";
import { Table } from "primeng/table";
import { LazyLoadEvent } from "primeng/primeng";
import { finalize } from "rxjs/operators";
import { Paginator } from 'primeng/components/paginator/paginator';


@Component({

    templateUrl: './postal-codes.component.html',
    animations: [appModuleAnimation()]


})

export class PostalCodesComponent extends AppComponentBase{

    @ViewChild("dataTable", {static: true}) dataTable:Table
    @ViewChild("paginator", {static: true}) paginator: Paginator
    
    constructor(

        injector:Injector,
        private _postalCodeService: PostalCodeServiceProxy

    ){

        super(injector);

    }

    filters: {
        id: number;
        name: string;
        country: string;
        city: string;
        state: string;

    } = <any>{};

    getPostalCodes(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.spinnerService.show();
        this._postalCodeService.getPostalCode(  
            this.filters.id,
            this.filters.name,
            this.filters.country,

            this.filters.state,
            this.filters.city,

            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event),
        ).pipe(finalize(() => this.spinnerService.hide())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
        });
    }

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }

    deletePostalCodes(postalcodes: PostalCodeListDto): void {
        this.message.confirm(
            this.l('Are you sure you want to delete this package?'),
            isConfirmed => {
                if (isConfirmed) {
                    this._postalCodeService.deletePostalCode(postalcodes.id).subscribe(() => {
                        this.reloadPage();
                        this.notify.info(this.l('Successfully Deleted'));
                        
                    });
                    
                }
            }
        );
    } 

}