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

  currentUserAccount: any = JSON.parse(localStorage.getItem("user")).user.account; //当前登录人账号
  searchCriteria = {
    //搜索条件
    packid: "", //请领单号
    launch: null, //发药科室
    target: null, //发药科室
    drugsState: 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();
  }
  // 查看流程信息弹窗
  logPromptModalShow = false; //弹窗开关
  drugsBagId = ""; //查看记录携带id
  showLogs(data) {
    this.drugsBagId = data.id;
    this.logPromptModalShow = true;
  }
  // 关闭流程信息弹窗
  closeModelLog(e) {
    this.logPromptModalShow = JSON.parse(e).show;
  }
  // 清空药品
  delModal: boolean = false; //删除模态框
  tipsMsg1: string; //提示框信息
  showDelModal(
    tipsMsg1: string
  ) {
    this.delModal = true;
    this.tipsMsg1 = tipsMsg1;
  }
  // 隐藏删除框
  hideDelModal() {
    this.delModal = false;
  }
  // 确认
  btnLoading: boolean = false; //提交按钮loading状态
  confirmDel() {
    this.btnLoading = true;
    this.mainService
      .coopDataM("drugsBag/emptyData", {"hosId":1,"isEmpty":'ok'})
      .subscribe((data) => {
        this.btnLoading = false;
        this.delModal = false;
        if (data.status == 200) {
          this.showPromptModal("药品清空", true, "");
        } else {
          this.showPromptModal("药品清空", false, "");
        }
      });
  }
  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
    }, 100);
    this.getList(0);
  }
  // 搜索类型
  isLoading1 = false;
  types = []; // 类型列表(搜索框)
  searchTypes() {
    this.isLoading1 = true;
    this.mainService.getDictionary("list", "drug_state").subscribe((res) => {
      this.isLoading1 = false;
      this.types = res;
    });
  }
  // 打开搜索框
  changeSearch1(flag) {
    if (flag) {
      this.searchTypes();
    }
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      packid: "",
      launch: null,
      target: null,
      drugsState: 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,
      drugsBag: {
        hosId: this.hosId,
        drugsState: this.searchCriteria.drugsState ? { id: this.searchCriteria.drugsState } : undefined,
      },
    };
    if (this.searchCriteria.target) {
      postData.drugsBag.target = { id: this.searchCriteria.target };
    }
    if (this.searchCriteria.launch) {
      postData.drugsBag.launch = { id: this.searchCriteria.launch };
    }
    if (this.searchCriteria.packid) {
      postData.drugsBag.packid = this.searchCriteria.packid;
    }
    this.loading1 = true;
    this.mainService
      .getFetchDataList("drugsBag", "drugsBag", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data["status"] == 200) {
          this.listOfData = data["list"];
          this.listLength = data["totalNum"];
        }
      });
  }
}
