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-quilt-washing-search",
  templateUrl: "./quilt-washing-search.component.html",
  styleUrls: ["./quilt-washing-search.component.less"],
})

export class QuiltWashingSearchComponent implements OnInit {
  constructor(private mainService: MainService, private tool: ToolService) {}

  searchCriteria = {
    //搜索条件
    batchCode: "", //请领单号
    relatedDept: 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();
  }

  // 展示信息提示框（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);
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      batchCode: "",
      relatedDept: null,
    };
    this.getList(1);
  }

  // 获取所有院区
  getHospital() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList(1);
  }

  // 打开搜索框
  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: {
        searchType: 1,// 简单查询
        cascadeHosId: 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,
      clothesWashing: {
        hosId: this.hosId,
        batchCode: this.searchCriteria.batchCode || undefined,
      },
    };
    if (this.searchCriteria.relatedDept) {
      postData.clothesWashing.relatedDept = this.searchCriteria.relatedDept;
    }
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "clothesWashing", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data["status"] == 200) {
          this.listOfData = data["list"];
          this.listLength = data["totalNum"];
        }
      });
  }
}

