import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TagTypeServiceProxy, CreateTagInput, TagServiceProxy, TagTypeListDto, ShippingPackageListDto, OrderServiceProxy, ShippingPackageServiceProxy, CreateOrderPackageInput, OrderListDto, OrderPackageServiceProxy} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { OrderList } from 'primeng/primeng';

@Component({
    selector: 'createOrderPackageModal',
    templateUrl: './create-order-package-modal.component.html'
})
export class CreateOrderPackageModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal', {static: false}) modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    orderPackage: CreateOrderPackageInput = new CreateOrderPackageInput();
    package: ShippingPackageListDto = new ShippingPackageListDto();

    packageFilter: ShippingPackageListDto = new ShippingPackageListDto();
    filteredPackage: any;

    orderFilter: OrderListDto = new OrderListDto();
    filteredOrder: any;

    constructor(
        injector: Injector,
        private _orderAppService: OrderServiceProxy,
        private _tagAppService: TagServiceProxy,
        private _packageAppService: ShippingPackageServiceProxy,
        private _orderPackageAppService: OrderPackageServiceProxy
    ) {
        super(injector);
    }

    show(): void {
        this.orderPackage = new CreateOrderPackageInput();
        this.package = new ShippingPackageListDto();
        this.orderFilter = new OrderListDto();
        // console.log(this.orderStatusType);
        this.active = true;
        this.modal.show();
    }

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

    save(): void {
        this.saving = true;
        this.orderPackage.orderId = Number((<HTMLInputElement>document.getElementById('Order')).value);
        this.orderPackage.packageId = Number((<HTMLInputElement>document.getElementById('Package')).value);
        this._orderPackageAppService.createOrderPackage(this.orderPackage)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }

    filterPackage(): void {
        this._packageAppService.getShippingPackage(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredPackage = result.items;
        });
    }


    filterOrder(): void {
        this._orderAppService.getOrder(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredOrder = result.items;
        });
    }
}
