import { Component, OnInit } from "@angular/core";
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
import { PerformanceAllocationService } from './performance-allocation.service';

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

  // 起点科室绩效计算方式
  startCalculate:any = null;
  // 终点科室绩效计算方式
  endCalculate:any = null;
  // 配置
  configs:any = {};
  constructor(private otherPageControlService: PerformanceAllocationService, private tool: ToolService, private msg: NzMessageService) {}

  ngOnInit():void {
    this.getIntegralCalculationMethod();
  }

  //获取绩效计算方式
  grade_calculates:any = [];
  dLoading = false;
  getIntegralCalculationMethod() {
    this.dLoading = true;
    this.otherPageControlService
      .getDictionary("grade_calculate")
      .subscribe((data) => {
        this.dLoading = false;
        this.grade_calculates = data;
        this.getConfig();
      });
  }

  // 切换tab
  tabModal(tabModalName:string){
    this.tabModalName = tabModalName;
  }

  // 保存
  submitForm() {
    if(!this.startCalculate){
      this.msg.create("warning", "请选择起点科室绩效计算方式！");
      return;
    }
    if(!this.endCalculate){
      this.msg.create("warning", "请选择终点科室绩效计算方式！");
      return;
    }
    let postData:any = {
      id: this.configs.id,
      hosId: this.hosId,
      startCalculate: this.startCalculate ? { id: this.startCalculate } : undefined,
      endCalculate: this.endCalculate ? { id: this.endCalculate } : undefined,
    };
    this.btnLoading = true;
    this.otherPageControlService
      .simplePost("addData", "workOrderGradeBase", postData)
      .subscribe((result) => {
        this.btnLoading = false;
        if (result.status == 200) {
          this.getConfig();
        }
      });
  }

  // 获取配置
  getConfig() {
    this.loading = true;
    let postData = {
      idx: 0,
      sum: 10,
      workOrderGradeBase: {
        hosId: this.hosId,
      }
    };
    this.otherPageControlService
      .getConfig(postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list[0] || {};
          this.startCalculate = this.configs.startCalculate ? this.configs.startCalculate.id : undefined;
          this.endCalculate = this.configs.endCalculate ? this.configs.endCalculate.id : undefined;
        }
      });
  }
}



