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-visit',
  templateUrl: './incident-visit.component.html',
  styleUrls: ['./incident-visit.component.less']
})
export class IncidentVisitComponent 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.getDetail();
    this.getRepairTypeList();
    this.getDeferralDayList();
  }

  tabs:any[] = [
    { name: '报修信息', value: 1 },
    { name: '处理信息', value: 2 },
  ]

  // 点击tab
  activeTabValue:any = 1;
  clickTbab(item){
    this.activeTabValue = item.value;
  }

  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initForm() {
    this.validateForm = this.fb.group({
      handleResult: [null, [Validators.required]],//回访处理结果
      degree: [null, [Validators.required]],//满意度评价
      visitRemarks: [null],//回访备注
    });
    console.log(this.validateForm.controls)
  }

  // 获取回访处理结果
  repairTypeList: any = [];
  getRepairTypeList() {
    this.mainService
    .getDictionary('list', 'incident_closecode')
    .subscribe((data) => {
      this.repairTypeList = data || [];
    });
  }

  // 获取满意度评价
  deferralDayList: any = [];
  getDeferralDayList() {
    this.mainService
    .getDictionary('list', 'incident_degree')
    .subscribe((data) => {
      this.deferralDayList = data || [];
    });
  }

  // 判断工单当前状态
  compareStateValue(incidentData){
    if(incidentData.state.value !== 'close'){
      this.message.warning('工单状态异常！请刷新页面再重试');
      this.hideModal();
    }
  }

  // 事件详情
  incidentData:any = {};
  getDetail(){
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    this.mainService
      .getFetchData("simple/data", "incident", this.id)
      .subscribe((result) => {
        this.message.remove(this.maskFlag);
        this.maskFlag = false;
        this.incidentData = result.data || {};
        this.compareStateValue(this.incidentData);
      });
  }

  // 关闭弹窗
  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 = {
      incident: {
        ...this.incidentData,
        visitRemarks: this.validateForm.value.visitRemarks,
        handleResult: this.validateForm.value.handleResult ? { id: this.validateForm.value.handleResult } : undefined,
        degree: this.validateForm.value.degree ? { id: this.validateForm.value.degree } : undefined,
      },
    }

    console.log(postData);
    // return;

    this.mainService
    .flowPost("incident/task/callback", postData)
    .subscribe((result) => {
      this.message.remove(this.maskFlag);
      this.maskFlag = false;
      this.confirmModelHs.emit();

      if (result.state == 200) {
        this.message.success('回访成功');
      } else {
        this.message.error(result.msg || '回访失败');
      }
    });
  }
}




