import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MainService } from '../../services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { format, startOfDay, endOfDay } from 'date-fns';
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: 'app-businessData-detail-modal',
  templateUrl: './businessData-detail-modal.component.html',
  styleUrls: ['./businessData-detail-modal.component.less']
})
export class BusinessDataDetailModalComponent implements OnInit {
  // 切换科室，切换弹窗
  hosId;
  hsLoading = false;
  bloodList: any = [];// 血制品列表
  washingExceptionList: any = [];// 被服洗涤异常列表
  specimenList: any = [];// 标本列表
  inspectionRateStatisticsList: any = [];// 科室检查率统计列表
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 10;//表格每页展示条数
  total: number = 0;//表格总数据量
  searchCriteria:any = {
    stype: 0,
    specimenDesc: 0,
		practical: false
  }
  @Input() show: Boolean;
  @Input() orderId: String;
	@Input() orderHosId: String;
  @Input() type: String = '';
  @Input() dataInfo: any;
	@Input() taskTypeValue: String;
  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

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

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.tabType = 1;
    switch(this.type){
      case 'blood':
        this.getBlood(1);
        break;
      case 'specimen':
        this.getSpecimen(1);
        this.getSpecimenType();
        this.getSpecimenDesc();
        break;
      case 'deptInspectionRateStatistics':
        this.getDeptInspectionRateStatistics(1);
        break;
      case 'deptInspectionStatistics':
        this.getDeptInspectionStatistics(1);
        break;
      case 'washingException':
        this.getWashingException(1);
        break;
      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;

    }
		console.log(10010,this.taskTypeValue)
  }
	
	// 查看标本历史记录
	historyPromptModalShow = false; //标本历史记录弹窗开关
	scode = ""; //查看历史记录携带
	viewSpecimenHistory(data) {
	  this.scode = data.scode;
	  this.historyPromptModalShow = true;
	}
	
	// 关闭标本历史记录弹窗
	closeModelHistory(e) {
	  this.historyPromptModalShow = JSON.parse(e).show;
	}
	
  // 关闭弹窗
  hideModal() {
    this.closeModelHs.emit(JSON.stringify({ show: false }));//emits(向上弹射)事件
  }

  // 切换tab栏
  tabType: number = 1; //tab栏
  checkTab(type) {
    this.tabType = type;
  }

  // 切换标本类型下拉框
  selectStype(e){
    console.log(e);
    this.searchCriteria.specimenDesc = 0;
    this.getSpecimenDesc();
    this.getSpecimen(1);
  }

  // 切换标本类型下拉框
  selectDesc(e){
    console.log(e);
    this.getSpecimen(1);
  }
	
	// 选择实际收取
	practicalChange(e){
		this.getSpecimen(1);
	}
	
  // 分页获取数据
  getList(){
    switch(this.type){
      case 'blood':
        this.getBlood();
        break;
      case 'specimen':
          this.getSpecimen();
          break;
      case 'deptInspectionRateStatistics':
        this.getDeptInspectionRateStatistics();
        break;
      case 'deptInspectionStatistics':
        this.getDeptInspectionStatistics();
        break;
      case 'washingException':
        this.getWashingException();
        break;
      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;
    }
  }

  // 获取列表数据
  getBlood(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      "type": "blood",
      "orderId": this.orderId
    }
    this.hsLoading = true;
    this.mainService.checkData(postData).subscribe((data:any) => {
      this.hsLoading = false;
      this.bloodList = data.data ? data.data.all : [];
      this.pageSize = this.total = data.data ? data.data.all.length : 0;
    })
  }

  // 获取标本类型
  specimentTypeList: any[] = [{ id: 0, name: '全部' }];
  getSpecimenType() {
    this.mainService.getDictionary("list", "specimen_type").subscribe((data:any) => {
      let specimentTypeList = [{ id: 0, name: '全部' }];
      this.specimentTypeList = specimentTypeList.concat(data);
    })
  }

  // 获取检验项目
  specimenDescList: any[] = [{ id: 0, name: '', nameNum: '全部' }];
  id:number = 0;
  getSpecimenDesc() {
    let postData = {
      "hosId": this.hosId,
      "gdId": this.orderId,
      "stype": this.searchCriteria.stype || undefined,
    };
    this.mainService.specimenCount(postData).subscribe((data:any) => {
      data.data = data.data || [];
      let specimenDescList:any = [{ id: 0, name: '全部', nameNum: '全部' }];
      specimenDescList[0].nameNum = `全部(${data.total})`;
      this.specimenDescList = specimenDescList.concat( data.data.map(v => ({id: --this.id, name: v[0] || '', nameNum: (v[0] ? v[0].slice(0, 15) + (v[0].length >= 15 ? '...' : '') : '') + `(${v[1]})`})));
    })
  }

  // 获取标本列表
  getSpecimen(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      specimen: {
        orderBy: 'arrive_time desc',
        hosId: this.hosId,
        gdid: this.orderId,
        stype: this.searchCriteria.stype ? { id: this.searchCriteria.stype } : undefined,
        specimenDesc: this.searchCriteria.specimenDesc ? this.specimenDescList.find(v => v.id == this.searchCriteria.specimenDesc).name: undefined,
				actualCollect: this.searchCriteria.practical ? 1 : undefined
			}
    }
    this.hsLoading = true;
    this.mainService.getFetchDataList('simple/data','specimen',postData).subscribe(data => {
      this.hsLoading = false;
      this.specimenList = data.list || [];
      this.total = data.totalNum || 0;
    })
  }

  // 获取被服洗涤异常列表
  getWashingException(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      clothesException: {
        hosId: this.hosId,
        batchId: this.orderId,
      }
    }
    this.hsLoading = true;
    this.mainService.getFetchDataList('simple/data','clothesException',postData).subscribe(data => {
      this.hsLoading = false;
      this.washingExceptionList = data.list || [];
      this.total = data.totalNum || 0;
    })
  }

  // 获取新药品一单一码列表
  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 || {};
        }
      }
    })
  }

  // 获取新药品一单多码列表
  drugsBugMultipleList: any = [];// 药包列表
  workOrder:any = {};// 工单
  getDrugsBugMultiple(idx, drugsModel) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      type: this.type,
      extraType: 'orderInfo',
      orderId: this.orderId,
    }
    this.hsLoading = true;
    this.mainService.transflow(postData, 'extra').subscribe(res => {
      this.hsLoading = false;
      if(res.state == 200){
        if(res.data){
          this.drugsBugMultipleList = res.data.drugsBagList || [];
          this.workOrder = res.data.workOrder || {};
          this.total = this.drugsBugMultipleList.length;

          if(drugsModel === 1){
            // 一单一码
            if(this.drugsBugMultipleList.length){
              this.getDrugsBugSingle(this.drugsBugMultipleList[0].id);
            }else{
              this.message.warning('未查询到药品！')
            }
          }

        }
      }
    })
  }

  // 获取新药品列表
  getDrugsBugList(idx?){
    this.getDrugsBugMultiple(idx, this.configs.drugsModel);
  }

  // 获取新药品配置
  configs:any = {};
  getNewDrugConfig(idx?) {
    this.hsLoading = true;
    let postData = {
      idx: 0,
      sum: 1,
      taskTypeConfig: {
        taskTypeDTO: {
          hosId: {
            id: this.orderHosId ? this.orderHosId : 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] || {};
					console.log(886886,this.configs)
          this.getDrugsBugList(idx);
        }else{
          this.hsLoading = false;
        }
      });
  }

  // 获取科室检查率统计-详情列表
  getDeptInspectionRateStatistics(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData:any = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      canceled: this.dataInfo.canceled ? true : undefined,
      priority: this.dataInfo.priority ? 0 : undefined,
    }
    switch(this.dataInfo.type){
      case 'total':
        postData.deptId = this.dataInfo.data.id;
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        break;
      case 'noCheckCount':
        postData.deptId = this.dataInfo.data.id;
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.reportInsState = 'noCheck';
        break;
      case 'checkCount':
        postData.deptId = this.dataInfo.data.id;
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.reportInsState = 'check';
        break;
      case 'insCount':
        postData.deptId = this.dataInfo.data.id;
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.reportInsState = 'inspect';
        break;
    }
    this.hsLoading = true;
    this.mainService.listMsgByMain("listInspect", postData).subscribe((data: any) => {
      this.hsLoading = false;
      this.inspectionRateStatisticsList = data.list || [];
      this.total = data.totalNum || 0;
    })

  }

  // 获取科室检查统计-详情列表
  getDeptInspectionStatistics(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData:any = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      znDeptInspect: true,
    }
    switch(this.dataInfo.type){
      case 'total':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        break;
      case 'cancel':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.cancel = true;
        break;
      case 'priority':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.priority = 1;
        break;
      case 'noneYY':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.noneYY = true;
        break;
      case 'waitCheck':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.waitCheck = true;
        break;
      case 'doCheck':
        postData.deptId = this.dataInfo.data[10];
        postData.hosId = this.hosId;
        postData.startTime = this.dataInfo.searchDto.dateRange[0] ? format(startOfDay(this.dataInfo.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.endTime = this.dataInfo.searchDto.dateRange[1] ? format(endOfDay(this.dataInfo.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined;
        postData.doCheck = true;
        break;
    }
    this.hsLoading = true;
    this.mainService.listMsgByMain("listInspect", postData).subscribe((data: any) => {
      this.hsLoading = false;
      this.inspectionRateStatisticsList = data.list || [];
      this.total = data.totalNum || 0;
    })

  }

  // 查看业务数据
  businessDataModalShow = false; //业务数据弹窗开关
  businessDataModalType = ''; //业务数据类型
  infoId = ''; //业务数据id
  viewDetail(type, id) {
    if(this.configs.showDrugsBagDetails != 1){
      return;
    }
    this.infoId = id;
    this.businessDataModalType = type;
    this.businessDataModalShow = true;
  }
  // 关闭业务数据弹窗
  closeModelBlood(e) {
    this.businessDataModalShow = JSON.parse(e).show;
  }
}



