import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { OrderPackagesServiceProxy, OrderPackageDto, PackagesServiceProxy, OrderServiceProxy } 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 { CreateOrEditOrderPackageModalComponent } from './create-or-edit-orderPackage-modal.component';
import { ViewOrderPackageModalComponent } from './view-orderPackage-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: './orderPackages.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class OrderPackagesComponent extends AppComponentBase {

    @ViewChild('createOrEditOrderPackageModal', { static: true }) createOrEditOrderPackageModal: CreateOrEditOrderPackageModalComponent;
    @ViewChild('viewOrderPackageModalComponent', { static: true }) viewOrderPackageModal: ViewOrderPackageModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    orderId: number;
    filteredPackages: any;
    filteredOrders: any;
    pacakageId : number;
    maxOrderIdFilter: number;
    maxOrderIdFilterEmpty: number;
    minOrderIdFilter: number;
    OrderIdFilter:number;
    minOrderIdFilterEmpty: number;
    maxPackageIdFilter: number;
    maxPackageIdFilterEmpty: number;
    minPackageIdFilter: number;
    minPackageIdFilterEmpty: number;
    maxQuantityFilter: number;
    maxQuantityFilterEmpty: number;
    minQuantityFilter: number;
    minQuantityFilterEmpty: number;




    constructor(
        injector: Injector,
        private _orderPackagesServiceProxy: OrderPackagesServiceProxy,
        private _packagesServiceProxy: PackagesServiceProxy,
        private _orderServiceProxy: OrderServiceProxy,
        private _notifyService: NotifyService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _fileDownloadService: FileDownloadService,
    ) {
        super(injector);
    }

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

    getOrderPackages(event?: LazyLoadEvent) {
        this.orderId =  Number((<HTMLInputElement>document.getElementById('selectOrder')).value) ? Number((<HTMLInputElement>document.getElementById('selectOrder')).value) : undefined;
        this.pacakageId =  Number((<HTMLInputElement>document.getElementById('selectPackage')).value) ? Number((<HTMLInputElement>document.getElementById('selectPackage')).value) : undefined;
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.spinnerService.show();

        this._orderPackagesServiceProxy.getAll(
            undefined,
            this.filterText,
            // this.maxOrderIdFilter,
            // this.minOrderIdFilter,
            this.orderId,
            this.pacakageId,
            this.maxQuantityFilter,
            this.minQuantityFilter,
            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());
    }

    createOrderPackage(): void {
        this.createOrEditOrderPackageModal.show();
    }

    deleteOrderPackage(orderPackage: OrderPackageDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderPackagesServiceProxy.delete(orderPackage.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._orderPackagesServiceProxy.getOrderPackagesToExcel(
            undefined,
            this.filterText,
            //this.maxOrderIdFilter == null ? this.maxOrderIdFilterEmpty : this.maxOrderIdFilter,
            //this.minOrderIdFilter == null ? this.minOrderIdFilterEmpty : this.minOrderIdFilter,
            this.orderId,            
            this.pacakageId,
            this.maxQuantityFilter == null ? this.maxQuantityFilterEmpty : this.maxQuantityFilter,
            this.minQuantityFilter == null ? this.minQuantityFilterEmpty : this.minQuantityFilter,
        )
            .subscribe(result => {
                this._fileDownloadService.downloadTempFile(result);
            });
    }

    filterPackage(): void {
        this._packagesServiceProxy.getAll(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
        ).subscribe(result => {
            this.filteredPackages = result.items;
        });
      }
      filterOrder(): void {
        this._orderServiceProxy.getOrder(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredOrders = result.items;
            console.log(this.filteredOrders);
        });
      } 
}
