import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { OrderStatusesServiceProxy, OrderStatusDto, OrderServiceProxy, OrderStatusTypeServiceProxy  } 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 { CreateOrEditOrderStatusModalComponent } from './create-or-edit-orderStatus-modal.component';
import { ViewOrderStatusModalComponent } from './view-orderStatus-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: './orderStatuses.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class OrderStatusesComponent extends AppComponentBase {

    @ViewChild('createOrEditOrderStatusModal', { static: true }) createOrEditOrderStatusModal: CreateOrEditOrderStatusModalComponent;
    @ViewChild('viewOrderStatusModalComponent', { static: true }) viewOrderStatusModal: ViewOrderStatusModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    filteredOrders: any;
    filteredOrderStatusTypes: any;
    orderId : number;
    orderStatusTypeId: number;
    maxUserIdFilter : number;
		maxUserIdFilterEmpty : number;
		minUserIdFilter : number;
		minUserIdFilterEmpty : number;
    maxOrderIdFilter : number;
		maxOrderIdFilterEmpty : number;
		minOrderIdFilter : number;
		minOrderIdFilterEmpty : number;
    maxGeocodeIdFilter : number;
		maxGeocodeIdFilterEmpty : number;
		minGeocodeIdFilter : number;
		minGeocodeIdFilterEmpty : number;
    maxOrderStatusTypeIdFilter : number;
		maxOrderStatusTypeIdFilterEmpty : number;
		minOrderStatusTypeIdFilter : number;
		minOrderStatusTypeIdFilterEmpty : number;
    maxNoteIdFilter : number;
		maxNoteIdFilterEmpty : number;
		minNoteIdFilter : number;
		minNoteIdFilterEmpty : number;
    maxTimeFilter : moment.Moment;
		minTimeFilter : moment.Moment;




    constructor(
        injector: Injector,
        private _orderStatusesServiceProxy: OrderStatusesServiceProxy,
        private _notifyService: NotifyService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _fileDownloadService: FileDownloadService,
        private _orderServiceProxy : OrderServiceProxy,
        private _orderStatusType: OrderStatusTypeServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(){
        this.filterOrder();
        $('.kt-select2').select2({ 
            placeholder: "Select.."
        });
        this.filterOrderStatusType();
    }

    getOrderStatuses(event?: LazyLoadEvent) {
        this.orderId =  Number((<HTMLInputElement>document.getElementById('selectedOrder')).value) ? Number((<HTMLInputElement>document.getElementById('selectedOrder')).value) : undefined;
        this.orderStatusTypeId = Number((<HTMLInputElement>document.getElementById('selectedOrderStatusType')).value) ? Number((<HTMLInputElement>document.getElementById('selectedOrderStatusType')).value) : undefined;
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.spinnerService.show();

        this._orderStatusesServiceProxy.getAll(
            undefined,
            this.filterText,
            undefined,
            this.orderId,
            undefined,
            this.orderStatusTypeId,
            undefined,
            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());
    }

    createOrderStatus(): void {
        this.createOrEditOrderStatusModal.show();
    }

    deleteOrderStatus(orderStatus: OrderStatusDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderStatusesServiceProxy.delete(orderStatus.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._orderStatusesServiceProxy.getOrderStatusesToExcel(
            undefined,
            this.filterText,
            undefined,
            this.orderId,
            undefined,
            this.orderStatusTypeId,
            undefined,
        )
        .subscribe(result => {
            this._fileDownloadService.downloadTempFile(result);
         });
    }

    filterOrder(): void {
        this._orderServiceProxy.getOrder(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredOrders = result.items;
        });
    }
    filterOrderStatusType(): void {
        this._orderStatusType.getOrderStatusType(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
        ).subscribe(result => {
            this.filteredOrderStatusTypes = result.items;
        });
    }
}
