import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrderServiceProxy, OrderListDto, LocationServiceProxy, LocationListDto } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { CreateOrderModalComponent } from './create-order-modal.component';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';

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

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

    //Filters
    filterText = undefined;
    locationFilter: LocationListDto = new LocationListDto();
    filteredLocation: any;
    filterDate = moment().format('MM/DD/YYYY');

     
    
    constructor(
        injector: Injector,
        private router: Router,
        private _orderAppService: OrderServiceProxy,
        private _locationAppService: LocationServiceProxy
    ) {
        super(injector);
    }

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

    getOrders(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        //if(Number((<HTMLInputElement>document.getElementById('FilterByLocation')).value) === 0){
            //this.filterText = undefined;
        //}else{
            //this.filterText = Number((<HTMLInputElement>document.getElementById('FilterByLocation')).value);
        //}
        console.log(this.filterText);
        this.spinnerService.show();
        this._orderAppService.getOrder(
            this.filterText,
            undefined,
            undefined,
            undefined, //this.filterText,
            undefined,
            undefined, //moment(this.filterDate),
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            // result.items.forEach(item => {
            //     let total = 0;
            //     item.orderPackages.forEach(orderPackage => {
            //         total = total + orderPackage.quantity;
            //     });
            //     item.totalPackages = total;
            // });
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
            console.log(result.items);
        });
    }

    createOrder(): void {
        this.createOrderModal.show();
    }

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

    filterLocation(): void {
        this._locationAppService.getLocation(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredLocation = result.items;
        });
      }

    deleteOrder(order: OrderListDto): void {
        this.message.confirm(
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._orderAppService.deleteOrder(order.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    createOrUpdate(url: any, id?: number){
        if(id != null){
            var myurl = `${url}/${id}`;
        }else{
            var myurl = `${url}`;
        }
        console.log(myurl);
        this.router.navigateByUrl(myurl);
    }

}
