<template>
  <div class="new-bar">
    <div :id="index" class="base">基础柱状图</div>
    <div
      class="zuan"
      v-show="config.zuanFlag && config.zuanLevel > 0"
      :style="`color:${option.zuanFontColor}`"
    >
      <i
        class="el-icon-back"
        @click="onZuanBack"
        style="cursor: pointer; margin-right: 10px"
      >
      </i>
      {{ config.zuanStack.join(` ${option.zuanFenge} `) }}
    </div>
  </div>
</template>
<script>
import {
  legend,
  title,
  grid,
  countLabel,
  countLegendSelect,
  countXlabel,
} from "../../newStyles/styleCommon";
export default {
  name: "new-bar",
  props: ["option", "config", "modalSet", "index", "site"],
  data() {
    return {
      originData: [],
      chartOption: {
        tooltip: {
          trigger: "axis",
          axisPointer: { type: "shadow" },
        },
        legend: { data: [] },
        grid: {},
        xAxis: {
          type: "category",
          data: [],
        },
        yAxis: [{ type: "value" }],
        series: [],
      },
      chart: null,
    };
  },
  watch: {
    site: {
      handler() {
        if (this.chart) {
          this.chart.resize();
        }
      },
      deep: true,
    },
    option: {
      handler() {
        if (this.originData.length) {
          this.refresh();
        }
      },
      deep: true,
    },
  },
  methods: {
    handleClick(event) {
      if (this.config.zuanLevel + 1 >= this.config.zuan.length) {
        return this.$message({
          type: "warning",
          message: "已经钻取到最后一层了",
        });
      }
      let key = this.modalSet.find((i) => i.zuan).key;
      let index = this.config[key].findIndex((i) => i.zuan);
      this.config.zuanStack.push(event.data.name[index]);
      this.config.zuanLevel += 1;
      this.$emit("preview");
    },
    onZuanBack() {
      if (this.config.zuanLevel <= 0) return;
      this.config.zuanLevel -= 1;
      this.config.zuanStack = this.config.zuanStack.slice(
        0,
        this.config.zuanLevel
      );
      this.$emit("preview");
    },
    refresh() {
      let { dimension, measure } = this.config;
      dimension = [...dimension].filter((i) => !i.extJson.hidden);
      measure = measure.filter((i) => !i.extJson.hidden);
      if (this.config.zuanFlag) {
        let zuanField = this.config.zuan[this.config.zuanLevel];
        let zuanIndex = dimension.findIndex((i) => i.zuan);
        dimension.splice(zuanIndex, 1, zuanField);
      }
      let { barWidth, barSwitchBg, barBgColor, barBorderRadius } = this.option;
      this.chartOption.grid = grid(this.option);
      if (this.option.isTitleShow) {
        this.chartOption.title = title(this.option);
      }
      //图例
      this.chartOption.legend = legend(this.option);
      let clearData = this.originData.map((i) => {
        let item = { key: [], value: [] };
        dimension.reduce((r, d) => {
          r.key.push(i[d.columnTitle]);
          return r;
        }, item);
        measure.reduce((r, d) => {
          r.value.push(parseFloat(i[d.columnTitle] || 0));
          return r;
        }, item);
        return item;
      });
      let chartOption = { ...this.chartOption };
      let xAxisData = clearData.map((i) => i.key);
      chartOption.xAxis.data = xAxisData.map((i) => i.join("-"));
      let legengData = [];
      let seriesLabel = countLabel(this.option);
      chartOption.series = measure.map((m, i) => {
        legengData.push(m.columnTitle);
        let serie = {
          name: m.columnTitle,
          type: "bar",
          barWidth,
          data: clearData.map((d, index) => ({
            value: d.value[i],
            name: xAxisData[index],
          })),
          label: seriesLabel,
          itemStyle: { borderRadius: parseInt(barBorderRadius) },
        };
        if (barSwitchBg) {
          serie.showBackground = true;
          serie.backgroundStyle = { color: barBgColor };
        }
        return serie;
      });
      chartOption.legend.data = legengData;
      chartOption.xAxis.axisLabel = countXlabel(this.option);
      this.setYlabel(chartOption);
      this.setSerieColor(chartOption);
      countLegendSelect(this.option, chartOption);
      this.chart = echarts.init(document.getElementById(this.index));
      this.chart.off("click", this.handleClick);
      if (this.config.zuanFlag) {
        this.chart.on("click", this.handleClick);
      }
      this.chart.setOption(chartOption, true);
    },
    // y轴设置
    setYlabel(chartOption) {
      let { ylabelFontSize, ylabelColor } = this.option;
      chartOption.yAxis.forEach((item) => {
        item.axisLabel = {
          fontSize: ylabelFontSize,
          color: ylabelColor,
        };
      });
    },
    // 设置系列颜色
    setSerieColor(chartOption) {
      let list = this.option.colorList.slice(0, chartOption.series.length);
      list.forEach(({ color1, color2, direction }, index) => {
        direction = direction || "top";
        if (color1 && color2) {
          chartOption.series[index].itemStyle.color = {
            x: direction == "left" ? 1 : 0,
            y: direction == "top" ? 1 : 0,
            x2: direction == "right" ? 1 : 0,
            y2: direction == "bottom" ? 1 : 0,
            colorStops: [
              { offset: 0, color: color1 },
              { offset: 1, color: color2 },
            ],
          };
        } else if (color1 && !color2) {
          chartOption.series[index].itemStyle.color = color1;
        }
      });
    },
    update(data) {
      this.originData = data;
      this.refresh();
    },
    initRender() {
      let option = {
        tooltip: { trigger: "axis", axisPointer: { type: "shadow" } },
        legend: {
          show: false,
          orient: "horizontal",
          left: "clean",
          top: "auto",
          type: "scroll",
          textStyle: { fontSize: 12 },
          data: ["价格"],
        },
        grid: {
          containLabel: true,
          left: "1%",
          top: "10%",
          right: "1%",
          bottom: "1%",
        },
        xAxis: {
          type: "category",
          data: ["老年", "青年", "少年", "中年"],
          axisLabel: {
            fontSize: 12,
            color: "#333333",
            interval: "0",
            rotate: 0,
          },
        },
        yAxis: [
          { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
        ],
        series: [
          {
            name: "价格",
            type: "bar",
            barWidth: 20,
            data: [
              { value: 167479, name: ["老年"] },
              { value: 360022, name: ["青年"] },
              { value: 180023, name: ["少年"] },
              { value: 174590, name: ["中年"] },
            ],
            label: {
              show: true,
              color: "#333333",
              fontSize: 12,
              position: "inside",
              textBorderWidth: 0,
              textBorderColor: "#ffffff",
              rotate: 0,
            },
            itemStyle: { borderRadius: 0 },
          },
        ],
      };
      this.chart = echarts.init(document.getElementById(this.index));
      this.chart.setOption(option, true);
      this.$nextTick(this.chart.resize);
    },
  },
};
</script>
<style lang="scss">
.new-bar {
  .base {
    width: 100%;
    height: calc(100% - 20px);
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .zuan {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    line-height: 20px;
    padding: 0 10px;
    font-size: 12px;
  }
}
</style>
