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

@Component({
  selector: 'app-inspection-address-tag-prompt-modal',
  templateUrl: './inspection-address-tag-prompt-modal.component.html',
  styleUrls: ['./inspection-address-tag-prompt-modal.component.less']
})
export class InspectionAddressTagPromptModalComponent implements OnInit {
  @Output() submitFormHand = new EventEmitter();
  @Output() deptFlagHand = new EventEmitter();
  @Input() deptFlag: boolean = false;//模态框
  @Input() checkedDepIdsModel: any[] = [];//科室ids
  loading = false;//获取科室的loading
  validateForm: FormGroup;//表单
  currentHospital; //当前院区
  constructor(
    private fb: FormBuilder,
    private mainService: MainService,
    private message: NzMessageService,
    private tool: ToolService,
  ) { }

  ngOnInit() {
    this.currentHospital = this.tool.getCurrentHospital();
    this.getTags();
    this.initForm()
  }
  // 隐藏模态框
  hideModal() {
    this.deptFlagHand.emit(false)
    this.initForm()
  }
  // 初始化新增form表单
  initForm() {
    this.validateForm = this.fb.group({
      tagIds: [[], [Validators.required]]
    });
  }
  // 获取标签
  isLoading = false;
  tagList: any = [];
  tagListCopy: any = [];
  getTags() {
    let postData = {
      tag: {
        hosId: this.currentHospital.id,
        modelType: { value: 'inspection' },
      },
      idx: 0,
      sum: 50,
    };
    this.mainService
      .getFetchDataList("simple/data", "tag", postData)
      .subscribe((data) => {
        this.tagList = data.list;
        this.tagListCopy = JSON.parse(JSON.stringify(this.tagList));
        this.isLoading = false;
      });
  }

  // 选择标签
  changeTags(tags){
    if(tags.length){
      let flag = tags.every(v => {
        return this.tagListCopy.map(vv => vv.name).includes(v);
      })

      if(flag){
        return;
      }

      let tagName = tags[tags.length - 1];
      let postData = {
        tag: {
          name: tagName,
          hosId: this.currentHospital.id,
          modelType: { value: 'inspection' },
        }
      }
      this.isLoading = true;
      this.mainService
      .coopData("addData", "tag", postData)
      .subscribe((result) => {
        this.isLoading = false;
        console.log(result);
        if(result.status == 200){
          this.tagListCopy.unshift(result.data)
          console.log(this.tagListCopy);
        } else if (result.status == 10001){
          // 名称重复
          this.tagListCopy.unshift(result.data)
          console.log(this.tagListCopy);
        } else {
          tags.pop();
          this.message.warning(result.msg || '请求数据失败')
        }
      });
    }
  }
  // 表单提交
  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 tagNames = this.validateForm.value.tagIds;
    let tags = this.tagListCopy;
    let tagIds = tagNames.map(name =>{
      return tags.find(v => v.name === name).id;
    })
    this.submitFormHand.emit(tagIds);
  }
}

