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

@Component({
  selector: 'app-incident-substitution-redeploy',
  templateUrl: './incident-substitution-redeploy.component.html',
  styleUrls: ['./incident-substitution-redeploy.component.less']
})
export class IncidentSubstitutionRedeployComponent implements OnInit {
  @Input() duty:any;
  constructor(
    private mainService: MainService,
    private fb: FormBuilder,
    private tool: ToolService,
  ) { }

  changeInpSubject = new Subject(); //防抖

  ngOnInit() {
    //防抖
    this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
      if(v[0] === 'user'){
        this.getUsers(v[1]);
      } else if(v[0] === 'group'){
        this.getGroups(v[1]);
      }
    });
    this.initForm();
    this.getGroups();
  }

  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initForm() {
    this.validateForm = this.fb.group({
      userGroup: [1, [Validators.required]],//分配方式
      userId: [null],//人
      groupId: [null, [Validators.required]],//组
      redeployRemark: [null, [Validators.required]],//转派原因
    });
    console.log(this.validateForm.controls)
  }

  // 修改组
  changeGroup(id){
    this.validateForm.controls.userId.setValue(null);
    this.getUsers();
  }

  // 选择人|组
  changeUserGroup(id){
    this.validateForm.controls.userId.setValue(null);
    if(id === 1){
      // 人
      this.requiredConfigChange('userId', true);
    }else if(id === 2){
      // 组
      this.requiredConfigChange('userId', false);
    }
  }

  requiredConfigChange(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();
  }

  // 边输边搜节流阀
  isLoading = false;
  changeInp(model, e) {
    this.isLoading = true;
    this.changeInpSubject.next([model, e]);
  }

  // 搜索
  // 获取用户
  userList: any = [];
  getUsers(keyword = '') {
    if((!this.validateForm.value.groupId && this.validateForm.value.userGroup === 1) || this.validateForm.value.userGroup === 0){
      this.userList = [];
      return;
    }
    let postData = {
      user: {
        name: keyword,
        simpleQuery: true,
        groupdata: { id: this.validateForm.value.groupId },
        roleCodes: 'first-line support,second-line support',
        engineer: 1,
      },
      idx: 0,
      sum: 10,
    };
    this.mainService
      .getFetchDataList("data", "user", postData)
      .subscribe((data) => {
        this.isLoading = false;
        let userList = data.list || [];
        this.userList = userList;
      });
  }

  // 获取组
  groupList: any = []; //所有所属科室(搜索)
  getGroups(keyword = '') {
    let postData = {
      group2: {
        groupName: keyword,
        hospitals: this.duty.id,
        type: 3,
      },
      idx: 0,
      sum: 10,
    };
    this.mainService
      .getFetchDataList("data", "group2", postData)
      .subscribe((data) => {
        this.isLoading = false;
        let groupList = data.list || [];
        this.groupList = groupList;
      });
  }
}




