import { Component, OnInit,Input} from "@angular/core";
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexModalComponent } from "../../../../shared/common/FlexModal.component";
import { AppComponent } from "../../../../app.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: "by-files-pdf-viewer-modal",
  templateUrl: "./by-files-pdf-viewer-modal.html",
  styleUrls: ["./by-files-pdf-viewer-modal.scss"]
})
export class ByFilesPdfViewerModal extends FlexModalComponent implements OnInit {

 
//	let checkedArray = this.framePager.getCheckedArray((item)=>item.id);
  @Input()
  pdfCheckedArray: any;
  @Input('ids')
	ids: any; 
	@Input()
  PDF_URL: any;

  pdfurl:any;
  pdfname:any;
  constructor(
	public bsModalService: BsModalService,
	private sanitizer: DomSanitizer
  ) {
    super(bsModalService);
  }
  ngOnInit() {

  }

  async preShowModalWin(){
	let checkedArray=[];
	if(this.ids) {
		checkedArray.push(this.ids);
	} else {
		this.pdfCheckedArray.forEach(item => {
			checkedArray.push(item.id);
		});
	}
	if (checkedArray.length != 1) {
	  AppComponent.alert({title:"信息提示",text:"请选择一条档案信息!"});
	  this._concel();
	  return false;
	}
	
	AppComponent.GobalComponent.setLoading(true);
	if(!this.PDF_URL){
		this.PDF_URL = "by/byFiles/download.htm";
	}

	FrameRequest.downloadService(this.PDF_URL, {id: checkedArray.join()}).subscribe(res => {
		debugger
		let content = res.headers.get('content-disposition');
		this.pdfname = content.substring(content.indexOf("=")+1,content.length);
		let blob = new Blob([res.json()], { type: "application/pdf" });
		let objectUrl = URL.createObjectURL(blob);
		this.pdfurl = this.sanitizer.bypassSecurityTrustResourceUrl(objectUrl); 
		AppComponent.GobalComponent.setLoading(false);
	  });
    return true
  }

}
