import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MainService } from '../../services/main.service';
import { ToolService } from 'src/app/services/tool.service';
@Component({
  selector: 'app-businessData-detail-info-modal',
  templateUrl: './businessData-detail-info-modal.component.html',
  styleUrls: ['./businessData-detail-info-modal.component.less']
})
export class BusinessDataDetailInfoModalComponent implements OnInit {
  // 切换科室，切换弹窗
  hosId;
  hsLoading = false;
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 10;//表格每页展示条数
  total: number = 0;//表格总数据量
  searchCriteria:any = {
    stype: 0,
    specimenDesc: 0,
  }
  @Input() show: Boolean;
  @Input() infoId: String;
  @Input() type: String = '';
  @Input() dataInfo: any;

  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

  constructor(private mainService: MainService, private tool: ToolService) { }

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    switch(this.type){
      case 'drugsJpbag':
        this.getNewDrugConfig(1);
        break;
      case 'drugsWestern':
        this.getNewDrugConfig(1);
        break;
      case 'drugsReturn':
        this.getNewDrugConfig(1);
        break;
      case 'drugsPoison':
        this.getNewDrugConfig(1);
        break;
      case 'drugsHerbal':
        this.getNewDrugConfig(1);
        break;

    }
  }
  // 关闭弹窗
  hideModal() {
    this.closeModelHs.emit(JSON.stringify({ show: false }));//emits(向上弹射)事件
  }

  // 分页获取数据
  getList(){
    switch(this.type){
      case 'drugsJpbag':
        this.getDrugsBugList(1);
        break;
      case 'drugsWestern':
        this.getDrugsBugList(1);
        break;
      case 'drugsReturn':
        this.getDrugsBugList(1);
        break;
      case 'drugsPoison':
        this.getDrugsBugList(1);
        break;
      case 'drugsHerbal':
        this.getDrugsBugList(1);
        break;
    }
  }

  // 获取新药品一单一码列表
  drugsBugSingleList: any = [];// 药品列表
  drugsBag:any = {};// 药包
  getDrugsBugSingle(drugsBagId) {
    let postData = {
      type: this.type,
      extraType: 'drugsInfo',
      drugsBagId,
    }
    this.hsLoading = true;
    this.mainService.transflow(postData, 'extra').subscribe(res => {
      this.hsLoading = false;
      if(res.state == 200){
        if(res.data){
          this.drugsBugSingleList = res.data.drugsList || [];
          this.drugsBag = res.data.drugsBag || {};
        }
      }
    })
  }

  // 获取新药品列表
  getDrugsBugList(idx?){
    this.getDrugsBugSingle(this.infoId);
  }

  // 获取新药品配置
  configs:any = {};
  getNewDrugConfig(idx?) {
    this.hsLoading = true;
    let postData = {
      idx: 0,
      sum: 1,
      taskTypeConfig: {
        taskTypeDTO: {
          hosId: {
            id: this.hosId
          },
          ordinaryField: {
            key: 'ordinary_field',
            value: this.type,
          }
        }
      }
    };
    this.mainService
      .getFetchDataList("simple/data", "taskTypeConfig", postData)
      .subscribe((result) => {
        if (result.status == 200) {
          let list = result.list || [];
          this.configs = list[0] || {};
          this.getDrugsBugList(idx);
        }else{
          this.hsLoading = false;
        }
      });
  }

}



