import { Component, Injector, OnInit, 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 { RouteListDto, RouteServiceProxy } from '@shared/service-proxies/service-proxies';

import * as moment from 'moment';
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 { finalize } from 'rxjs/operators';
import { CreateRouteModalComponent } from './create-routes-modal.component';
import * as _ from 'lodash';
@Component({
    templateUrl: 'routes.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class RoutesComponent extends AppComponentBase implements OnInit {

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


    
    
    filters: {
        id: number;
        location: string;
        
    } = <any>{};

    constructor(
        injector: Injector,
        private _routeService: RouteServiceProxy
        
    ) {
        super(injector);
    }

    ngOnInit(): void {

    }

    getRoutes(event?: LazyLoadEvent) {
        console.log('PRIMENG');
        console.log(this.primengTableHelper);  
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.spinnerService.show();
        this._routeService.getRoute(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            this.filters.location,
            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;
            this.spinnerService.hide();
            
            console.log(result.items);          

            //console.log(this.incidentId.id);
            
        });

    }

 

    
    createRoute(): void {
        this.createRouteModal.show();
    }

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

     

    deleteRoute(route: RouteListDto): void {
            
        
        this.message.confirm(
            this.l('RouteWarningMessage', route.id),
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._routeService.deleteRoute(route.id).subscribe(() => {
                        this.reloadPage();
                        this.notify.info(this.l('SuccessfullyDeleted'));
                        
                    });
                }
            }
        );
    }
    
 

    
   

}
