import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";
import { MainService } from "../../services/main.service";
import { ToolService } from "src/app/services/tool.service";
import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";

@Component({
  selector: "app-view-specimen",
  templateUrl: "./view-specimen.component.html",
  styleUrls: ["./view-specimen.component.less"],
})
export class ViewSpecimenComponent implements OnInit {
  hosId;
  // 切换科室，切换弹窗
  hsLoading = false;
  historySpecimenList: any = [];
  historySpecimenPageIndex: number = 1; //表格当前页码
  historySpecimenPageSize: number = 10; //表格每页展示条数
  historySpecimenListLength: number = 10; //表格总数据量
	changeInpSubject = new Subject(); //防抖
	userId:any = 0;
	tubeTypeId:any = 0;

  @Input() speId: String;

  @Output() closeModelHs = new EventEmitter<any>(); //1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

  constructor(private mainService: MainService, private tool: ToolService) {}

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getHistorySpecimen();
  }
  // 关闭弹窗
  hideModal() {
    this.closeModelHs.emit(JSON.stringify({ show: false })); //emits(向上弹射)事件
  }

	// 重置
	reset(){
		this.userId = 0
		this.tubeTypeId = 0
		this.getHistorySpecimen(true)
	}

	// 选择患者
	userChange(e){
		this.getHistorySpecimen(true)
	}

	// 选择试管类型
	tubeChange(e){
		this.getHistorySpecimen(true)
	}

  // 获取列表数据
	userList:any = [];
	tubeList:any = [];
  packageNo:string = '';
  getHistorySpecimen(idx?) {
    if (idx) {
      this.historySpecimenPageIndex = 1;
    }
    let postData = {
      idx: this.historySpecimenPageIndex - 1,
      sum: this.historySpecimenPageSize,
			patientNo: this.userId!=0 ? this.userId : undefined,
			tubeTypeId: this.tubeTypeId!=0 ? this.tubeTypeId: undefined
    };
    this.hsLoading = true;
    this.mainService
      .getInfoPack(this.speId, postData)
      .subscribe((data:any) => {
        this.hsLoading = false;
        this.packageNo = data.packageNo;
        this.historySpecimenList = data.specimen || [];
        data.patient = data.patient || [];
        data.tubeType = data.tubeType || [];
				data.patient.forEach(i=>{
					if(i.bedNum){
						i.name = i.patientName + '-' + i.bedNum + '-' + i.patientNo
					}else{
						i.name = i.patientName + '-' + i.patientNo
					}
				})
				data.patient.unshift({
					name: '全部',
					patientName:'',
					patientNo: 0,
				})
				this.userList = data.patient || [];
				data.tubeType.forEach(i=>{
					i.name = i.tubeTypeName +'('+ i.specimenNum +')'
				})
				data.tubeType.unshift({
					name: '全部',
					tubeTypeName: '',
					specimenNum: '',
					tubeTypeId: 0
				})
				this.tubeList = data.tubeType || [];
        this.historySpecimenListLength = data.totalNum || 0;
      });
  }
}
