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

@Component({
  selector: "app-patient-search",
  templateUrl: "./patient-search.component.html",
  styleUrls: ["./patient-search.component.less"],
})
export class PatientSearchComponent implements OnInit {
  constructor(private mainService: MainService, private tool: ToolService) {}

  searchCriteria = {
    //搜索条件
    patientName: "",
    bedNum: "",
    hospital: null,
    department: null,
  };
  allHospital: any = []; //院区下拉框
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量
  changeInpSubject = new Subject();
  ngOnInit() {
    this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
      this.searchDepartment(v[0], v[1]);
    });
    this.getAllHospital();
  }
  // 选择院区
  changeHospital(id, type) {
    if (type === "search") {
      this.searchCriteria.department = null;
    }
  }
  // 搜索科室
  departmentSearch = []; // 院区下的科室列表(搜索框)
  deptKey = "";
  isLoading = false;
  searchDepartment(dept, type) {
    let data = {
      department: {
        dept,
        hospital: {
          id: this.searchCriteria.hospital,
        },
      },
      idx: 0,
      sum: 10,
    };
    this.mainService
      .getFetchDataList("data", "department", data)
      .subscribe((data) => {
        if (data.status == 200) {
          if (type === "search") {
            if (this.deptKey === dept) {
              this.isLoading = false;
              this.departmentSearch = data.list;
            }
          }
        }
      });
  }
  changeInp(dept, type) {
    if (!dept) {
      return;
    }
    if (dept === "no") {
      dept = "";
    }
    this.deptKey = dept;
    this.isLoading = true;
    this.changeInpSubject.next([dept, type]);
  }
  // 打开搜索框
  changeSearch(flag) {
    if (flag) {
      this.changeInp("no", "search");
    }
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      patientName: "",
      bedNum: "",
      hospital: this.allHospital[0] ? this.allHospital[0]["id"] + "" : null,
      department: null,
    };
    this.getList(1);
  }

  // 获取所有院区
  getAllHospital() {
    this.allHospital = [this.tool.getCurrentHospital()];
    this.searchCriteria.hospital = this.tool.getCurrentHospital().id + "";
    this.getList(1);
  }
  // 查看标本历史记录
  historyPromptModalShow = false; //标本历史记录弹窗开关
  scode = ""; //查看历史记录携带
  viewSpecimenHistory(data) {
    this.scode = data.scode;
    this.historyPromptModalShow = true;
  }
  // 关闭标本历史记录弹窗
  closeModelHistory(e) {
    this.historyPromptModalShow = JSON.parse(e).show;
  }

  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      hosId: this.searchCriteria.hospital,
      patientName: this.searchCriteria.patientName,
      bedNum: this.searchCriteria.bedNum,
      department: this.searchCriteria.department || undefined,
    };
    this.loading1 = true;
    this.mainService.listMsgByMain('listPatient',postData).subscribe((data) => {
      this.loading1 = false;
      if (data["status"] == 200) {
        this.listOfData = data["list"];
        this.listLength = data["totalNum"];
      }
    });
  }
}
