<template>
  <div ref="chart" class="chart" :style="{height: '400px', width: '100%'}" />
</template>

<script>
import * as echarts from "echarts";
import resize from '@/utils/resize'

export default {
  mixins: [resize],
  props: {
    plain: Object,
  },
  mounted() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs.chart, 'macarons');
      this.chart.on('click', (params) => {
        if (params.componentType === 'series') {
          const { data } = params;
          this.$emit('addPoint', data[0]);
        }
      })
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  watch: {
    plain: {
      handler(value) {
        this.setOptions(value)
      },
      deep: true,
      immediate: true,
    }
  },
  methods: {
    setOptions(plain) {
      if (!this.chart) {
        return;
      }
      const { sections, wlevelmax, wlevelmin, wlevel, positions } = plain;
      const xAxisData = sections.map(({ x }) => x);
      const seriesData = sections.map(({ x, y }) => ([x, y]));
      const pointsData = positions.reduce((init, p) => {
        const xIndex = xAxisData.findIndex(x => x === p);
        if (xIndex !== -1) {
          init.push({ coord: [p, wlevel] })
        }
        return init;
      }, []);
      const options = {
        xAxis: {
          name: '起点距',
          boundaryGap: false,
          type: 'value',
          min: 'dataMin',
          max: 'dataMax',
          axisLine: {
            lineStyle: {
              color: '#FF8500'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#8D99A4'
            }
          },
          axisLabel: {
            color: '#54606C'
          },
          nameTextStyle: {
            color: '#54606C'
          }
        },
        yAxis: [
          {
            name: '高程（m）',
            triggerEvent: true,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#FF8500'
              }
            },
            axisTick: {
              show: true,
              lineStyle: {
                color: '#8D99A4'
              }
            },
            axisLabel: {
              color: '#54606C'
            },
            nameTextStyle: {
              color: '#54606C'
            },
            min: 'dataMin',
            max: 'dataMax',
          },
          {
            axisLine: {
              show: true,
              lineStyle: {
                color: '#FF8500'
              }
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
          },
        ],
        grid: {
          left: 50,
          right: 50,
          bottom: 40,
          top: 40,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        series: [{
          name: '高程',
          lineStyle: {
            width: 1,
            color: '#FF8500',
          },
          areaStyle: {
            opacity: 1,
            color: '#ffc27f',
          },
          smooth: false,
          symbol: 'none',
          type: 'line',
          data: seriesData,
          animations: false,
          z: 2,
          markLine: {
            symbol: 'none',
            silent: true,
            lineStyle: { normal: { type: 'dashed' } },
            label: { position: 'start' },
            data: [
              {
                yAxis: wlevelmax,
                lineStyle: { width: 1, color: 'red' },
                name: '最高水位',
                label: {
                  formatter: '{b}'
                }
              },
              {
                yAxis: wlevelmin,
                lineStyle: { width: 1, color: 'red' },
                name: '最低水位',
                label: {
                  formatter: '{b}'
                }
              },
            ]
          },
        }]
      };
      if (plain.waterlevel) {
        options.series.push({
          data: [[Math.min(...xAxisData), plain.waterlevel],[Math.max(...xAxisData), plain.waterlevel]],
          type: 'line',
          symbol: 'none',
          z: 0,
          lineStyle: {
            width: 0,
          },
          areaStyle: {
            opacity: 1,
            color: '#a5cdf7',
          }
        })
      }
      if (wlevel > wlevelmin && wlevel < wlevelmax) {
        const points = seriesData.reduce((prev, [x, y]) => {
          if (y < wlevel) {
            prev.push([x, wlevel]);
          }
          return prev;
        }, [])
        options.series.push({
          name: '起测水位',
          lineStyle: {
            width: 0,
          },
          data: points,
          type: 'line',
          markLine: {
            symbol: 'none',
            silent: true,
            data: [{
              type: 'average',
              label: {
                formatter: '{b}'
              }
            }],
          },
          markPoint: {
            label: {
              formatter: ({ data }) => data.coord[0],
            },
            data: pointsData,
          },
          z: 0,
        });
      }
      this.chart.setOption(options);
    }
  }
}
</script>

<style scoped>

</style>
