import { Component, ElementRef, ViewChild } from '@angular/core';
import * as echarts from 'echarts';
import { getOption } from './getOption';

@Component({
  selector: 'workbench-hazard-statistics',
  standalone: true,
  imports: [],
  templateUrl: './hazard-statistics.component.html',
  styleUrl: './hazard-statistics.component.less',
})
export class HazardStatisticsComponent {
  @ViewChild('chart') chartElement!: ElementRef<HTMLDivElement>;
  @ViewChild('chart2') chart2Element!: ElementRef<HTMLDivElement>;
  @ViewChild('chart3') chart3Element!: ElementRef<HTMLDivElement>;
  @ViewChild('chart4') chart4Element!: ElementRef<HTMLDivElement>;

  chart?: echarts.ECharts;
  chart2?: echarts.ECharts;
  chart3?: echarts.ECharts;
  chart4?: echarts.ECharts;

  ngAfterViewInit() {
    const dom = this.chartElement?.nativeElement;
    const dom2 = this.chart2Element?.nativeElement;
    const dom3 = this.chart3Element?.nativeElement;
    const dom4 = this.chart4Element?.nativeElement;
    if (dom) {
      this.chart = echarts.init(dom);
      this.chart.setOption(getOption({ title: '重大隐患:', titleColor: '#EE1A1A', legend: false }, this.getParams()));
    }
    if (dom2) {
      this.chart2 = echarts.init(dom2);
      this.chart2.setOption(getOption({ title: 'A类隐患:', titleColor: '#F48323', legend: true }, this.getParams()));
    }
    if (dom3) {
      this.chart3 = echarts.init(dom3);
      this.chart3.setOption(getOption({ title: 'B类隐患:', titleColor: '#FFCB13', legend: false }, this.getParams()));
    }
    if (dom4) {
      this.chart4 = echarts.init(dom4);
      this.chart4.setOption(getOption({ title: 'C类隐患:', titleColor: '#0081FF', legend: true }, this.getParams()));
    }
  }

  getParams() {
    const getRandomNumber = (min: number, max: number) => {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    };
    return [
      { name: '已完成', color: '#0081FF', data: getRandomNumber(2, 20) },
      { name: '整治中', color: '#FDBF5E ', data: getRandomNumber(5, 10) },
      { name: '审核中', color: '#8291A9', data: getRandomNumber(2, 5) },
    ];
  }
}
