<template>
  <div class="chars-wrapper">
    <!-- <div class="bg">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div> -->
    <div ref="chart" class="pressure-bar-chart"></div>
  </div>
</template>

<script>
export default {
  name: "PressureBarChart",
  props: {
    value: {
      type: Number,
      default: 78,
    },
    max: {
      type: Number,
      default: 100,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handleResize);
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
  watch: {
    value() {
      this.updateChart();
    },
    max() {
      this.updateChart();
    },
  },
  methods: {
    initChart() {
      if (!this.$refs.chart) {
        return;
      }
      this.chart = this.$echarts.init(this.$refs.chart);
      this.updateChart();
    },
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    updateChart() {
      if (!this.chart) {
        return;
      }
      const maxValue = Math.max(this.max, 1);
      const safeValue = Math.min(Math.max(this.value, 0), maxValue);

      this.chart.setOption({
        grid: {
          left: 0,
          right: 40,
          top: 0,
          bottom: 0,
          containLabel: true,
        },
        xAxis: {
          type: "value",
          max: maxValue,
          splitLine: { show: false },
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: { show: false },
        },
        yAxis: [
          {
            show: false,
            type: "category",
            data: [""],
            axisTick: { show: false },
            axisLine: { show: false },
          },
          {
            // show: false,
            type: "category",
            data: [`${this.value}%`],
            axisTick: { show: false },
            axisLine: { show: false },
            axisLabel: {
              color: "#ffffff",
              fontSize: 18,
            },
          },
        ],
        series: [
          {
            type: "bar",
            data: [safeValue],
            barWidth: 14,
            barCategoryGap: 5,
            showBackground: true,
            backgroundStyle: {
              color: "rgba(16, 180, 245, 0.22)",
              borderRadius: 6,
            },
            itemStyle: {
              borderRadius: [0, 6, 6, 0],
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: "#2f7bff" },
                { offset: 0.6, color: "#31c3ff" },
                { offset: 1, color: "#2ef5ff" },
              ]),
            },
            label: {
              show: false,
              position: "right",
              color: "#fff",
              fontSize: 16,
              formatter: ({ value }) => `${value}%`,
            },
          },
        ],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.chars-wrapper {
  width: 100%;
  position: relative;
}
.bg {
  position: absolute;
  width: 100%;
  // height: 152px;
  // display: flex;
  // flex-direction:column;
  // justify-content: space-between;
  padding-top: 5px;

  .item {
    width: 100%;
    height: 40px;
    // background: rgba(130, 202, 255, 0.18);
    background: rgba(130, 202, 255, 0.18);
    margin-bottom: 10px;
  }

  // width: 100%;
  // height: 120px;
}
.pressure-bar-chart {
  position: relative;
  // position:absolute;
  // background: red;
  width: 100%;
  height: 20px;
  // overflow: hidden;
  // background: red;
}
</style>
