import { Component } from '@angular/core';
import { horizontalInOutReverse300ms } from '../../../../common.animation';
import { CommonNzModule } from '../../../../common.nz.module';
import { CustomDrawerComponent } from '../../../../shared/custom-drawer/custom-drawer.component';
import { HazardCardComponent } from './card/card.component';
import { HazardDetailComponent } from './detail/detail.component';
import { HazardFormComponent, HazardFormComponentProps } from './hazard-form/hazard-form.component';

export interface HazardDetailConfig {
  visible: boolean;
  data: Hazard.HazardDto;
}

const hazardLevels = ['重大', 'A', 'B', 'C'];
const departments = Array.from({ length: 15 }, (_, i) => i + 1);

const getMockHazard = (index: number): Hazard.HazardDto => {
  return {
    id: index,
    type: 1,
    category: 1,
    level: hazardLevels[Math.floor(Math.random() * hazardLevels.length)],
    name: `隐患问题隐患问题隐患问题${index}`,
    description: '隐患1描述',
    status: index % 3,
    creator: '张三',
    reportingTime: '2024-09-25 12:00',
    responsible: 1,
    responsibleDepartments: departments.slice(0, Math.floor(Math.random() * departments.length)),
    images: Array.from({ length: 11 }).map(() => `https://loremflickr.com/260/150?q=${Math.random()}`),
  };
};

@Component({
  selector: 'app-hazard-tracking',
  standalone: true,
  imports: [CommonNzModule, HazardCardComponent, HazardDetailComponent, CustomDrawerComponent, HazardFormComponent],
  templateUrl: './hazard-tracking.component.html',
  styleUrl: './hazard-tracking.component.less',
  animations: [horizontalInOutReverse300ms],
})
export class HazardTrackingComponent {
  hazards: Hazard.HazardDto[] = [];

  detailConfig: HazardDetailConfig = {
    visible: false,
    data: {} as Hazard.HazardDto,
  };
  formConfig: HazardFormComponentProps = {
    visible: false,
    data: undefined,
  };
  ngOnInit() {
    this.hazards = Array.from({ length: 2 }, (_, i) => getMockHazard(i));
  }
  handleViewDetail(data: Hazard.HazardDto) {
    this.detailConfig.visible = true;
    this.detailConfig.data = data;
  }
  handleCloseDetail() {
    this.detailConfig.visible = false;
  }
  handleAddHazard() {
    this.formConfig.visible = true;
  }
  closeFormDrawer() {
    this.formConfig.visible = false;
  }
}
