import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { EntityDtoOfInt64, ShippingPackageListDto, ShippingPackageServiceProxy } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { HttpClient } from '@angular/common/http';
import { FileUpload } from 'primeng/fileupload';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './package-type.component.html',
    encapsulation: ViewEncapsulation.None,
    // styleUrls: ['./users.component.less'],
    animations: [appModuleAnimation()]
})
export class PackageTypeComponent extends AppComponentBase {

    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
    @ViewChild('ExcelFileUpload', {static: false}) excelFileUpload: FileUpload;

    uploadUrl: string;

    filters: {
        id: number;
        name: string;
        CurrentPage: number;
    } = <any>{};
    
    advancedFiltersAreShown: boolean = false;

    constructor(
        injector: Injector,
        private _packageService: ShippingPackageServiceProxy,
    ) {
        super(injector);
        
    }

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

            return;
        }

        this.spinnerService.show();

        this._packageService.getShippingPackage(  
            this.filters.id,
            this.filters.name,
            this.filters.CurrentPage,
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSorting(this.dataTable),
            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());
    }

    deletePackageType(packages: ShippingPackageListDto): void {
        this.message.confirm(
            this.l('Are you sure you want to delete this package?'),
            isConfirmed => {
                if (isConfirmed) {
                    this._packageService.deleteShippingPackage(packages.id).subscribe(() => {
                        this.reloadPage();
                        this.notify.info(this.l('Successfully Deleted'));
                        
                    });
                    
                }
            }
        );
    } 
}
