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: "img-flex-pdf-viewer-modal",
  templateUrl: "./img-flex-pdf-viewer-modal.html",
  styleUrls: ["./img-flex-pdf-viewer-modal.scss"]
})
export class ImgFlexPdfViewerModal extends FlexModalComponent implements OnInit {

  PDF_URL = "std/refund/viewerImg.htm";

  @Input()
	id:any;

	@Input()
	tablename:any;

	@Input()
	fieldid:any;
	
	@Input()
	fieldname:any;

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

  }

  async preShowModalWin(){
	  
	if (!this.id) {
	  AppComponent.alert({title:"信息提示",text:"ID不允许为空!"});
	  this._concel();
	  return false;
	}

	if (!this.tablename) {
	  AppComponent.alert({title:"信息提示",text:"表名不允许为空!"});
	  this._concel();
	  return false;
	}

	if (!this.fieldid) {
	  AppComponent.alert({title:"信息提示",text:"字段ID不允许为空!"});
	  this._concel();
	  return false;
	}

	if (!this.fieldname) {
	  AppComponent.alert({title:"信息提示",text:"字段名称不允许为空!"});
	  this._concel();
	  return false;
	}

	AppComponent.GobalComponent.setLoading(true);
	FrameRequest.downloadService(this.PDF_URL, {id: this.id,table_name: this.tablename,field_id: this.fieldid,field_name: this.fieldname}).subscribe(res => {
		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
  }

}
