import { Component, OnInit } from "@angular/core";
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
import { DrugPageControlService } from './drug-page-control.service';

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

  // 是否显示关联批次药品
  drugsBatchInfo:any[] = [
    {label:'是否开启',value: 0}
  ];
  // 配置
  configs:any = {};
  // 任务类型
  tasktype:any = {};
  constructor(
    private tool: ToolService,
    private msg: NzMessageService,
    private drugPageControlService: DrugPageControlService,
    ) {}

  ngOnInit():void {
    // todo
    this.getTaskType();
  }

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

  // 保存
  submitForm() {
    if(!this.tasktype.id){
      this.msg.create("warning", "请先配置药品任务类型！");
      return;
    }

    this.btnLoading = true;
    this.drugPageControlService
      .submitForm({
        configs: this.configs, 
        tasktype: this.tasktype, 
        hosId: this.hosId, 
        drugsBatchInfo: this.drugsBatchInfo,
      })
      .subscribe((result) => {
        this.btnLoading = false;
        if (result.status == 200) {
          this.getTaskType();
        }
      });
  }

  //获取任务类型
  getTaskType() {
    this.loading = true;
    this.drugPageControlService
      .getTaskType({
        hosId: this.hosId,
      })
      .subscribe((result) => {
        if (result.status == 200) {
          this.tasktype = result.list[0] || {};
          this.getConfig();
        }else{
          this.loading = false;
        }
      });
  }
  // 获取配置
  getConfig() {
    this.drugPageControlService
      .getConfig({
        tasktype: this.tasktype,
        hosId: this.hosId,
      })
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list;

          this.drugsBatchInfo[0].checked = this.configs.drugsBatchInfo == 1;
        }
      });
  }
}



