import { Component, EventEmitter, Input, Output } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { CommonNzModule } from './../../../../../common.nz.module';
import { ApiService } from './../../../../../services/api.service';
import { SettingService } from './../../../../../services/setting.service';
import { BasicInfoComponent } from './basic-info/basic-info.component';
import { DetailInfoComponent } from './detail-info/detail-info.component';

@Component({
  selector: 'inspection-plan-detail',
  standalone: true,
  imports: [CommonNzModule, BasicInfoComponent, DetailInfoComponent],
  templateUrl: './detail.component.html',
  styleUrl: './detail.component.less',
  host: {
    class: 'custom-scroll-bar',
  },
})
export class InspectionPlanDetailComponent {
  @Input() loading = false;
  @Input() data: Hazard.InspectionPlanDetail = {} as Hazard.InspectionPlanDetail;
  @Output() onClose = new EventEmitter<void>();
  @Output() refreshList = new EventEmitter<void>();
  @Output() onEdit = new EventEmitter<Hazard.InspectionPlanDetail>();
  @Output() onFetchDetail = new EventEmitter<number>();

  constructor(
    private setting: SettingService,
    private modal: NzModalService,
    private api: ApiService,
    private message: NzMessageService
  ) {}
  handleEdit() {
    this.onEdit.emit(this.data);
  }
  handleWithdraw() {
    const modalRef = this.modal.warning({
      nzClassName: 'precaution-modal',
      nzTitle: '撤回',
      nzContent: '确定要撤回该计划吗？',
      nzOkText: '撤回',
      nzOnOk: async () => {
        modalRef.updateConfig({
          nzOkLoading: true,
        });
        await this.api.hazard.withdrawPlan(this.data.id).finally(() => {
          modalRef.destroy();
        });
        this.message.success('撤回成功');
        this.refreshList.emit();
        this.onClose.emit();
      },
    });
  }
  fetchDetail(id: number) {
    this.onFetchDetail.emit(id);
  }

  get planBelongToMe() {
    const user = this.setting.user;
    return !!user && this.data.createUserId === user.id;
  }
  get disabledEdit() {
    switch (this.data.status) {
      case '未执行':
      case '制定中':
      case '已制定':
        return false;
      default:
        return true;
    }
  }
}
