<template>
  <!-- 修改为正确的闭合标签 -->
  <div :class="className" :style="{ height: height, width: width }"></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "680px",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: false,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions();
    },
    setOptions() {
      const option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["执行用例总数", "执行用例成功率"],
          // 修改图例颜色
          textStyle: {
            color: "#686868",
          },
        },
        grid: {
          left: "5%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
          // 去掉背景保留背景线条
          backgroundColor: "transparent",
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLabel: {
            textStyle: {
              color: "#686868",
            },
          },
          data: [
            "2025-01-21 10:00:00",
            "2025-01-25 10:00:00",
            "2025-01-29 10:00:00",
            "2025-02-02 10:00:00",
            "2025-02-06 10:00:00",
            "2025-02-10 10:00:00",
            "2025-02-14 10:00:00",
            "2025-02-18 10:00:00",
          ],
        },
        yAxis: [
          {
            type: "value",
            // 去掉坐标轴名称
            name: "",
            min: 0,
            max: 2,
            interval: 0.5,
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "#686868",
              },
            },
          },
          {
            type: "value",
            // 去掉坐标轴名称
            name: "",
            min: 0,
            max: 100,
            interval: 25,
            axisLabel: {
              formatter: "{value}%",
              textStyle: {
                color: "#686868",
              },
            },
          },
        ],
        series: [
          {
            name: "执行用例总数",
            type: "line",
            yAxisIndex: 0, // 使用左边的纵坐标
            data: [0.5, 1.2, 1.8, 1.5, 1.0, 0.8, 1.3, 1.7],
            itemStyle: {
              color: "blue", // 设置为蓝色
            },
          },
          {
            name: "执行用例成功率",
            type: "line",
            yAxisIndex: 1, // 使用右边的纵坐标
            data: [25, 50, 75, 60, 40, 30, 70, 90],
            itemStyle: {
              color: "green", // 设置为绿色
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
