import { Component, OnInit } from '@angular/core';
import {ResourceService} from "../../service/resource.service";
import {ActivatedRoute} from "@angular/router";
import {environment} from "../../../environments/environment";
import {Resource} from "../../model/resource";

@Component({
  selector: 'app-resource',
  templateUrl: './resource.component.html',
  styleUrls: ['./resource.component.css']
})
export class ResourceComponent implements OnInit {
  resource: Resource ={};
  imagePath = environment.baseUrl+"/resource/image";
  filePath = environment.baseUrl+"/resource/file";
  url:any;

  constructor(private resourceService:ResourceService, private route: ActivatedRoute) { }

  ngOnInit(): void {
    const id = this.route.snapshot.paramMap.get('id');
    // @ts-ignore
     this.resourceService.getResource(id).subscribe(data =>{
       this.resource =data;
     });

  }
  downloadFile(fileName: any): void {
    console.log("file")
    this.resourceService.downloadFile(fileName).subscribe((file: any) => {
      const blob = new Blob([file], {type: 'application/pdf'});

      this.url = window.URL.createObjectURL(file);
      const link = document.createElement('a');
      link.href = this.url;
      link.download = fileName;
      link.click();
    }, error => {
      console.log(error)
    });
  }


}
