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 { QuiltWashingGetPageControlService } from './quilt-washing-get-page-control.service';

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

  // 积分计算方式
  clothingDept:any = null;
  // 批次号生成规则
  batchNoRule:any = null;
  // 特定科室无需填写回收数量
  noFillInQuantityIds:any = [];
  // 配置
  configs:any = {};
  searchTimerSubject = new Subject();
  constructor(private otherPageControlService: QuiltWashingGetPageControlService, 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.getDepts();
    this.getClothingBatchnoRule();
    this.getConfig();
  }

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

  // 用户输入搜索任务类型
  isLoading: boolean = false;
  clothingGetTypeId: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: 'clothingGet'
        }
      }
    };
    this.isLoading = true;
    this.otherPageControlService
      .getTaskTypes(postData)
      .subscribe((result) => {
        this.isLoading = false;
        if (result.status == 200) {
          this.taskTypes = result.list || [];
        }
      });
  }

  //获取科室
  clothingDepts:any = [];
  getDepts(e:string = '') {
    this.isLoading = true;
    let postData = {
      idx: 0,
      sum: 9999,
      department: {
        searchType: 1,// 简单查询
        dept: e,
        cascadeHosId: this.hosId,
      }
    };
    this.otherPageControlService
      .getDepts(postData)
      .subscribe((result) => {
        this.isLoading = false;
        if (result.status == 200) {
          this.clothingDepts = result.list || [];
        }
      });
  }
  //获取批次号生成规则
  clothing_batchno_rule:any = [];
  getClothingBatchnoRule() {
    this.isLoading = true;
    this.otherPageControlService
      .getDictionary("clothing_batchno_rule")
      .subscribe((data) => {
        this.isLoading = false;
        this.clothing_batchno_rule = data;
      });
  }
  // 切换tab
  tabModal(tabModalName:string){
    this.tabModalName = tabModalName;
  }

  // 保存
  submitForm() {
    if(!this.clothingGetTypeId){
      this.msg.create("warning", "请先配置被服洗涤-回收任务类型！");
      return;
    }

    if(!this.clothingDept){
      this.msg.create("warning", "请先配置被服洗涤科室！");
      return;
    }

    if(!this.batchNoRule){
      this.msg.create("warning", "请先配置批次号生成规则！");
      return;
    }

    let postData:any = {
      id: this.configs.id,
      taskType: this.clothingGetTypeId,
      clothingGetTypeId: this.clothingGetTypeId,
      hosId: this.hosId,
      clothingDept: this.clothingDept,
      batchNoRule: { id: this.batchNoRule },
      noFillInQuantityIds: this.noFillInQuantityIds.toString(),
    };
    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: 'clothingGet'
          }
        }
      }
    };
    this.otherPageControlService
      .getConfig(postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list[0] || {};
          this.clothingDept = this.configs.clothingDept;
          this.batchNoRule = this.configs.batchNoRule ? this.configs.batchNoRule.id : null;
          this.clothingGetTypeId = this.configs.clothingGetTypeId || null;
          this.noFillInQuantityIds = this.configs.noFillInQuantityIds ? this.configs.noFillInQuantityIds.split(',') : [];
        }
      });
  }
}



