import { Component, OnInit } from "@angular/core";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";
import { NzModalService, NzMessageService } from "ng-zorro-antd";
import { startOfDay, endOfDay, format } from 'date-fns';

@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,
    private message: NzMessageService,
  ) {}

  dateRange: any = []; //时间范围

  searchCriteria = {
    //搜索条件
    id: "", //编码
    target: "order", //类型
    busiType: "", //业务标识
  };
  typeList = [
    {
      type: "order",
      name: "工单",
    },
    {
      type: "specimen",
      name: "标本",
    },
    {
      type: "inspect",
      name: "检查",
    },
    {
      type: "drugsBag",
      name: "药包",
    },
    {
      type: "jpBag",
      name: "静配",
    },
    {
      type: "dataSource",
      name: "原始数据",
    },
  ]; // 类型列表
  hosId;
  ngOnInit() {
    this.getHospital();
  }
  //展开/关闭
  panelClick(e, panel) {
    if (e.target.className == "app-collapse-panel-header") {
      panel.active = !panel.active;
    }
  }
  // 日期选择
  onCalendarChangeDate(dateArr){
    if(dateArr.length == 2){
      this.dateRange = [startOfDay(dateArr[0]), endOfDay(dateArr[1])];
    }
  }
  // 重置
  reset() {
    this.dateRange = [];
    this.searchCriteria = {
      //搜索条件
      id: "",
      target: "order",
      busiType: "",
    };
    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: [],
    },
    {
      active: true,
      name: "原始数据日志",
      extra: "dataSourceList",
      list: [],
    },
  ];
  // 修改类型
  changeTarget(e){
    this.searchCriteria.id = '';
    this.searchCriteria.busiType = '';
    this.dateRange = [];
  }
  // 获取数据
  getList() {
    this.loading1 = true;
    console.log(this.dateRange)
    if(this.searchCriteria.target == 'dataSource'){
      // 原始数据
      let postData = {
        content: this.searchCriteria.id,
        busiType: this.searchCriteria.busiType,
        startTime: this.dateRange.length ? format(this.dateRange[0], 'yyyy-MM-dd HH:mm:ss') : undefined,
        endTime: this.dateRange.length ? format(this.dateRange[1], 'yyyy-MM-dd HH:mm:ss') : undefined,
      }
      this.mainService
        .queryDataSource(postData)
        .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);
        });
    }else{
      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,
    });
  }
  // 弹窗
  toSource(string, name) {
    this.modalService.info({
      nzTitle: name,
      nzContent: "<pre>" + string + "</pre>",
      nzWidth: 1000,
      nzMaskClosable: true,
      nzOkText: '复制',
      nzOnOk: () => {
        navigator.clipboard.writeText(string);
        this.message.info(`复制${name}成功`);
      }
    });
  }
}
