import { Component, OnInit } from '@angular/core';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { Attachment } from 'src/app/models/attachment.model';
import { ActivatedRoute, Router } from '@angular/router';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { ModalController, NavParams } from '@ionic/angular';

@Component({
  selector: 'app-attachments',
  templateUrl: './attachments.page.html',
  styleUrls: ['./attachments.page.scss'],
})
export class AttachmentsPage implements OnInit {

  loadedEventAttachments: Attachment[];
  pageId: string;

  constructor(
    private attachamentsService: AttachmentsService, 
    private iab: InAppBrowser, 
    private modalCtrl: ModalController,
    private navParams: NavParams) { }

  ngOnInit() {
    this.loadAttachments(this.navParams.get('eventId'));
  }

  loadAttachments(eventid:string)
  {    
    this.attachamentsService.getAttachmentsById(eventid).subscribe(attachments => {
      this.loadedEventAttachments = attachments;
      this.pageId = eventid;
    });
  }

  async viewDocument(documenturl:string)
  {
    const url: string = documenturl.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1");
    const browser = this.iab.create(url,"_blank");
    browser.show();
  }
  close(){
    this.modalCtrl.dismiss(null);
  }


}
