import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { CommonNzModule } from '../../../../../common.nz.module';
import { BasicDataService } from '../../../../../services/basic.service';
import { KnowledgeService } from '../../../../../services/knowledge.service';
import { ScrollImagesComponent } from '../../../../../shared/scroll-images/scroll-images.component';
import { ApiService } from './../../../../../services/api.service';
import { DetailHeaderComponent } from './detail-header/detail-header.component';
import { HandleProgressComponent } from './handle-progress/handle-progress.component';
import { HazardFlowComponent } from './hazard-flow/hazard-flow.component';
import { MeasureTargetComponent } from './measure-target/measure-target.component';

@Component({
  selector: 'hazard-detail',
  standalone: true,
  imports: [
    CommonNzModule,
    DetailHeaderComponent,
    ScrollImagesComponent,
    HandleProgressComponent,
    MeasureTargetComponent,
    HazardFlowComponent,
  ],
  templateUrl: './detail.component.html',
  styleUrl: './detail.component.less',
  host: {
    class: 'custom-scroll-bar',
  },
})
export class HazardDetailComponent {
  @Input() data: Hazard.HazardDto = {} as Hazard.HazardDto;
  @Output() onClose = new EventEmitter<void>();
  @Output() onEdit = new EventEmitter<Hazard.HazardDto>();
  @Output() update = new EventEmitter<number>();
  comment = '';
  auditable = false;
  loading = false;

  constructor(
    private knowledge: KnowledgeService,
    private api: ApiService,
    private message: NzMessageService,
    private basic: BasicDataService
  ) {}

  ngOnChanges(changes: SimpleChanges) {
    if (changes['data']) {
      this.comment = this.data.toUserRemark || '';
      this.auditable = this.data.status === 0;
    }
  }
  handleEdit(data: Hazard.HazardDto) {
    this.onEdit.emit(data);
  }
  async handlePass() {
    this.loading = true;
    await this.api.hazard
      .passHazard(this.data.id, this.comment)
      .then(async () => {
        this.update.emit(this.data.id);
        setTimeout(() => {
          this.message.success('已通过');
          this.loading = false;
        }, 1000);
      })
      .finally(() => {});
  }
  async handleReject() {
    this.loading = true;
    await this.api.hazard.refuseHazard(this.data.id, this.comment).then(async () => {
      this.update.emit(this.data.id);
      setTimeout(() => {
        this.message.success('已拒绝');
        this.loading = false;
      }, 1000);
    });
  }
  get measure() {
    if (this.data.plan) {
      return this.data.plan;
    }
    return {} as Hazard.HazardPlan;
  }
}
