import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, OnDestroy, ElementRef, Renderer, QueryList, ViewChildren } 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, ControllerRouteServiceProxy } 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 { CheckBoxComponent } from '@app/sprintship/core-components/checkbox/checkbox.component.ts';

import * as _ from 'lodash';
declare var $: any;
declare var jquery:any;
@Component({
    selector: 'table-routes-details',
    templateUrl: 'table-routes-details.component.html',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./table-routes-details.component.less', './tables.checkbox.component.less'],
    animations: [appModuleAnimation()]
})

export class TableRoutesComponents extends AppComponentBase implements OnInit, OnDestroy {

    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
    @ViewChildren('CheckBoxComponent') chkBoxList: QueryList<any>;
    @Input() filters: {
        locationFilter: number;
        statusFilter: number;
        typeFilter: number;
        startDate: Date;
        endDate: Date;
    } = <any>{};
    device_type: string;

    private cBox: CheckBoxComponent;
	constructor(
        injector: Injector,
        private _routeService: RouteServiceProxy,
        private _controllerRouteService: ControllerRouteServiceProxy,
        private renderer: Renderer, 
        private elem: ElementRef
        
    ) {
        super(injector);
    }

    ngOnInit(): void {
       
    }

    ngOnDestroy(): void {

    }
    populateTable(event?: LazyLoadEvent): void
    {
        var that = this;
        if(this.filters.locationFilter !== undefined)
        { 
                this.device_type = 'IOS';
                if (this.primengTableHelper.shouldResetPaging(event)) {
                    this.paginator.changePage(0);
                    return;
                }
         
               this.filters.startDate = new Date(this.filters.startDate.toString());
               this.filters.endDate = new Date(this.filters.endDate.toString());
               let startDate = moment(moment(this.filters.startDate).format('YYYY-MM-DD HH:mm:ss'));
               let endDate = moment(moment(this.filters.endDate).format('YYYY-MM-DD HH:mm:ss'));
              
               let startDateA = startDate['_i'].split(' ');
               startDateA = startDateA[0] + ' ' + '00:00:00';
    
               let endDateA = endDate['_i'].split(' ');
               endDateA = endDateA[0] + ' ' + '23:59:59';
               that.spinnerService.show();
               this._controllerRouteService.getControllerRoute(undefined, this.filters.locationFilter, undefined,  moment.utc(moment(startDateA).toISOString()), moment.utc(moment(endDateA).toISOString()), undefined, undefined, undefined)
                            .subscribe(result => {
                           
                            that.primengTableHelper.totalRecordsCount = result.totalCount;
                            that.primengTableHelper.records = result.items;
                            that.spinnerService.hide();
                   
               });
         }
    }
    tickBox()
    {
        this.cBox.tickBox();
    }
    getSelected()
    {
        console.log(this.chkBoxList);
        this.chkBoxList.forEach(chkBoxL=> console.log(chkBoxL));
    }
}