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 { TimeZoneListDto, TimeZoneServiceProxy } 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 { CreateTimeZoneModalComponent } from './create-timezones-modal.component';
import * as _ from 'lodash';
@Component({
    templateUrl: 'timezones.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class TimeZonesComponent extends AppComponentBase implements OnInit {

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

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



    constructor(
        injector: Injector,
        private _timeZoneService: TimeZoneServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {

    }

    getTimeZones(event?: LazyLoadEvent) {
          
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.spinnerService.show();
        this._timeZoneService.getTimeZone(
            this.filters.id,
            this.filters.value,
            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();
        });

    }

    createTimeZone(): void {
        this.createTimeZoneModal.show();
    }

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

    deleteTimeZone(timezone: TimeZoneListDto): void {
    this.message.confirm(
        this.l('TimeZonesWarningMessage', timezone.value),
        this.l('AreYouSure'),
        isConfirmed => {
            if (isConfirmed) {
                this._timeZoneService.deleteTimeZone(timezone.id).subscribe(() => {
                    this.reloadPage();
                    this.notify.info(this.l('SuccessfullyDeleted'));
                    
                });
                
            }
        }
      );
    } 

 

    
   

}
