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

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

  // 送回自主发起科室范围
  clothingBackDeptIds:any = [];
  // 积分计算方式
  clothingDept:any = null;
  // 批次号生成规则
  // batchNoRule:any = null;
  // 配置
  configs:any = {};
  searchTimerSubject = new Subject();
  constructor(private otherPageControlService: QuiltWashingSendPageControlService, 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;
  clothingSendTypeId: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: 'clothingSend'
        }
      }
    };
    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.clothingSendTypeId){
      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.clothingSendTypeId,
      clothingSendTypeId: this.clothingSendTypeId,
      hosId: this.hosId,
      clothingDept: this.clothingDept,
      clothingBackDeptIds: this.clothingBackDeptIds.toString(),
      // batchNoRule: { id: this.batchNoRule },
    };
    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: 'clothingSend'
          }
        }
      }
    };
    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.clothingSendTypeId = this.configs.clothingSendTypeId || null;
          this.clothingBackDeptIds = this.configs.clothingBackDeptIds ? this.configs.clothingBackDeptIds.split(',') : [];
        }
      });
  }
}



