import { Component, OnInit } from "@angular/core";
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { MedicalWastePageControlService } from './medical-waste-page-control.service';

@Component({
  selector: "app-medical-waste-page-control",
  templateUrl: "./medical-waste-page-control.component.html",
  styleUrls: ["./medical-waste-page-control.component.less"],
})
export class MedicalWastePageControlComponent implements OnInit {
  loading:boolean = false; //页面加载的loading
  btnLoading:boolean = false; //提交按钮的loading
  tabModalName:string = 'characteristics'; //当前选中的tab
  hosId = this.tool.getCurrentHospital().id; //当前院区

  // 医废存储科室
  wasteDept:any = null;
  // 回收支持交接方式
  wasteHandoverType:any = null;
  // 配置
  configs:any = {};
  searchTimerSubject = new Subject();
  constructor(private otherPageControlService: MedicalWastePageControlService, private tool: ToolService, private msg: NzMessageService) {}

  ngOnInit():void {
    // todo
    this.searchTimerSubject.pipe(debounceTime(500)).subscribe((v) => {
      let fun = v[0];
      fun.call(this, v[1]);
    });
    this.getTaskTypes();
    this.getDeptHandoverType();
    this.getWasteHandoverType();
    this.getConfig();
  }

  // 边输入边搜索节流阀
  searchTimer(fun, e) {
    this.isLoading = true;
    this.searchTimerSubject.next([fun, e]);
  }

  // 用户输入搜索任务类型
  isLoading: boolean = false;
  taskTypeId:any = null;
  taskTypes:any[] = [];
  changeTasktype(e) {
    this.searchTimer(this.getTaskTypes, e);
  }

  changeDept(e) {
    this.searchTimer(this.getDepts, e);
  }

  //获取任务类型
  getTaskTypes(e:string = '') {
    let postData:any = {
      idx: 0,
      sum: 9999,
      taskType: {
        taskName: e,
        simpleQuery: true,
        hosId: {
          id: this.hosId
        },
        ordinaryField: {
          key: 'ordinary_field',
          value: 'clinicalWaste'
        }
      }
    };
    this.isLoading = true;
    this.otherPageControlService
      .getTaskTypes(postData)
      .subscribe((result) => {
        this.isLoading = false;
        if (result.status == 200) {
          this.taskTypes = result.list || [];
        }
      });
  }

  //获取科室
  wasteDepts:any = [];
  getDepts(e:string = '') {
    this.isLoading = true;
    let postData = {
      idx: 0,
      sum: 9999,
      department: {
        searchType: 1,// 简单查询
        dept: e,
        cascadeHosId: this.hosId,
        deptHandoverType: this.dept_handover_type.find(v => v.value == 'clinicalWaste')
      }
    };
    this.otherPageControlService
      .getDepts(postData)
      .subscribe((result) => {
        this.isLoading = false;
        if (result.status == 200) {
          this.wasteDepts = result.list || [];
        }
      });
  }
  //获取科室交接类型
  dept_handover_type:any = [];
  getDeptHandoverType() {
    this.otherPageControlService
      .getDictionary("dept_handover_type")
      .subscribe((data) => {
        this.dept_handover_type = data;
        this.getDepts();
      });
  }
  //获取批次号生成规则
  waste_handover_type:any = [];
  getWasteHandoverType() {
    this.isLoading = true;
    this.otherPageControlService
      .getDictionary("waste_handover_type")
      .subscribe((data) => {
        this.isLoading = false;
        this.waste_handover_type = data;
      });
  }
  // 切换tab
  tabModal(tabModalName:string){
    this.tabModalName = tabModalName;
  }

  // 保存
  submitForm() {
    if(!this.taskTypeId){
      this.msg.create("warning", "请先配置关联医废任务类型！");
      return;
    }

    if(!this.wasteHandoverType){
      this.msg.create("warning", "请先配置回收支持交接方式！");
      return;
    }

    if(!this.wasteDept){
      this.msg.create("warning", "请先配置医废存储科室！");
      return;
    }

    let postData:any = {
      id: this.configs.id,
      taskType: this.taskTypeId,
      hosId: this.hosId,
      wasteDept: this.wasteDept,
      wasteHandoverType: { id: this.wasteHandoverType },
    };
    this.btnLoading = true;
    this.otherPageControlService
      .simplePost("addData", "taskTypeConfig", postData)
      .subscribe((result) => {
        this.btnLoading = false;
        if (result.status == 200) {
          this.getConfig();
        }
      });
  }

  // 获取配置
  getConfig() {
    this.loading = true;
    let postData = {
      idx: 0,
      sum: 10,
      taskTypeConfig: {
        taskTypeDTO: {
          hosId: {
            id: this.hosId
          },
          ordinaryField: {
            key: 'ordinary_field',
            value: 'clinicalWaste'
          }
        }
      }
    };
    this.otherPageControlService
      .getConfig(postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list[0] || {};
          this.wasteDept = this.configs.wasteDept;
          this.wasteHandoverType = this.configs.wasteHandoverType ? this.configs.wasteHandoverType.id : null;
          this.taskTypeId = this.configs.taskType || null;
        }
      });
  }
}



