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

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

  searchCriteria = {
    //搜索条件
    patientName: "",
    inspectCode: "",
    hospital: null,
    workerId: null,
    inspectState: null,
  };
  workerSearch = []; // 院区下的配送人员列表(搜索框)
  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.searchWorker(v);
    });
    this.getAllHospital();
  }
  // 搜索类型
  isLoading1 = false;
  types = []; // 类型列表(搜索框)
  searchTypes() {
    this.isLoading1 = true;
    this.mainService.getDictionary("list", "inspect_state").subscribe((res) => {
      this.isLoading1 = false;
      this.types = res;
    });
  }
  // 打开搜索框
  changeSearch1(flag) {
    if (flag) {
      this.searchTypes();
    }
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      patientName: "",
      inspectCode: "",
      hospital: this.allHospital[0] ? this.allHospital[0]["id"] + "" : null,
      workerId: null,
      inspectState: 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;
  }
  // 打开搜索框
  changeSearch(flag) {
    if (flag) {
      this.changeInp("no");
    }
  }
  // 边输边搜节流阀
  isLoading = false;
  changeInp(worker) {
    if (!worker) {
      return;
    }
    if (worker === "no") {
      worker = "";
    }
    this.isLoading = true;
    this.changeInpSubject.next(worker);
  }
  // 搜索配送人员
  searchWorker(worker) {
    let postData = {
      user: {
        name: worker,
        hospital: { id: this.searchCriteria.hospital },
        usertype: { id: 106 },
      },
      idx: 0,
      sum: 10,
    };
    this.mainService
      .getFetchDataList("data", "user", postData)
      .subscribe((result) => {
        if (result.status == 200) {
          this.isLoading = false;
          this.workerSearch = result.list;
        }
      });
  }

  // 表格数据
  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,
      inspectCode: this.searchCriteria.inspectCode,
      workerId: this.searchCriteria.workerId,
      inspectState: this.searchCriteria.inspectState || undefined,
    };
    this.loading1 = true;
    this.mainService
      .listMsgByMain("listInspect", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data["status"] == 200) {
          this.listOfData = data["list"];
          this.listLength = data["totalNum"];
        }
      });
  }
}
