import { Component, OnInit, Output, Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { addDays, startOfMinute, format, addMinutes } from 'date-fns';

@Component({
  selector: 'app-edit-inspect-info',
  templateUrl: './edit-inspect-info.component.html',
  styleUrls: ['./edit-inspect-info.component.less']
})

export class EditInspectInfoComponent implements OnInit {
  @Input() date:any;
  @Input() execDeptId:any;
  @Input() remarkText:any;
  @Input() execDeptList:any;
	@Input() extra1:any;
	@Input() inspectAndPatientTransportConfig:any = {};
	@Input() endCheckTime:any = null;
  @Output() submitFormHand = new EventEmitter();
  @Output() cancelFlagHand = new EventEmitter();
  yyDate = null; //预约日期-患者其他服务
  yyTime = null; //预约时间-患者其他服务
  deptId = null;
  remark = '';
  deptList:any[] = [];
  isLoading:boolean = false;
  constructor(
    private message: NzMessageService,
  ) { }
	
  ngOnInit() {
    if(this.date){
      this.yyDate = new Date(this.date);
      this.yyTime = new Date(this.date);
			this.mdData = format(this.yyDate, 'yyyy-MM-dd')
			if(this.endCheckTime){
				this.setTimeData();
			}
    }

    if(this.execDeptList){
      this.deptList = this.execDeptList;
    }

    console.log('this.remarkText:', this.remarkText)
    if(this.remarkText){
      this.remark = this.remarkText;
    }

    if(this.execDeptId){
      let flag = this.deptList.some(v => v.id == this.execDeptId);
      if(flag){
        this.deptId = this.execDeptId;
      }
    }
  }

  // 下一日(患者其他服务)
  nextDay() {
    this.yyDate = addDays(this.yyDate, 1);
  }
	
	// 选择日期
	mdData:any;
	dateChange(e){
		if(e){
			this.mdData = format(e, 'yyyy-MM-dd')
			this.setTimeData();
		}else{
			this.endTime = null
		}
	}
	
	// 选择时间
	endTime:any;
	timeChange(e){
		if(e){
			this.mdData = format(this.yyDate, 'yyyy-MM-dd')
			this.setTimeData();
		}else{
			this.endTime = null
		}
	}
	
	// 设置结束时间
	formEndCheckTime:any;
	setTimeData(){
		if(this.mdData && this.yyTime){
			let num = this.extra1 ? Number(this.extra1) : undefined
			let time = addMinutes(this.yyTime, num ? num : this.inspectAndPatientTransportConfig.yytimeGapMinute)
			let time2 = format(time, 'HH:mm')
			this.endTime = this.mdData +' '+ time2
			let date = new Date(this.endTime);
			this.formEndCheckTime = date.getTime()
		}
	}
	
  // 隐藏模态框
  hideModal() {
    this.cancelFlagHand.emit(false)
  }

  // 表单提交
  submitForm(): void {
    if(!this.deptId) {
      this.message.warning('请选择检查科室！');
      return;
    }
    if(!this.yyDate || !this.yyTime) {
      this.message.warning('请选择完整预约时间！');
      return;
    }
    this.submitFormHand.emit({
      deptId: this.deptId,
      remark: this.remark,
      date: format(this.yyDate, 'yyyy-MM-dd') + ' ' + format(startOfMinute(this.yyTime), 'HH:mm:ss'),
			endCheckTime: this.formEndCheckTime
		});
  }
}

