import { Component, OnInit, Output, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { EventEmitter } from '@angular/core';
import { ToolService } from '../../services/tool.service';

@Component({
  selector: 'app-select-hospital',
  templateUrl: './select-hospital.component.html',
  styleUrls: ['./select-hospital.component.less']
})
export class SelectHospitalComponent implements OnInit {
  @Output() submitFormHand = new EventEmitter();
  @Output() hosFlagHand = new EventEmitter();
  @Input() hosFlag: boolean = false;//模态框
  validateForm: FormGroup;//表单
  allHospital: any;//所有院区
  hosLoading: boolean = false;//确定按钮的loading
  constructor(private fb: FormBuilder, private tool: ToolService) { }

  ngOnInit() {
    this.allHospital = this.tool.getHospitalList();
    this.initForm()
  }
  // 隐藏模态框
  hideModal() {
    this.hosFlagHand.emit(false)
    this.initForm()
  }
  // 初始化新增form表单
  initForm() {
    this.validateForm = this.fb.group({
      affiliatedHospital: [this.tool.getCurrentHospital().id + '', [Validators.required]]
    });
  }
  // 表单提交
  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty({ onlySelf: true });
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) return;
    let id = this.validateForm.value.affiliatedHospital;
    this.hosLoading = true;
    this.submitFormHand.emit(id);
    this.hideModal();
  }
}
