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


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

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


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

    active = false;
    saving = false;

    orderStatusType: CreateOrEditOrderStatusTypeDto = new CreateOrEditOrderStatusTypeDto();



    constructor(
        injector: Injector,
        private _orderStatusTypesServiceProxy: OrderStatusTypesServiceProxy
    ) {
        super(injector);
    }

    show(orderStatusTypeId?: number): void {

        if (!orderStatusTypeId) {
            this.orderStatusType = new CreateOrEditOrderStatusTypeDto();
            this.orderStatusType.id = orderStatusTypeId;

            this.active = true;
            this.modal.show();
        } else {
            this._orderStatusTypesServiceProxy.getOrderStatusTypeForEdit(orderStatusTypeId).subscribe(result => {
                this.orderStatusType = result.orderStatusType;


                this.active = true;
                this.modal.show();
            });
        }
    }

    save(): void {
            this.saving = true;

			
            this._orderStatusTypesServiceProxy.createOrEdit(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();
    }
}
