<template>
  <moduleEnclosure title="近30日订单金额趋势">
    <div class="all-container">
      <div id="orderEcharts2" style="width: 100%; height: 100%"></div>
    </div>
  </moduleEnclosure>
</template>

<script>
import * as echarts from 'echarts'
import moduleEnclosure from '@/components/moduleEnclosure.vue'
import { getOrderTrend } from '@/api/bigView.js'
import { csize } from '@/utils/const.js'

export default {
  components: {
    moduleEnclosure
  },

  data() {
    return {}
  },

  async mounted() {
    getOrderTrend().then(res => {
      this.initEcharts(res.data)
    })
  },

  beforeUnmount() {},

  methods: {
    getDate() {
      const today = new Date()
      const dates = []

      for (let i = 0; i < 30; i++) {
        const d = new Date()
        d.setDate(today.getDate() - i)
        dates.unshift(d.toISOString().split('T')[0].slice(5, 11))
      }

      return dates
    },

    initEcharts(data) {
      var chartDom = document.getElementById('orderEcharts2')
      var myChart = echarts.init(chartDom)
      var option

      option = {
        title: {
          text: ''
        },
        tooltip: {
          confine: true,
          trigger: 'axis',
          transitionDuration: 0,
          backgroundColor: '#092a66',
          borderColor: '#0462b5',
          borderRadius: 8,
          borderWidth: 2,
          padding: [5, 10],
          axisPointer: {
            type: 'line',
            lineStyle: {
              type: 'dashed',
              color: '#8AB1DC'
            }
          },
          textStyle: {
            color: '#ffffff'
          }
        },
        legend: {
          icon: 'circle',
          itemWidth: 8,
          itemHeight: 8,
          itemGap: 10,
          top: '16',
          right: '10',
          textStyle: {
            color: '#A9BBCC'
          },
          data: ['新增辅材金额', '新增配件金额']
        },
        grid: {
          x: csize(40),
          y: csize(50),
          x2: csize(20),
          y2: csize(20)
        },
        toolbox: {
          // feature: {
          //   saveAsImage: {}
          // }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            fontSize: csize(12)
          },
          axisLine: {
            lineStyle: {
              color: '#8AB1DC' // x轴文字颜色
            }
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: 'rgba(256, 256, 256, 0.1)' // x轴辅助线颜色
            }
          },
          data: this.getDate()
        },
        yAxis: {
          type: 'value',
          minInterval: 1,
          name: '（元）',
          nameTextStyle: {
            color: '#aaa',
            nameLocation: 'start',
            align: 'right'
          },
          axisLabel: {
            fontSize: csize(12)
          },
          axisLine: {
            lineStyle: {
              color: '#8AB1DC' // y轴文字颜色
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(256, 256, 256, 0.1)' // y轴辅助线颜色
            }
          }
        },
        series: [
          // {
          //   name: '新增销售金额',
          //   type: 'line',
          //   stack: 'Total',
          //   itemStyle: {
          //     normal: {
          //       color: '#FDFE02'
          //     }
          //   },
          //   data: data.map(o => o.sxddje)
          // },
          {
            name: '新增辅材金额',
            type: 'line',
            stack: 'Total',
            itemStyle: {
              normal: {
                color: '#44E28F'
              }
            },
            data: data.map(o => o.fcddje)
          },
          {
            name: '新增配件金额',
            type: 'line',
            stack: 'Total',
            itemStyle: {
              normal: {
                color: '#41C5E2'
              }
            },
            data: data.map(o => o.pjddje)
          }
          // {
          //   name: '新增增值金额',
          //   type: 'line',
          //   stack: 'Total',
          //   itemStyle: {
          //     normal: {
          //       color: '#D73A79'
          //     }
          //   },
          //   data: data.map(o => o.zzddje)
          // }
        ]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>

<style scoped lang="scss">
.all-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 16px;
}
</style>
