import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { IncidentServiceProxy, DriverIncidentServiceProxy, DriverIncidentListDto } from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { CreateOrEditDriverIncidentModalComponent } from './create-or-edit-driver-incident.component';

@Component({
    templateUrl: './driver-incidents.component.html',
    animations: [appModuleAnimation()],
    encapsulation: ViewEncapsulation.None
})

export class DriverIncidentsComponent extends AppComponentBase {

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

    filters: {
        userName: string;
    } = <any>{};

    constructor(
        injector: Injector,
        private _driverIncidentServiceProxy: DriverIncidentServiceProxy,
        private _incidentServiceProxy: IncidentServiceProxy
    ) {
        super(injector);  
    }

    onShown(): void {
        $('.kt-select2').select2();
    }

    create() {
        this.createOrEditDriverIncidentModal.show();
    }

    update(id: number): void {
        this.createOrEditDriverIncidentModal.show(id);
    }

    delete(driverIncident: DriverIncidentListDto): void {

        this.message.confirm(
            this.l('IncidentDeleteWarningMessage', driverIncident.incident.content),
            this.l('AreYouSure'),
            (isConfirmed)=> {
                if(isConfirmed) {
                    this._driverIncidentServiceProxy
                        .deleteDriverIncident(driverIncident.id)
                        .subscribe(()=> {
                            this.reloadPage();
                            this.notify.success(this.l('Successfully Deleted'));
                        });
                }
            }
        )
    }

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

    getIncidents(event?: LazyLoadEvent) {
        this.spinnerService.show();
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.spinnerService.show();

        this._driverIncidentServiceProxy.getDriverIncident(undefined,undefined,undefined,this.filters.userName,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            console.log(result.items);
            this.spinnerService.hide();
        });
    }
}