import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrderPackageServiceProxy, OrderStatusListDto, OrderStatusServiceProxy } 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 * as moment from 'moment';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import { CreateOrderStatusComponent } from './create-order-status.component';

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

    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
    @ViewChild('createOrderStatus', {static: false}) createOrderStatus: CreateOrderStatusComponent;

    //Filters
    filterText: number;
    // locationFilter: LocationListDto = new LocationListDto();
    // filteredLocation: any;
    // filterDate = moment().format('MM/DD/YYYY');

     
    
    constructor(
        injector: Injector,
        private router: Router,
        private _orderStatusService: OrderStatusServiceProxy
    ) {
        super(injector);
    }

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

    createOrderStatusModal(): void {
        this.createOrderStatus.show();
    }

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

    deleteStatus(id): void {
        this.message.confirm(
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderStatusService.deleteOrderStatus(id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

}
