import { Component } from '@angular/core';
import { ModalService } from 'src/services/modal/modal.service';

@Component({
  selector: 'modal',
  templateUrl: './modal.component.html',
  styleUrl: './modal.component.scss',
})
export class ModalComponent {
  constructor(private modalAccessor: ModalService) {}
  public modalComponent: any;
  public inputData: any;
  public modalRef: any;

  public show: boolean = false;

  ngOnInit() {
    this.modalRef = document.getElementById('myModal');
    this.modalAccessor.modal$.subscribe((data) => {
      this.modalComponent = data.component;
      this.inputData = data.data;
      this.open();
    });
    this.modalAccessor.closeModal$.subscribe(() => {
      this.close();
    });
  }

  public open(): void {
    this.show = true;
    this.modalRef!.style.display = 'block';
  }

  public close(): void {
    this.show = false;
    this.modalRef!.style.display = 'none';
  }

  public cancel() {
    this.modalAccessor.handleModalClosing('cancel');
  }
}
