import { Component, ElementRef, Output, Input, OnInit, EventEmitter } from '@angular/core';
import { HttpHeaders, HttpRequest, HttpClient  } from "@angular/common/http";
import host from "../../../assets/js/http";
import { NzMessageService } from "ng-zorro-antd";
import { MainService } from '../../services/main.service';
import {
  FormBuilder,
  Validators,
  FormGroup,
  FormControl,
} from "@angular/forms";
import { format, startOfDay, endOfDay, subDays} from "date-fns";
import { ToolService } from "../../services/tool.service";
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'app-pathology-detail',
  templateUrl: './pathology-detail.component.html',
  styleUrls: ['./pathology-detail.component.less']
})
export class PathologyDetailComponent implements OnInit {
	
	@Output() pathologyCancel = new EventEmitter();
	@Input() surgeryId: string;
	@Input() patientId: string;
	@Input() detailId: string;
	
	constructor(
		private http: HttpClient,
		private message: NzMessageService,
		private mainService: MainService,
		private fb: FormBuilder,
		private tool: ToolService,
		private sanitizer: DomSanitizer
	) { }
	
	specimenLookData:any=[
		{
			name:'通用-头颈部-口腔-黏膜活检',
		},
		{
			name:'通用-头颈部-口腔-黏膜活检',
		},
		{
			name:'通用-头颈部-口腔-黏膜活检',
		},
		{
			name:'通用-头颈部-口腔-黏膜活检',
		}
	]
	stepLength:any = 0;
	btnLoading:boolean = false;
	validateForm: FormGroup; //新增/编辑表单
	validateSpecimenForm: FormGroup; //选择标本表单
	validatePrintForm: FormGroup;
	specimenData:any=[]; //标本类型
	isDeptLoading:boolean = false;
	specimenModal:boolean = false; //选择标本状态
	isSpinning:boolean = false; //页面加载状态
	detailsData:any;
	specimenList:any=[]; //已选标本
	placeIndex:any = null;
	systemIndex:any = null;
	apparatusIndex:any = null;
	specimenNameIndex:any = null;
	placeList:any=[]; //位置来源数据
	systemList:any=[]; //系统数据
	apparatusList:any=[]; //器官数据
	specimenNameList:any=[]; //标本名称数据
	promptContent: string; //操作提示框提示信息
	ifSuccess: boolean; //操作成功/失败
	promptInfo: string; //操作结果提示信息
	promptModalShow: boolean; //操作提示框是否展示
	specimenModalShow: boolean; //操作提示框是否展示
	type:any = '未打印';
	notModal:boolean = false;
	hosId:any;
	pathologyLogs:any //日志
	printLoading:boolean = false;
	project:any;
	
  ngOnInit() {
		this.hosId = this.tool.getCurrentHospital().id;
		// this.initForm()
		this.getTaskTypesId()
		this.fetchDataList()
  }
	
	// 补打
	printData:any;
	submitPrint(){
		let that = this
		let data: any = {
			pathologyForm:{
				...this.detailsData,
				surgeryId: this.surgeryId || "",
				patientId: this.patientId || "",
				hosId: this.hosId || "" ,
			},
			deptType:'surgery'
		};
		delete data.pathologyForm.patientDTO
		delete data.pathologyForm.surgeryDeptDTO
		delete data.pathologyForm.surgeryDoctorDTO
		delete data.pathologyForm.specimenGenre
		this.printLoading = true;
		this.mainService
		  .pathologyPrint(data)
		  .subscribe((res:any) => {
				if(res.status==200){
					let arr = res.data;
					if(arr.applyBarCode){
						arr.applyBarCode = this.sanitizer.bypassSecurityTrustResourceUrl(arr.applyBarCode)
					}
					for(let i of arr.specimenList){
						i.applyCode = arr.applyCode?arr.applyCode:data.pathologyForm.applyCode
						i.barCode = this.sanitizer.bypassSecurityTrustResourceUrl(i.barCode)
					}
					that.printLoading = false;
					that.printData = arr;
					setTimeout(() => {
					  const printContent = document.getElementById("report");
					  const WindowPrt = window.open("", "", "width=100,height=1000");
						WindowPrt.document.body.innerHTML = printContent.innerHTML;
						setTimeout(_=>{
							WindowPrt.document.close();
							WindowPrt.focus();
							WindowPrt.print();
							WindowPrt.close();
							that.hideModal();
						},500)
					}, 500);
				}else{
					this.message.error(res.msg)
				}
		  });
	}
	
	// 返回
	closeDetailMoadl(){
		this.pathologyCancel.emit()
	}
	
	// 获取任务类型（病理标本）
	taskTypeData:any;
	getTaskTypesId() {
		this.mainService.getDictionary("list", "ordinary_field").subscribe((data) => {
			this.taskTypeData = data.find(i=>i.value=='pathology')
			this.getPathologySys();
		});
	}
	
	// 获取标本配置
	pathologyBarCode:any = 1;
	surgeryPathologyLabelSize:any = 1;
	getPathologySys(){
		let postData = {
			  idx: 0,
			  sum: 10,
			  taskTypeConfig: {
					hosId:this.hosId,
					taskType:this.taskTypeData.id
			  }
		}
	
		this.mainService
		  .getFetchDataList("simple/data", "taskTypeConfig", postData)
		  .subscribe((result) => {
		    if (result.status == 200) {
					this.pathologyBarCode = result.list[0].pathologyBarCode?result.list[0].pathologyBarCode:1;
					if(result.list[0].surgeryPathologyLabelSize){
						this.surgeryPathologyLabelSize = result.list[0].surgeryPathologyLabelSize.value;
					}
				}
		  });
	}
	
	// 获取详情
	statusValue:any;
	fetchDataList(){
		let data = {
		  idx: 0,
		  sum: 9999,
		  pathologyForm: {
				surgeryId: this.surgeryId || "",
		    patientId: this.patientId || "",
		    hosId: this.hosId || "" ,
		  },
		};
		this.isSpinning = true;
		// this.mainService
		//   .getFetchDataList("data", "pathologyForm", data)
			this.mainService.getFetchData("data", "pathologyForm", this.detailId)
		  .subscribe((data) => {
		    this.isSpinning = false;
		    this.detailsData = data.data;
				if(this.detailsData.status){
					this.statusValue = this.detailsData.status.value
				}
				this.pathologyLogs = data.data.formLogDTOS
				this.stepLength = this.pathologyLogs.length
				if(this.detailsData.pathologyInspectDTOS){
					this.project = this.detailsData.pathologyInspectDTOS.map(i=>{
						return i.inspectProject.name
					})
					this.project = this.project.join('、')
				}
				this.getSampleData()
		  });
	}
	
	// 获取标本列表
	getSampleData(){
		let data={
			idx: 0,
			sum: 9999,
			pathologySpecimen:{
				pathologyFormId:this.detailsData.id
			}
		}
		this.mainService
		  .getFetchDataList("data", "pathologySpecimen", data)
		  .subscribe((res) => {
		    this.specimenList = res.list
		  });
	}
	
	hideNotModal(){
		this.notModal = false
	}
	
	// 关闭查看标本
	hideSpecimenModal(){
		this.specimenViewDialog = false
	}
	
	// 查看标本
	specimenViewDialog:any = false;
	viewData:any = [];
	specimenItem:any;
	specimenView(item){
		this.viewData = []
		this.specimenViewDialog = true
		this.specimenItem = item;
		this.viewData.push(item)
	}
	
	// 关闭标本
	closeSpecimenModel(){
		this.getSampleData()
		this.hideSpecimenModal()
	}
	
	closeModel(){
		this.hideModal()
	}
	hideModal(){
		this.pathologyCancel.emit()
	}
	
	// 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
	showPromptModal(con, success, promptInfo?) {
	  this.promptModalShow = false;
	  this.promptContent = con;
	  this.ifSuccess = success;
	  this.promptInfo = promptInfo;
	  setTimeout(() => {
	    this.promptModalShow = true;
	  }, 100);
	}
	
	showSpecimenModal(con, success, promptInfo?) {
	  this.specimenModalShow = false;
	  this.promptContent = con;
	  this.ifSuccess = success;
	  this.promptInfo = promptInfo;
	  setTimeout(() => {
	    this.specimenModalShow = true;
	  }, 100);
	}
}

