import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { EmployeeReviewServiceProxy, EmployeeReviewListDto } 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 { CreateOrEditEmployeeReviewModalComponent } from './create-or-edit-review.component';

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

export class EmployeeReviewComponent extends AppComponentBase {

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

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

    constructor(
        injector: Injector,
        private _employeeReviewServiceProxy: EmployeeReviewServiceProxy
    ) {
        super(injector);  
    }

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

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

    delete(empReview: EmployeeReviewListDto): void {

        this.message.confirm(
            this.l('EmployeeReviewWarningMessage', empReview.review.content),
            this.l('AreYouSure'),
            (isConfirmed)=> {
                if(isConfirmed) {
                    this._employeeReviewServiceProxy
                        .deleteEmployeeReview(empReview.id)
                        .subscribe(()=> {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        )
    }

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

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

        this.spinnerService.show();

        this._employeeReviewServiceProxy.getEmployeeReview(undefined,undefined,undefined,this.filters.employeeName,
            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;
            this.spinnerService.hide();
        });
    }

}