<template>
  <div class="new-mixture">
    <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 * as styleCommon from "../../newStyles/styleCommon";
export default {
  name: "new-mixture",
  props: ["option", "config", "modalSet", "index", "site"],
  data() {
    return {
      originData: [],
      chartOption: {
        tooltip: {
          trigger: "axis",
          axisPointer: { type: "shadow" },
        },
        legend: { data: [] },
        grid: { left: "3%", right: "3%", bottom: "3%", containLabel: true },
        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, serieSet } =
        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;
      });

      this.chartOption.grid = styleCommon.grid(this.option);
      //标题设置
      if (this.option.isTitleShow) {
        this.chartOption.title = styleCommon.title(this.option);
      }
      this.chartOption.legend = styleCommon.legend(this.option);

      let chartOption = { ...this.chartOption };
      let xAxisData = clearData.map((i) => i.key);
      chartOption.xAxis.data = xAxisData.map((i) => i.join("-"));
      let legengData = [];
      let seriesLabel = styleCommon.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;
      // 设置指标展示方式
      let secondFlag = false;
      serieSet = serieSet.slice(0, chartOption.series.length);
      serieSet.forEach((set, index) => {
        if (set.second) secondFlag = true;
        chartOption.series[index].type = set.type;
        if (set.type === "line") {
          chartOption.series[index].lineStyle = styleCommon.countLine(
            this.option
          );
        }
        chartOption.series[index].yAxisIndex = set.second ? 1 : 0;
      });
      if (secondFlag) {
        chartOption.yAxis.push({ type: "value" });
      }
      chartOption.xAxis.axisLabel = styleCommon.countXlabel(this.option);
      this.setYlabel(chartOption);
      this.setSerieColor(chartOption);
      styleCommon.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: { data: ["成本额", "数量"] },
        grid: { left: "3%", right: "3%", bottom: "3%", containLabel: true },
        xAxis: {
          type: "category",
          data: ["调料", "零食", "日用品", "生鲜", "饮料"],
          axisLabel: {
            fontSize: 12,
            color: "#333333",
            interval: "0",
            rotate: 0,
          },
        },
        yAxis: [
          { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
          { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
          { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
          { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
        ],
        series: [
          {
            name: "成本额",
            type: "bar",
            barWidth: 20,
            data: [
              { value: 410407.5, name: ["调料"] },
              { value: 16857967.76, name: ["零食"] },
              { value: 8910576.01, name: ["日用品"] },
              { value: 4344919.27, name: ["生鲜"] },
              { value: 3278582.06, name: ["饮料"] },
            ],
            label: {
              show: false,
              color: "#333333",
              fontSize: 12,
              position: "inside",
              textBorderWidth: 0,
              textBorderColor: "#ffffff",
              rotate: 0,
            },
            itemStyle: { borderRadius: 0 },
            yAxisIndex: 0,
          },
          {
            name: "数量",
            type: "line",
            barWidth: 20,
            data: [
              { value: 9952, name: ["调料"] },
              { value: 1090532, name: ["零食"] },
              { value: 832817, name: ["日用品"] },
              { value: 123691, name: ["生鲜"] },
              { value: 291179, name: ["饮料"] },
            ],
            label: {
              show: false,
              color: "#333333",
              fontSize: 12,
              position: "inside",
              textBorderWidth: 0,
              textBorderColor: "#ffffff",
              rotate: 0,
            },
            itemStyle: { borderRadius: 0 },
            yAxisIndex: 1,
          },
        ],
      };
      this.chart = echarts.init(document.getElementById(this.index));
      this.chart.setOption(option, true);
      this.$nextTick(this.chart.resize);
    },
  },
};
</script>
<style lang="scss">
.new-mixture {
  .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>
