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, FormControl } from '@angular/forms';

@Component({
  selector: 'app-incident-direct-order',
  templateUrl: './incident-direct-order.component.html',
  styleUrls: ['./incident-direct-order.component.less']
})
export class IncidentDicrectOrderComponent 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;
  hosId:any;

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.initForm();
    this.getClosecodes();
    this.getHospitalConfig();
  }

  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initForm() {
    this.validateForm = this.fb.group({
      handleDescription: [null],//解决方案
      closecode: [null, [Validators.required]],//处理结果
    });
    console.log(this.validateForm.controls)
  }

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

  // 获取处理结果列表
  closecodeList:any[] = [];
  getClosecodes(){
    this.mainService.getDictionary("list", "incident_closecode").subscribe((data) => {
      this.closecodeList = data || [];
    });
  }

  // 获取院区配置信息
  itsmSimpleHandle:any = {};//是否简单处理
  getHospitalConfig() {
    let postData = {
      idx: 0,
      sum: 9999,
      hospitalConfig: {
        model: "itsm",
        hosId: this.hosId,
      },
    }
    this.mainService
      .getFetchDataList("simple/data", "hospitalConfig", postData)
      .subscribe((result) => {
        let list = result.list || [];
        this.itsmSimpleHandle = list.find(v => v.key == 'itsmSimpleHandle') || {};
        if(this.itsmSimpleHandle.value != 1){
          this.requiredChange('handleDescription', true);
        }
      });
  }

  // 关闭弹窗
  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.confirmModelHs.emit({
      handleDescription: this.validateForm.value.handleDescription,
      closecode: this.validateForm.value.closecode,
    });//emits(向上弹射)事件
  }
}




