import {Component, OnInit, Output, EventEmitter, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import {project_type} from '../../project.constants';
import {SystemService} from '../../../system/system.service';
import {ProjectService} from '../../project.service';
import {SelectPersonComponent} from '../../../modal/select-person/select-person.component';
import {WorkService} from '../../../work/work.service';

@Component({
  selector: 'smart-create-track-info',
  templateUrl: './create-track-info.component.html',
  styles: []
})
export class CreateTrackInfoComponent implements OnInit {
  @ViewChild('smartSelectPerson') smartSelectPerson: SelectPersonComponent;
  @Output() done = new EventEmitter<any>();

  timeFormat = 'yyyy-MM-dd';

  title;
  isVisible = false;
  isOkLoading = false;
  validateForm: FormGroup;
  fileList: UploadFile[] = [];
  opr_company_type = project_type;

  cID;

  constructor(private fb: FormBuilder, private systemSer: SystemService,
              private workSer:WorkService,
              private message: NzMessageService, private projectSer: ProjectService) {
  }

  ngOnInit(): void {
    this.initForm();
  }

  initForm() {
    this.validateForm = this.fb.group({
      createUserId: [null, [Validators.required]],
      createUserName: [null, [Validators.required]],
      msg: [null, [Validators.required]],
    });
    this.isOkLoading = false;
  }

  showAddModal(title) {
    this.isVisible = true;
    this.title = title;
  }

  showEditModal(data, title) {
    this.isVisible = true;
    data.type += '';
    this.title = title;
    this.cID = data.id;
    this.validateForm.patchValue(data);
  }

  handleCancel() {
    this.isVisible = false;
    this.initForm();
  }

  handleOk() {
    for (let i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) {
      return false;
    }
    this.isOkLoading = true;
    if (this.title == '添加跟踪信息') {   //运维工作
      this.create();
    }
    if (this.title == '添加配件盘点') {   //项目管理
      this.add();
    }
  }

  //运维工作
  create() {
    this.projectSer.addMaintainProjectTrackMsg(this.validateForm.value).subscribe(
        (res)=>{
          if(res.errCode == 10000){
            this.initForm();
            this.done.emit();
            this.isVisible = false;
            this.isOkLoading = false;
            this.message.success(`${this.title}成功`);
          }
        }
    )
  }

  //备件盘点
  add(){
    this.workSer.addTrackMsg(this.validateForm.value).subscribe(
        (res)=>{
          if(res.errCode == 10000){
            this.initForm();
            this.done.emit();
            this.isVisible = false;
            this.isOkLoading = false;
            this.message.success(`${this.title}成功`);
          }
        }
    )
  }

  //选择人员
  showPeoplemodal() {
    this.smartSelectPerson.showModal('选择人员', null);
  }

  //设置人员
  setPeolpe(e) {
    const d = {
      createUserId: e[0].id,
      createUserName: e[0].name,
    };
    this.validateForm.patchValue(d);
    console.log(e);
  }
}
