import { Component, OnInit } from "@angular/core";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";
import { NzModalService } from "ng-zorro-antd";

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

  searchCriteria = {
    //搜索条件
    id: "", //编码
    target: "order", //类型
  };
  typeList = [
    {
      type: "order",
      name: "工单",
    },
    {
      type: "specimen",
      name: "标本",
    },
    {
      type: "inspect",
      name: "检查",
    },
    {
      type: "drugsBag",
      name: "药包",
    },
    {
      type: "jpBag",
      name: "静配",
    },
  ]; // 类型列表
  hosId;
  ngOnInit() {
    this.getHospital();
  }
  //展开/关闭
  panelClick(e, panel) {
    if (e.target.className == "app-collapse-panel-header") {
      panel.active = !panel.active;
    }
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      id: "",
      target: "order",
    };
    this.getList();
  }

  // 获取所有院区
  getHospital() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList();
  }
  // 查看标本历史记录
  historyPromptModalShow = false; //标本历史记录弹窗开关
  scode = ""; //查看历史记录携带
  viewSpecimenHistory(data) {
    this.scode = data.scode;
    this.historyPromptModalShow = true;
  }
  // 关闭标本历史记录弹窗
  closeModelHistory(e) {
    this.historyPromptModalShow = JSON.parse(e).show;
  }
  isLoading = false;
  // 表格数据
  loading1 = false;
  panels: any = [];
  panelsClone: any = [
    {
      active: true,
      name: "检查列表",
      extra: "inspectList",
      list: [],
    },
    {
      active: true,
      name: "工单信息",
      extra: "order",
      list: [],
    },
    {
      active: true,
      name: "工单日志",
      extra: "orderLogs",
      list: [],
    },
    {
      active: true,
      name: "检查日志",
      extra: "inspectLogs",
      list: [],
    },
    {
      active: true,
      name: "标本列表",
      extra: "specimenList",
      list: [],
    },
    {
      active: true,
      name: "标本日志",
      extra: "specimenLogs",
      list: [],
    },
    {
      active: true,
      name: "药品列表",
      extra: "drugsBagList",
      list: [],
    },
    {
      active: true,
      name: "药包日志",
      extra: "drugsBagLogs",
      list: [],
    },
    {
      active: true,
      name: "药品日志",
      extra: "drugsLogs",
      list: [],
    },
    {
      active: true,
      name: "静配包列表",
      extra: "jpBagList",
      list: [],
    },
    {
      active: true,
      name: "静配包日志",
      extra: "jpBagLogs",
      list: [],
    },
    {
      active: true,
      name: "静配日志",
      extra: "jpLogs",
      list: [],
    },
  ];
  // 获取数据
  getList() {
    this.loading1 = true;
    let id = this.searchCriteria.id || "dsadmin";
    console.log(id);
    this.mainService
      .infoSearch(this.searchCriteria.target, id)
      .subscribe((result) => {
        this.panels = this.panelsClone;
        this.loading1 = false;
        this.panels.forEach((item) => {
          item.list = result[item.extra] || [];
        });
        this.panels = this.panels.filter((item) => item.list.length > 0);
      });
  }
  // json弹窗
  toJson(json) {
    this.modalService.info({
      nzTitle: "JSON格式化",
      nzContent: "<pre>" + JSON.stringify(JSON.parse(json), null, 4) + "</pre>",
      nzWidth: 600,
      nzMaskClosable: true,
    });
  }
}
