import React, { useEffect, useRef, useState } from 'react';
import styles from './lineChartsCommon.less';
import * as echarts from 'echarts';

interface editPros {
  width: number;
  height: number;
  data: any;
}

/**
 * 折线图组件
 * @constructor
 */
const LineChartsCommon: React.FC<editPros> = (props) => {
  const chartRef: any = useRef();
  const { width, height, data } = props;
  const [chartsData, setChartsData] = useState(data);

  const options = {
    color: ['#1890FF'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        // 坐标轴指示器，坐标轴触发有效
        type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
      },
    },
    grid: {
      top: '15%',
      left: '5%',
      right: '0',
      bottom: '8%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        data: [],
        boundaryGap: false,
        axisTick: {
          show: false, // 不显示坐标轴刻度线
        },
        splitLine: {
          show: false,
          lineStyle: {
            type: 'solid',
            color: 'rgba(230, 247, 255, 0.20)',
          },
        },
        axisLine: {
          lineStyle: {
            color: '#27b4c2',
          },
        },
        //x底部文字
        axisLabel: {
          interval: 0,
          rotate: 30,
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        // y轴的分割线
        splitLine: {
          show: true,
          lineStyle: {
            type: 'solid',
            color: 'rgba(230, 247, 255, 0.20)',
          },
        },
      },
    ],
    series: [
      {
        type: 'line',
        smooth: false,
        symbol: 'circle', //拐点设置为实心
        symbolSize: 2, //拐点大小
        itemStyle: {
          borderColor: 'rgba(42,157,255,.2)', //拐点边框颜色
          borderWidth: 10, //拐点边框大小，
          color: '#1890FF',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        lineStyle: {
          width: 3,
          shadowColor: '#1890FF',
          shadowBlur: 20,
        },
        areaStyle: {
          opacity: 1,
          //右下左上
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: 'rgba(24, 144, 255, .5)',
            },
            {
              offset: 0.3,
              color: 'rgba(24, 144, 255, 0.2)',
            },
            {
              offset: 1,
              color: 'rgba(24, 144, 255, 0)',
            },
          ]),
        },
        data: [],
      },
      {
        type: 'lines',
        coordinateSystem: 'cartesian2d',
        symbolSize: 8,
        polyline: true, // 多线段
        effect: {
          show: true,
          period: 5,
          trailLength: 0.3,
          symbolSize: 11,
          symbol: 'circle',
          color: '#0069d9',
        },
        lineStyle: {
          width: 1,
          opacity: 0,
        },
        // 光点
        data: [
          {
            coords: [],
          },
        ],
      },
    ],
  };

  const setCharts = () => {
    const myChart = echarts.init(chartRef.current);
    if (chartsData && chartsData.length > 0) {
      const list: any = [];
      const value: any = [];
      const total: any = [];
      chartsData.forEach((res: { time: string; value: number }) => {
        list.push(res.time);
        value.push(res.value);
        total.push([res.time, res.value]);
      });
      options.xAxis[0].data = list;
      options.series[0].data = value;
      options.series[1].data[0].coords = total;
      myChart.setOption(options, true);
    }
  };

  useEffect(() => {
    setChartsData(data);
    setCharts();
  }, [data]);

  return (
    <div className={styles.container}>
      <div ref={chartRef} style={{ width: `${width}px`, height: `${height}px` }} />
    </div>
  );
};
export default LineChartsCommon;
