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, OrderStatusTypeListDto, OrderStatusTypeServiceProxy, CreateOrderStatusInput, OrderStatusServiceProxy} 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';
import { OrderStatusTypeComponent } from '../order-status-types/order-status-type.component';

@Component({
    selector: 'createOrderStatus',
    templateUrl: './create-order-status.component.html'
})
export class CreateOrderStatusComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;

    status: CreateOrderStatusInput = new CreateOrderStatusInput();
    statusType: number;
    note: string;

    typeFilter: OrderStatusTypeListDto = new OrderStatusTypeListDto();
    filteredType: any;

    orderFilter: number;
    filteredOrder: any;

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

    show(): void {
        this.status = new CreateOrderStatusInput();
        // console.log(this.orderStatusType);
        this.statusType = 0;
        this.orderFilter = 0;
        this.active = true;
        this.modal.show();
    }

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

    save(): void {
        this.saving = true;
        // this.status.orderId = this.orderFilter.id;
        // this.status.orderStatusTypeId = this.statusType.id;
        this.status.orderId = Number((<HTMLInputElement>document.getElementById('Order')).value);
        this.status.orderStatusTypeId = Number((<HTMLInputElement>document.getElementById('StatusType')).value);
        this.status.note = this.note;
        this._orderStatusApp.createOrderStatus(this.status)
            .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();
    }

    filterStatusType(): void {
        this._typeAppService.getOrderStatusType(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredType = result.items;
        });
    }


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