<template>
  <el-card
    shadow="always"
    class="common-card-container common-card-container-body-no-padding"
  >
    <div slot="header">
      <span class="card-title">电池平均温度</span>
    </div>
    <div style="height: 360px" id="dcpjwdChart" />
  </el-card>
</template>

<style scoped lang="scss"></style>
<script>
import * as echarts from "echarts";
import resize from "@/mixins/ems/resize";
import { formatDate } from "@/filters/ems";
import { batteryAveTemp } from "@/api/ems/dzjk";

export default {
  mixins: [resize],
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.chart = echarts.init(document.querySelector("#dcpjwdChart"));
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    init(siteId,timeRange) {
      this.chart.showLoading();
      const x = [];
      const data1 = [],
        data2 = [];
      const [startTime='', endTime=''] = timeRange;
      batteryAveTemp(siteId,startTime,endTime)
        .then((response) => {
          this.setOption(response?.data?.batteryAveTempList || []);
        })
        .finally(() => {
          this.chart.hideLoading();
        });
    },
    setOption(data) {
      let xdata = [],
        ydata = [];
      data.forEach((element) => {
        xdata.push(element.createDate);
        ydata.push(element.batteryTemp);
      });
      xdata = this.chart.setOption({
        legend: {
          left: "center",
          top: "5",
          itemWidth: 10,
          itemHeight: 5,
          textStyle: {
            fontSize: 9,
          },
        },
        grid: {
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        textStyle: {
          color: "#333333",
        },
        xAxis: { type: "category", data: xdata },
        yAxis: {
          type: "value",
        },
        dataZoom: [
          {
            type: "inside",
            start: 0,
            end: 100,
          },
          {
            start: 0,
            end: 100,
          },
        ],
        series: [
          {
            type: "line",
            name: `电池平均温度`,
            areaStyle: {
              // color:'#FFBD00'
            },
            data: ydata,
          },
        ],
      });
    },
  },
};
</script>
