import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { OrderStatusTypesServiceProxy, OrderStatusTypeDto  } from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies';
import { CreateOrEditOrderStatusTypeModalComponent } from './create-or-edit-orderStatusType-modal.component';
import { ViewOrderStatusTypeModalComponent } from './view-orderStatusType-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { FileDownloadService } from '@shared/utils/file-download.service';
import * as _ from 'lodash';
import * as moment from 'moment';

@Component({
    templateUrl: './orderStatusTypes.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class OrderStatusTypesComponent extends AppComponentBase {

    @ViewChild('createOrEditOrderStatusTypeModal', { static: true }) createOrEditOrderStatusTypeModal: CreateOrEditOrderStatusTypeModalComponent;
    @ViewChild('viewOrderStatusTypeModalComponent', { static: true }) viewOrderStatusTypeModal: ViewOrderStatusTypeModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    codeFilter = '';
    descriptionFilter = '';
    maxSortOrderFilter : number;
		maxSortOrderFilterEmpty : number;
		minSortOrderFilter : number;
		minSortOrderFilterEmpty : number;




    constructor(
        injector: Injector,
        private _orderStatusTypesServiceProxy: OrderStatusTypesServiceProxy,
        private _notifyService: NotifyService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _fileDownloadService: FileDownloadService
    ) {
        super(injector);
    }

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

        this.spinnerService.show();
        

        this._orderStatusTypesServiceProxy.getAll(
            this.filterText,
            this.codeFilter,
            this.descriptionFilter,
            this.maxSortOrderFilter == null ? this.maxSortOrderFilterEmpty: this.maxSortOrderFilter,
            this.minSortOrderFilter == null ? this.minSortOrderFilterEmpty: this.minSortOrderFilter,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getSkipCount(this.paginator, event),
            this.primengTableHelper.getMaxResultCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
        });
    }

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

    createOrderStatusType(): void {
        this.createOrEditOrderStatusTypeModal.show();
    }

    deleteOrderStatusType(orderStatusType: OrderStatusTypeDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderStatusTypesServiceProxy.delete(orderStatusType.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._orderStatusTypesServiceProxy.getOrderStatusTypesToExcel(
        this.filterText,
            this.codeFilter,
            this.descriptionFilter,
            this.maxSortOrderFilter == null ? this.maxSortOrderFilterEmpty: this.maxSortOrderFilter,
            this.minSortOrderFilter == null ? this.minSortOrderFilterEmpty: this.minSortOrderFilter,
        )
        .subscribe(result => {
            this._fileDownloadService.downloadTempFile(result);
         });
    }
}
