 
import { Component, OnInit,  } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import {DownloaderService} from './downloader.service';
import { saveAs } from 'file-saver';
import { ToastrService } from 'ngx-toastr';


@Component({
  selector: 'app-downloader',
  templateUrl: './downloader.component.html',
  styleUrls: ['./downloader.component.css']
})
export class DownloaderComponent implements OnInit {
  private sub: any;
  trankey: any;
  userid: any;
  Filename: any;
  error_message = '';
  progressbar_type: number =1; 	   
  message = '';
  tranid: any;
  key:any;
  constructor( private route: ActivatedRoute, private downloadService: DownloaderService, private toastr: ToastrService) { }

  ngOnInit(): void {  
    this.trankey = this.route.snapshot.queryParams.L;
    this.userid = +this.route.snapshot.queryParams.U;
    this.Filename = this.route.snapshot.queryParams.F;  
    this.tranid = +this.route.snapshot.queryParams.T; 
    this.key = this.route.snapshot.queryParams.K; 
    const fileData = [];  
    fileData.push({
      F: this.Filename,
      U: this.userid ,
      L: this.trankey,
      T: this.tranid,
      K:this.key
    }); 
 this.downloadService.Downloader(fileData[0]).subscribe(res => {
   console.log(res);
       var content = res.body.file;
       let objectURL = 'data:application/octet-stream;base64,' + res.body.file;
      saveAs(objectURL,  res.body.fileName);
     // self.close();
       this.message = 'File downloaded successfully';
		  this.progressbar_type = 2;						  
       var lotid=res.body.lotid;
       this.downloadService.checkin(this.userid,this.key).subscribe(res => {
        window.close();
      });
     
       
   },
   error => {      
     this.error_message = error['error'].message;
     this.progressbar_type = 0;
   });
   }

}
