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


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

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


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

    active = false;
    saving = false;

    orderStatus: CreateOrEditOrderStatusDto = new CreateOrEditOrderStatusDto();
    // note = new CreateNoteInput();

    filteredOrders: any;
    filteredOrderStatusTypes: any;
    // note: any;


    constructor(
        injector: Injector,
        private _orderStatusesServiceProxy: OrderStatusesServiceProxy,
        private _orderServiceProxy: OrderServiceProxy,
        private _orderStatusType: OrderStatusTypeServiceProxy,
        // private _noteAppService: NoteServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this.filterOrder();
        this.filterOrderStatusType();
    }

    show(orderStatusId?: number): void {

        if (!orderStatusId) {
            this.orderStatus = new CreateOrEditOrderStatusDto();
            this.orderStatus.id = orderStatusId;
            this.orderStatus.time = moment().startOf('day');

            this.active = true;
            this.modal.show();
        } else {
            this._orderStatusesServiceProxy.getOrderStatusForEdit(orderStatusId).subscribe(result => {
                this.orderStatus = result.orderStatus;
                this.active = true;
                this.modal.show();
            });
        }
    }

    save(): void {
        this.saving = true;
        this.orderStatus.orderId = Number((<HTMLInputElement>document.getElementById('updateSelectedOrder')).value) ? Number((<HTMLInputElement>document.getElementById('updateSelectedOrder')).value) : null;
        this.orderStatus.orderStatusTypeId = Number((<HTMLInputElement>document.getElementById('updateSelectedOrderStatusType')).value) ? Number((<HTMLInputElement>document.getElementById('updateSelectedOrderStatusType')).value) : null;
        this._orderStatusesServiceProxy.createOrEdit(this.orderStatus)
            .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();
    }

    filterOrder(): void {
        this._orderServiceProxy.getOrder(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
        ).subscribe(result => {
            this.filteredOrders = result.items;
        });
    }
    filterOrderStatusType(): void {
        this._orderStatusType.getOrderStatusType(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
        ).subscribe(result => {
            this.filteredOrderStatusTypes = result.items;
        });
    }
}
