<template>
  <div class="new-line">
    <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-line",
  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: [],
          axisLabel: {
            showMaxLabel: true,
          },
        },
        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 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);
      let serieLine = styleCommon.countLine(this.option);
      chartOption.series = measure.map((m, i) => {
        legengData.push(m.columnTitle);
        let serie = {
          name: m.columnTitle,
          type: "line",
          data: clearData.map((d, index) => ({
            value: d.value[i],
            name: xAxisData[index],
          })),
          label: seriesLabel,
          itemStyle: {},
          lineStyle: serieLine,
          smooth: this.option.lineSmooth,
        };
        return serie;
      });
      chartOption.legend.data = legengData;
      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: {
          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: "line",
            data: [
              { value: 4239, name: ["卡布奇诺"] },
              { value: 7067, name: ["美式"] },
              { value: 19071, name: ["摩卡"] },
              { value: 23868, name: ["拿铁"] },
            ],
            label: {
              show: true,
              color: "#333333",
              fontSize: 12,
              position: "inside",
              textBorderWidth: 0,
              textBorderColor: "#ffffff",
              rotate: 0,
            },
            itemStyle: {},
          },
        ],
      };
      this.chart = echarts.init(document.getElementById(this.index));
      this.chart.setOption(option, true);
      this.$nextTick(this.chart.resize);
    },
  },
};
</script>
<style lang="scss">
.new-line {
  .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>
