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 { UserLocationListDto, UserLocationServiceProxy } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
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 { FileUpload } from 'primeng/fileupload';
import { finalize } from 'rxjs/operators';
import { CreateUserLocationModalComponent } from './create-user-location-modal.component';

import * as _ from 'lodash';
@Component({
    templateUrl: 'user-location.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class UserLocationComponent extends AppComponentBase implements OnInit {

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

    
    filters: {
        id: number;
        userId: number;
        locationId: number;
        user: string;
        CurrentPage: number;
    } = <any>{};


    userLocation : UserLocationListDto;
    constructor(
        injector: Injector,
        private _userLocationService: UserLocationServiceProxy
       
    ) {
        super(injector);
    }

    ngOnInit(): void {

    }

    getUserLocations(event?: LazyLoadEvent) {
          
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.spinnerService.show();
        this._userLocationService.getUserLocation(
            this.filters.id,
            this.filters.userId,
            this.filters.user,
            this.filters.locationId,
            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);
        });



    }

    
    createUserLocation(): void {
        this.createUserLocationModal.show();
    }
   

    

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }
    
    deleteUserLocation(userLocation): void {
    this.message.confirm(
        this.l('Delete UserLocation'),
        isConfirmed => {
            if (isConfirmed) {
                this._userLocationService.deleteUserLocation(userLocation).subscribe(() => {
                    this.reloadPage();
                    this.notify.info(this.l('Successfully Deleted'));
                    
                });
                
            }
        }
      );
    } 

}
