import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { OrderPackagesServiceProxy, CreateOrEditOrderPackageDto, PackagesServiceProxy, OrderServiceProxy } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';


@Component({
    selector: 'createOrEditOrderPackageModal',
    templateUrl: './create-or-edit-orderPackage-modal.component.html'
})
export class CreateOrEditOrderPackageModalComponent extends AppComponentBase {

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


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

    active = false;
    saving = false;

    filteredPackages:any;
    filteredOrders: any;

    orderPackage: CreateOrEditOrderPackageDto = new CreateOrEditOrderPackageDto();



    constructor(
        injector: Injector, 
        private _orderServiceProxy: OrderServiceProxy,
        private _orderPackagesServiceProxy: OrderPackagesServiceProxy,
        private _packagesServiceProxy: PackagesServiceProxy,
       
    ) {
        super(injector);
    }

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

    show(orderPackageId?: number): void {

        if (!orderPackageId) {
            this.orderPackage = new CreateOrEditOrderPackageDto();
            this.orderPackage.id = orderPackageId;

            this.active = true;
            this.modal.show();
        } else {
            this._orderPackagesServiceProxy.getOrderPackageForEdit(orderPackageId).subscribe(result => {
                this.orderPackage = result.orderPackage;
                this.active = true;
                this.modal.show();
            });
        }
    }

    save(): void {
            this.saving = true;
            this.orderPackage.orderId =  Number((<HTMLInputElement>document.getElementById('selectedOrder')).value) ? Number((<HTMLInputElement>document.getElementById('selectedOrder')).value) : null;
            this.orderPackage.packageId =  Number((<HTMLInputElement>document.getElementById('selectedPackage')).value) ? Number((<HTMLInputElement>document.getElementById('selectedPackage')).value) : null;
            console.log(this.orderPackage);
            this._orderPackagesServiceProxy.createOrEdit(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._packagesServiceProxy.getAll(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
        ).subscribe(result => {
            this.filteredPackages = result.items;
            console.log(this.filteredPackages);
        });
      }
      filterOrder(): void {
        this._orderServiceProxy.getOrder(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredOrders = result.items;
        });
      } 
}
