import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrderStatusTypeServiceProxy, OrderStatusTypeListDto } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import { CreateOrderStatuTypeModalComponent } from './create-order-status-types-modal.component';

@Component({
    templateUrl: './order-status-type.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class OrderStatusTypeComponent extends AppComponentBase {

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

    uploadUrl: string;

    //Filters
    advancedFiltersAreShown = false;
    filterText = '';

    constructor(
        injector: Injector,
        private _orderStatusTypeService: OrderStatusTypeServiceProxy,
        private _activatedRoute: ActivatedRoute
    ) {
        super(injector);
        // this.filterText = this._activatedRoute.snapshot.queryParams['filterText'] || '';
    }

    getOrderStatusTypes(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.spinnerService.show();
        this._orderStatusTypeService.getOrderStatusType(
            undefined,
            this.filterText,
            undefined,
            undefined,
            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;
        });
    }

    createOrderStatuType(): void {
        this.createOrderStatuTypeModal.show();
    }


    deleteOrderStatusType(orderStatusType: OrderStatusTypeListDto): void {
        this.message.confirm(
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderStatusTypeService.deleteOrderStatusType(orderStatusType.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

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

}
