import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { MainService } from '../../../services/main.service';
import { Router, ActivatedRoute } from '@angular/router';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';
import { NzMessageService } from 'ng-zorro-antd';
import { ToolService } from 'src/app/services/tool.service';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-incident-setduty',
  templateUrl: './incident-setduty.component.html',
  styleUrls: ['./incident-setduty.component.less']
})
export class IncidentSetdutyComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件
  @Output() confirmModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

  @Input() id: any;

  constructor(
    private mainService: MainService,
    private router: Router,
    private route: ActivatedRoute,
    private message: NzMessageService,
    private tool: ToolService,
    private fb: FormBuilder,
  ) { }

  isLoading = false;

  ngOnInit() {
    this.initForm();
    this.getHospitals(1);
  }

  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initForm() {
    this.validateForm = this.fb.group({
      hosId: [null, [Validators.required]],//院区
      dutyId: [null, [Validators.required]],//责任部门
    });
    console.log(this.validateForm.controls)
  }

  // 修改院区
  changeHospital(id){
    this.validateForm.controls.dutyId.setValue(null);

    this.dutyList = [];
    this.getHospitals(2, id);
  }

  // 获取院区
  hospitalList: any = [];
  dutyList: any = [];
  getHospitals(type:number, parentId?) {
    if(type === 1){
      this.hospitalList = this.tool.getHospitalList().filter(v => !v.parent);
      return;
    }
    if(type === 2 && !parentId){
      this.dutyList = [];
      return;
    }
    let postData = {
      hospital: {
        parent: { id: parentId },
        type: { key: 'hospital_type', value: '6' },
      },
      idx: 0,
      sum: 9999,
    };
    this.mainService
      .getFetchDataList("data", "hospital", postData)
      .subscribe((data) => {
        this.isLoading = false;
        this.dutyList = data.list || [];
      });
  }

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

  // 表单提交
  maskFlag:any = false;
  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) {
      return;
    }

    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;

    let postData = {
      orderId: this.id,
      dutyId: this.validateForm.value.dutyId,
    }

    this.mainService
    .changeIncidentDuty(postData)
    .subscribe((result) => {
      this.message.remove(this.maskFlag);
      this.maskFlag = false;
      this.confirmModelHs.emit();

      if (result.status == 200) {
        this.message.success('设置成功');
      } else {
        this.message.error('设置失败');
      }
    });
  }
}




