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

@Component({
  selector: 'app-query-range',
  templateUrl: './query-range.component.html',
  styleUrls: ['./query-range.component.less']
})
export class QueryRangeComponent implements OnInit {
  @Output() submitQueryRange = new EventEmitter();
  @Output() cancelQueryRange = new EventEmitter();
  @Input() queryType: number;
  @Input() hospital: any;
  @Input() duty: any;
	@Input() parent: any;
	@Input() hiddenParent: any = false;
  validateForm: FormGroup;//表单
  constructor(
    private fb: FormBuilder,
    private tool: ToolService,
    private mainService: MainService,
  ) { }

  ngOnInit() {
    this.getHospitalList();
    this.queryType == 3 && this.getDutyList(this.hospital.id);
    this.getDutyOneList();
    this.initForm();
  }
  // 隐藏模态框
  hideModal() {
    this.cancelQueryRange.emit()
  }
  // 初始化新增form表单
  initForm() {
    this.validateForm = this.fb.group({
      queryType: [this.queryType, [Validators.required]],
      hospital: [(this.hospital && (this.queryType == 2 || this.queryType == 3)) ? this.hospital.id : null],
      duty: [(this.duty && this.queryType == 3) ? this.duty.id : null],
      dutyOne: [(this.duty && this.queryType == 4) ? this.duty.id : null],
			parent :[this.parent?1:0, [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 queryType = this.validateForm.value.queryType;
		let parent = this.validateForm.value.parent;
    let hospital;
    let duty;
    switch (queryType) {
      case 1:
        break;
      case 2:
        hospital = this.hospitalList.find(v => v.id == this.validateForm.value.hospital);
        break;
      case 3:
        hospital = this.hospitalList.find(v => v.id == this.validateForm.value.hospital);
        duty = this.dutyList.find(v => v.id == this.validateForm.value.duty);
        break;
      case 4:
        hospital = this.dutyOneList.find(v => v.id == this.validateForm.value.dutyOne).parent;
        duty = this.dutyOneList.find(v => v.id == this.validateForm.value.dutyOne);
        break;
    }
    this.submitQueryRange.emit({queryType, hospital, duty, parent});
    this.hideModal();
  }

  // 选择查询范围
  changeQueryType(queryType){
    this.validateForm.controls.hospital.setValue(null);
    this.validateForm.controls.duty.setValue(null);
    this.validateForm.controls.dutyOne.setValue(null);
    this.dutyList = [];
    switch (queryType) {
      case 1:
        this.requiredChange(false, 'hospital');
        this.requiredChange(false, 'duty');
        this.requiredChange(false, 'dutyOne');
        break;
      case 2:
        this.requiredChange(true, 'hospital');
        this.requiredChange(false, 'duty');
        this.requiredChange(false, 'dutyOne');
        break;
      case 3:
        this.requiredChange(true, 'hospital');
        this.requiredChange(true, 'duty');
        this.requiredChange(false, 'dutyOne');
        break;
      case 4:
        this.requiredChange(false, 'hospital');
        this.requiredChange(false, 'duty');
        this.requiredChange(true, 'dutyOne');
        break;
    }
  }

  // 选择院区
  changeHospital(hosId){
    if(this.validateForm.value.queryType == 3){
      this.dutyList = [];
      this.getDutyList(hosId);
    }
  }

  requiredChange(required: boolean, field: string): void {
    if (!required) {
      this.validateForm.get(field)!.clearValidators();
      this.validateForm.get(field)!.markAsPristine();
    } else {
      this.validateForm.get(field)!.setValidators(Validators.required);
      this.validateForm.get(field)!.markAsDirty();
    }
    this.validateForm.get(field)!.updateValueAndValidity();
  }

  // 获取所有的院区
  hospitalList:any[] = [];
  getHospitalList(){
    let postData: any = {
      idx: 0,
      sum: 99999,
      hospital: {
        selectType: "level1",
      },
    };
    this.mainService.getFetchDataList("data", "hospital", postData).subscribe(result => {
      if(result.status == 200){
        this.hospitalList = result.list || [];
      }else{
        this.hospitalList = [];
      }
    });
  }

  // 获取所有的责任部门
  dutyList:any[] = [];
  getDutyList(parentId){
    if(!parentId){
      this.dutyList = [];
      return;
    }
    let postData: any = {
      idx: 0,
      sum: 99999,
      hospital: {
        parent: { id: parentId },
        selectType: "branch",
      },
    };
    this.mainService.getFetchDataList("data", "hospital", postData).subscribe(result => {
      if(result.status == 200){
        this.dutyList = result.list || [];
      }else{
        this.dutyList = [];
      }
    });
  }

  // 获取所有的一级责任部门
  dutyOneList:any[] = [];
  getDutyOneList(){
    let postData: any = {
      idx: 0,
      sum: 99999,
      hospital: {
        selectType: "verticalBranch",
      },
    };
    this.mainService.getFetchDataList("data", "hospital", postData).subscribe(result => {
      if(result.status == 200){
        this.dutyOneList = result.list || [];
      }else{
        this.dutyOneList = [];
      }
    });
  }

}
