<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
  name: "multiple-bar-chart",
  componentName: "multiple-bar-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 传入数据
    list: {
      type: Array,
      default: () => [
        {
          title: "日发电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 1,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 1,
            },
            {
              text: "5日",
              value: 1,
            },
            {
              text: "6日",
              value: 1,
            },
            {
              text: "7日",
              value: 1,
            },
            {
              text: "8日",
              value: 1,
            },
            {
              text: "9日",
              value: 1,
            },
            {
              text: "10日",
              value: 1,
            },
            {
              text: "11日",
              value: 1,
            },
            {
              text: "12日",
              value: 1,
            },
          ],
        },
        {
          title: "上网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
        {
          title: "购网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
        {
          title: "风速",
          yAxisIndex: 1,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
      ],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["(万kWh)", "(风速)"],
    },
    // 自定义tooltip 显示内容
    customerTooltip: {
      type: Boolean,
      default: true,
    },
    // 颜色
    color: {
      type: Array,
      default: () => ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    xdata() {
      let result = [];
      if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
        result = this.list[0].value.map((t) => {
          return t.text;
        });
      }
      return result;
    },
    ydata() {
      let result = [];
      this.units.forEach((value, index) => {
        let data = null;
        if (index == 0) {
          data = {
            type: "value",
            name: value,
            axisLabel: {
              formatter: "{value} ",
              fontSize: 14,
            },
            //分格线
            splitLine: {
              lineStyle: {
                color: "#5a6162",
                type: "dashed",
              },
            },
          };
        } else {
          data = {
            type: "value",
            name: value,
            axisLabel: {
              formatter: "{value}",
              fontSize: 14,
            },

            //分格线
            splitLine: {
              show: false,
            },
          };
        }

        result.push(data);
      });

      return result;
    },
    series() {
      let result = [];
      if (this.list && this.list.length > 0) {
        this.list.forEach((value, index) => {
          result.push({
            name: value.title,
            type: "bar",
            yAxisIndex: value.yAxisIndex,
            data: value.value.map((t) => {
              return t.value;
            }),
            itemStyle: {
              color: partten.getColor("gray") + 50,
            },
            emphasis: {
              itemStyle: {
                color: this.color[index],
              },
            },
          });
        });
      }
      return result;
    },
  },
  emits: {
    tooltip: function(param, callback) {
      var color = ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"];
      var result = param[0].axisValue;
      param.forEach((value, index) => {
        result += "<br />" + `<span style="display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:${color[index]};"></span>` + value.seriesName + "：" + value.value;
      });
      callback(result);
      return true;
    },
  },
  methods: {
    initChart() {
      let that = this;
      let chart = echarts.init(this.$el);

      let option = {
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: 14,
          },
        },
        grid: {
          top: util.vh(32),
          left: util.vh(40),
          right: this.ydata.length > 1 ? util.vh(40) : util.vh(16),
          bottom: util.vh(24),
        },
        xAxis: [
          {
            type: "category",
            data: this.xdata,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              fontSize: 14,
            },
          },
        ],
        yAxis: this.ydata,
        series: this.series,
      };

      if (that.customerTooltip) {
        option.tooltip.formatter = function(param) {
          let result = "";
          let callback = function(value) {
            result = value;
          };
          that.$emit("tooltip", param, callback);
          return result;
        };
      }

      chart.setOption(option);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$nextTick(() => {
      this.$el.style.width = this.width;
      this.$el.style.height = this.height;
      this.initChart();
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
};
</script>

<style lang="less">
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>
