import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateEmailInput, EmailServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './create-email-modal.component.html',
    selector: 'createEmailModal',
})
export class CreateEmailModalComponent extends AppComponentBase {

   @ViewChild('createModal', {static: false}) modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;s
    saving = false;
    email:CreateEmailInput = new CreateEmailInput();

     

    constructor(
        injector: Injector,
        private _emailService: EmailServiceProxy
    ) {
        super(injector);
    }

    show() {
        this.active = true;
        // this.emailService.value = null;
        this.modal.show();
    }

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

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

        this._emailService.createEmail(this.email)
            .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();
    }
}
