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 { OtherPageControlService } from './other-page-control.service';

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

  // 多人模式支持
  clinicalMultiplayerMode:any[] = [
    {label:'是否开启',value: 0}
  ];
  // 列表是否显示追加人按钮
  showAppendUser:any[] = [
    {label:'是否开启',value: 0}
  ];
  // 积分计算方式
  clinicalIntegralCalculationMethod:any = null;
  // 执行方式
  clinicalExecutionMethod:any = null;
  // 配置
  configs:any = {};
  searchTimerSubject = new Subject();
  constructor(private otherPageControlService: OtherPageControlService, 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.getAssociationType();
    this.getTaskTypes();
  }

  // 修改是否多人模式支持
  changeMultiplayerMode(e){
    console.log(e);
    if(!e[0].checked){
      this.clinicalIntegralCalculationMethod = null;
      this.clinicalExecutionMethod = null;
      this.clinicalTaskIds = [];
      this.showAppendUser[0].checked = 0;
    }
  }


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

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

  //获取业务类型
  associationType = null;
  getAssociationType() {
    this.loading = true;
    this.otherPageControlService.getDictionary("association_types").subscribe((data) => {
      this.associationType = data.find(v => v.value === 'other');
      this.associationType && this.getIntegralCalculationMethod();
    });
  }

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

  //获取积分计算方式
  clinicalIntegralCalculationMethods:any = [];
  dLoading = false;
  getIntegralCalculationMethod() {
    this.dLoading = true;
    this.otherPageControlService
      .getDictionary("clinical_integral_calculation_method")
      .subscribe((data) => {
        this.dLoading = false;
        this.clinicalIntegralCalculationMethods = data;
        this.getClinicalExecutionMethod();
      });
  }
  //获取执行方式
  clinicalExecutionMethods:any = [];
  getClinicalExecutionMethod() {
    this.dLoading = true;
    this.otherPageControlService
      .getDictionary("clinical_execution_method")
      .subscribe((data) => {
        this.dLoading = false;
        this.clinicalExecutionMethods = data;
        this.getConfig();
      });
  }
  // 切换tab
  tabModal(tabModalName:string){
    this.tabModalName = tabModalName;
  }

  // 保存
  submitForm() {
    if(!this.associationType){
      this.msg.create("warning", "请先配置其他临床服务的业务类型！");
      return;
    }
    if(this.clinicalMultiplayerMode[0].checked && !this.clinicalIntegralCalculationMethod){
      this.msg.create("warning", "请选择积分计算方式！");
      return;
    }
    if(this.clinicalMultiplayerMode[0].checked && !this.clinicalExecutionMethod){
      this.msg.create("warning", "请选择执行方式！");
      return;
    }
    if(this.clinicalMultiplayerMode[0].checked && !this.clinicalTaskIds.length){
      this.msg.create("warning", "请选择关联任务类型！");
      return;
    }
    let postData:any = {
      id: this.configs.id,
      associationType: this.associationType.id,
      clinicalTaskIds: this.clinicalTaskIds.length ? this.clinicalTaskIds.toString() : undefined,
      hosId: this.hosId,
      clinicalMultiplayerMode: this.clinicalMultiplayerMode[0].checked ? 1 : 0,
      clinicalIntegralCalculationMethod: this.clinicalMultiplayerMode[0].checked ? this.clinicalIntegralCalculationMethod : undefined,
      clinicalExecutionMethod: this.clinicalMultiplayerMode[0].checked ? this.clinicalExecutionMethod : undefined,
      showAppendUser: this.showAppendUser[0].checked ? 1 : 0,
    };
    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: {
        hosId: this.hosId,
        associationType: this.associationType.id,
      }
    };
    this.otherPageControlService
      .getConfig(postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list[0] || {};
          this.clinicalMultiplayerMode[0].checked = this.configs.clinicalMultiplayerMode == 1;
          this.showAppendUser[0].checked = this.configs.showAppendUser == 1;
          this.clinicalIntegralCalculationMethod = this.configs.clinicalIntegralCalculationMethod;
          this.clinicalExecutionMethod = this.configs.clinicalExecutionMethod;
          this.clinicalTaskIds = this.configs.clinicalTaskIds ? this.configs.clinicalTaskIds.split(',').map(v => +v) : [];
        }
      });
  }
}



