import { Component, OnInit, Output, EventEmitter, Input, HostListener } from '@angular/core';
import { environment } from 'src/environments/environment';
import { TokenService } from 'src/app/services/token.service';

@Component({
  selector: 'app-update-company-payment-method-dialog',
  templateUrl: './update-company-payment-method-dialog.component.html',
  styleUrls: ['./update-company-payment-method-dialog.component.css']
})
export class UpdateCompanyPaymentMethodDialogComponent implements OnInit {

  @Input() company_id:number;
  @Output() close:EventEmitter<any> = new EventEmitter();

  payment_method_url:string = environment.baseServerUrl;
  token:string;

  constructor(private tokenService:TokenService) { }

  ngOnInit(): void {
    this.token = this.tokenService.get();
  }

  onClose() {
    this.close.emit(null);
  }

  @HostListener('window:message',['$event'])
  onIframeMessage(e) {
    if (e.origin==this.payment_method_url) {
      if(e.data.status=='success') {
        const self = this;
        setTimeout(() => {
          self.onClose();
        }, 1000);
      }
      else if(e.data.status=='error') {
        const self = this;
        setTimeout(() => {
          self.onClose();
        }, 1000);
      }
    }
  }

}
