import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MainService } from '../../services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { startOfDay, format, addDays, addMinutes } from 'date-fns';
import { NzMessageService } from 'ng-zorro-antd';
import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";
@Component({
  selector: 'app-add-inspect-three-modal',
  templateUrl: './add-inspect-three-modal.component.html',
  styleUrls: ['./add-inspect-three-modal.component.less']
})
export class AddInspectThreeModalComponent implements OnInit {
  // 切换科室，切换弹窗
  listOfDisplayData2: any[] = [];
  currentDept; //当前科室

  hosId;
  hsLoading = false;

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

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

  inspectData:any = {
    date: new Date(),
  }; //检查项目
  changeInspectSubject = new Subject(); //防抖

  ngOnInit() {
    //防抖
    this.changeInspectSubject.pipe(debounceTime(500)).subscribe((v) => {
      this.getInspectList(v[0]);
    });
    this.hosId = this.tool.getCurrentHospital().id;
    this.currentDept = this.tool.getCurrentUserDept();
		console.log(777, this.inspectAndPatientTransportConfig)
  }

  // 确认弹窗
  confirmModal() {
    if(!this.inspectData.id){
      this.message.warning('请选择检查项目！');
      return;
    }
    if(!this.inspectData.execDeptId){
      this.message.warning('请选择检查科室！');
      return;
    }
    if(!this.inspectData.date){
      this.message.warning('请选择预约时间！');
      return;
    }
    if(!this.inspectData.time){
      this.message.warning('请选择预约时间！');
      return;
    }
    this.confirmModelHs.emit(JSON.stringify({ show: false, inspectData: this.inspectData }));//emits(向上弹射)事件
  }

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

  // 下一日
  nextDay() {
    this.inspectData.date = addDays(this.inspectData.date, 1);
  }
	
	// 选择日期
	mdData:any = format(this.inspectData.date, 'yyyy-MM-dd');
	dateChange(e){
		this.mdData = format(e, 'yyyy-MM-dd')
		this.setTimeData();
	}

	// 选择时间
	endTime:any;
	timeChange(e){
		if(e){
			this.setTimeData();
		}
	}
	
	// 设置结束时间
	setTimeData(){
		if(this.mdData && this.inspectData.time){
			let num = this.inspectDTO.extra1 ? Number(this.inspectDTO.extra1) : undefined
			let time = addMinutes(this.inspectData.time, num ? num : this.inspectAndPatientTransportConfig.yytimeGapMinute)
			let time2 = format(time, 'HH:mm')
			this.endTime = this.mdData +' '+ time2
			let date = new Date(this.endTime);
			this.inspectData.endCheckTime = date.getTime()
		}
	}
	
  isLoading = false;
  inspectList: Array<any>; //检查项目列表
  // 获取检查项目
  getInspectList(keyword = '') {
    let postData = {
      dictionary: {
        key: "inspect_check_type",
        keyword: keyword || undefined,
      },
      idx: 0,
      sum: 9999,
    };
    this.isLoading = true;
    this.mainService
      .getFetchDataList("simple/data", "dictionary", postData)
      .subscribe((data) => {
        this.isLoading = false;
        this.inspectList = data.list || [];
      });
  }

  // 边输边搜节流阀
  searchInspect(e) {
    this.isLoading = true;
    this.changeInspectSubject.next([e]);
  }

  // 修改检查项目
  inspectDTO:any = {};
  execDeptList:any[] = [];
  changeInspect(id){
    this.inspectDTO = this.inspectList.find(v => v.id == id);
		console.log(4545, this.inspectDTO)
    if(this.inspectDTO){
			this.setTimeData();
      this.inspectData.id = this.inspectDTO.id;
      this.execDeptList = this.inspectDTO.deptList || [];
      this.inspectData.execDeptId = this.execDeptList.length === 1 ? this.execDeptList[0].id : undefined;
      this.inspectData.sign = this.inspectDTO.extra6 || '';
      this.inspectData.type = this.inspectDTO.extra5DTO ? this.inspectDTO.extra5DTO.name : '';
    }else{
      this.inspectData.id = undefined;
      this.inspectData.execDeptId= undefined;
      this.execDeptList = [];
      this.inspectData.sign = '';
      this.inspectData.type = '';
    }
  }

  // 打开班次
  openInspect(e){
    if(e){
      this.getInspectList();
    }
  }
}



