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 { OrderStatusTypeServiceProxy, UpdateOrderStatusTypeInput} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'updateOrderStatuTypeModal',
    templateUrl: './update-order-status-types-modal.component.html'
})
export class UpdateOrderStatuTypeModalComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;

    orderStatusType: UpdateOrderStatusTypeInput = new UpdateOrderStatusTypeInput();

    constructor(
        injector: Injector,
        private _orderStatusTypeAppService: OrderStatusTypeServiceProxy
    ) {
        super(injector);
    }

    show(orderStatusTypeRecord): void {
        this.orderStatusType = orderStatusTypeRecord;
        // console.log(this.orderStatusType);
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        // document.getElementById('Code').focus();
    }

    save(): void {

        this.saving = true;
        this._orderStatusTypeAppService.updateOrderStatusType(this.orderStatusType)
            .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();
    }
}
