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-jp-search",
  templateUrl: "./jp-search.component.html",
  styleUrls: ["./jp-search.component.less"],
})
export class JpSearchComponent implements OnInit {
  constructor(private mainService: MainService, private tool: ToolService) {}

  searchCriteria = {
    //搜索条件
    packid: "", //静配标识
    launch: null, //发药科室
    target: null, //发药科室
    jpstate: null,
  };
  deptList = []; // 院区下的配送人员列表(搜索框)
  hosId;
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量
  changeInpSubject = new Subject();
  ngOnInit() {
    this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
      this.searchDept(v);
    });
    this.getHospital();
  }
  // 搜索类型
  isLoading1 = false;
  types = []; // 类型列表(搜索框)
  searchTypes() {
    this.isLoading1 = true;
    this.mainService.getDictionary("list", "jp_state").subscribe((res) => {
      this.isLoading1 = false;
      this.types = res;
    });
  }
  // 打开搜索框
  changeSearch1(flag) {
    if (flag) {
      this.searchTypes();
    }
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      packid: "",
      launch: null,
      target: null,
      jpstate: null,
    };
    this.getList(1);
  }

  // 获取所有院区
  getHospital() {
    this.hosId = 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(dept) {
    if (!dept) {
      return;
    }
    if (dept === "no") {
      dept = "";
    }
    this.isLoading = true;
    this.changeInpSubject.next(dept);
  }
  // 搜索科室
  searchDept(dept) {
    let postData = {
      department: {
        hospital: { id: this.hosId },
        dept,
      },
      idx: 0,
      sum: 10,
    };
    this.mainService
      .getFetchDataList("data", "department", postData)
      .subscribe((result) => {
        if (result.status == 200) {
          this.isLoading = false;
          this.deptList = result.list;
        }
      });
  }

  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let postData: any = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      jPBag: {
        hosId: this.hosId,
        jpstate: this.searchCriteria.jpstate ? { id: this.searchCriteria.jpstate } : undefined,
      },
    };
    if (this.searchCriteria.target) {
      postData.jPBag.target = { id: this.searchCriteria.target };
    }
    if (this.searchCriteria.launch) {
      postData.jPBag.launch = { id: this.searchCriteria.launch };
    }
    if (this.searchCriteria.packid) {
      postData.jPBag.packid = this.searchCriteria.packid;
    }
    this.loading1 = true;
    this.mainService
      .getFetchDataList("pivas", "jPBag", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data["status"] == 200) {
          this.listOfData = data["list"];
          this.listLength = data["totalNum"];
        }
      });
  }
}
