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-postpone',
  templateUrl: './incident-postpone.component.html',
  styleUrls: ['./incident-postpone.component.less']
})
export class IncidentPostponeComponent 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();
  }

  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initForm() {
    this.validateForm = this.fb.group({
      repairTypeId: [null, [Validators.required]],//延期原因
      deferralDayId: [null, [Validators.required]],//延期天数
      deferralRemark: [null, [Validators.required]],//延期说明
    });
    console.log(this.validateForm.controls)
  }

  // 获取延期原因
  repairTypeList: any = [];
  getRepairTypeList() {
    this.mainService
    .getDictionary('list', 'repair_type')
    .subscribe((data) => {
      this.repairTypeList = data || [];
    });
  }

  // 获取延期天数
  deferralDayList: any = [];
  getDeferralDayList() {
    this.deferralDayList = this.tool.generateNumberArray(1, 15).map(v => ({
      text: v + '天',
      value: v,
    }));
		this.deferralDayList.unshift({
			text: '0.5天',
			value: 0.5,
		})
  }

  // 判断工单当前状态
  compareStateValue(incidentData){
    if(incidentData.state.value !== 'handler'){
      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,
    }

    postData.incident.currentLog = {
      remark: this.validateForm.value.deferralRemark,
      extra1: this.validateForm.value.repairTypeId,
      extra2: this.validateForm.value.deferralDayId,
    }

    this.mainService
    .flowPost("incident/task/overtime", 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 || '操作失败');
      }
    });
  }
}




