import { Component, EventEmitter, Input, Output } from '@angular/core';
import { getLevelColor, getLevelText } from '../../../../../app.util';
import { RiskItemModel } from '../../../../../classes/risk-item.model';
import { CommonNzModule } from '../../../../../common.nz.module';
@Component({
  selector: 'risk-item-detail',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './risk-item-detail.component.html',
  styleUrl: './risk-item-detail.component.less',
})
export class RiskItemDetailComponent extends RiskItemModel {
  @Input() loading = false;
  @Input() data: Knowledge.RiskItem[] = [];
  @Input() isCompare = false;
  @Input() isSelect = false;
  @Input() selected?: number;

  @Output() selectChange = new EventEmitter<number>();
  onSelect(id: number) {
    if (!this.isSelect) return;
    this.selectChange.emit(id);
  }

  levelText(level: string) {
    return getLevelText(level);
  }

  levelColor(level: string) {
    return getLevelColor(level);
  }

  isDiffProperty(item: Knowledge.RiskItem, index: number, property: keyof Knowledge.RiskItem) {
    if (!this.isCompare) return false;
    if (!this.data[index + 1]) return false;
    return this.data[index + 1][property] !== item[property];
  }

  isDiffTitle(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'title');
  }
  isDiffCategory(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'category');
  }
  isDiffType(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'type');
  }
  isDiffLevel(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'level');
  }
  isDiffRequirement(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'requirement');
  }
  isDiffOtherMeasure(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'otherMeasure');
  }
  isDiffSpecificUnit(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'specificUnit');
  }
  isDiffResponsiblePerson(item: Knowledge.RiskItem, index: number) {
    return this.isDiffProperty(item, index, 'responsiblePerson');
  }

  isDiffRegulationItem(item: Knowledge.RiskItem, index: number, idx: number) {
    if (!this.isCompare) return false;
    const lastRow = this.data[index + 1];
    if (!lastRow) return false;
    if (!lastRow.regulations || !lastRow.regulations.length) return true;
    const currentItem = item.regulations[index];
    const targetItem = lastRow.regulations[idx];
    if (!targetItem && currentItem) return true;
    if (currentItem !== targetItem) return true;
    return false;
  }

  isDiffJobItem(item: Knowledge.RiskItem, index: number, idx: number) {
    if (!this.isCompare) return false;
    const lastRow = this.data[index + 1];
    if (!lastRow) return false;
    if (!lastRow.responsiblePositions || !lastRow.responsiblePositions.length) return true;
    const currentItem = item.responsiblePositions[index];
    const targetItem = lastRow.responsiblePositions[idx];
    if (!targetItem && currentItem) return true;
    if (currentItem !== targetItem) return true;
    return false;
  }

  scrollConfig = {
    x: '2660px',
    y: 'calc(100vh - 324px)',
  };
}
