import { Component, EventEmitter, Input, Output } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { getPositionLikeDtoText } from '../../../../../../../shared/compose-position-pick-modal/compose-position-pick-modal.component';
import { CommonNzModule } from './../../../../../../../common.nz.module';
import { ApiService } from './../../../../../../../services/api.service';
import { BasicDataService } from './../../../../../../../services/basic.service';
import { KnowledgeService } from './../../../../../../../services/knowledge.service';
import { SettingService } from './../../../../../../../services/setting.service';
import { RoundFillFormComponent, RoundFillFormData } from './fill-form/fill-form.component';

@Component({
  selector: 'plan-round-static-table',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './round-static-table.component.html',
  styleUrl: './round-static-table.component.less',
})
export class RoundStaticTableComponent {
  @Input() list: Hazard.InspectionPlanHandleDto[] = [];
  @Input() planFillable = false;
  @Output() fetchDetail = new EventEmitter<void>();

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

  getPositionText(item: Hazard.InspectionPlanHandleDto) {
    return getPositionLikeDtoText(item, this.basic.lines, this.basic.positions, this.basic.positionCategories);
  }
  getSingleRiskItemText(id: number) {
    return this.knowledge.getRiskItemTitle(id);
  }
  getRiskItemText(item: Hazard.InspectionPlanHandleDto) {
    return item.targets.map((id, index) => `${index + 1}. ${this.getSingleRiskItemText(id)}`).join('\n');
  }
  getTeamName(id: number) {
    return this.basic.getTeamName(id);
  }
  getResponsibleName(item: Hazard.InspectionPlanHandleDto) {
    const id = item.leaderUserId;
    return this.setting.usersOfCurrentCompany.find(u => u.id === id)?.name || '';
  }

  handleFill(item: Hazard.InspectionPlanHandleDto) {
    this.createFillForm(item, 'create');
  }
  handleEdit(item: Hazard.InspectionPlanHandleDto) {
    this.createFillForm(item, 'update');
  }

  getTaskFillPriority(task: Hazard.InspectionPlanHandleDto) {
    if (!this.planFillable) {
      return false;
    }
    if (this.setting.isAdmin) {
      return true;
    }
    if (this.setting.user!.id === task.leaderUserId) {
      return true;
    }
    return false;
  }

  createFillForm(item: Hazard.InspectionPlanHandleDto, action: 'create' | 'update') {
    const modalRef = this.modal.create<RoundFillFormComponent, RoundFillFormData>({
      nzClassName: 'precaution-modal no-body-yp',
      nzTitle: `排查结果上报 - ${this.getPositionText(item)}`,
      nzContent: RoundFillFormComponent,
      nzWidth: '600px',
      nzData: {
        item,
        setSize: (size: 'large' | 'default') => {
          modalRef.updateConfig({
            nzWidth: size === 'large' ? '900px' : '600px',
          });
        },
        action,
        updateDetail: () => {
          this.fetchDetail.emit();
        },
      },
      nzFooter: null,
      nzOkText: '提交',
    });
  }
  toHazardListPage(task: Hazard.InspectionPlanHandleDto) {
    const handlers = task.checkOpinions || [];
    const queries = handlers.map(item => `ids=${item.trouble.id}`).join('&');
    window.open(`/manager/potential-hazard/hazard-tracking?${queries}`);
  }
  onSubmit(res: Hazard.ReportTaskParams) {
    return this.api.hazard.reportTask(res);
  }
}
