<template>
  <div class="h-full flex flex-col">
    <div class="flex justify-between items-center p-4">
      <div class="text-[#999] text-sm">销售走势</div>
      <div class="w-30">
        <a-select
          ref="select"
          v-model:value="day"
          class="w-full"
          @change="onChange"
        >
          <a-select-option
            v-for="item in dayList"
            :key="item.value"
            :value="item.value"
            >{{ item.label }}</a-select-option
          >
        </a-select>
      </div>
    </div>
    <div class="flex-1" ref="trendRef1"></div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useECharts } from '@/components/echarts/useECharts.js'
import echarts from '@/components/echarts/echarts.js'
import http from '@/utils/request'

const datas = ref([])
const day = ref('7days')
const dayList = [
  {
    value: '7days',
    label: '近7天',
  },
  {
    value: '30days',
    label: '近30天',
  },
  {
    value: '180days',
    label: '近半年',
  },
  {
    value: '365days',
    label: '近一年',
  },
]
const trendRef1 = ref(null)
const { setOptions } = useECharts(trendRef1)

onMounted(() => {
  getData()
})

const getData = async () => {
  const resData = await http.get('/admin-api/api/cockpit/sales-trend', {
    params: {
      last: day.value,
    },
  })
  datas.value = resData

  chatInit()
}
const chatInit = () => {
  const xAxisData = []
  const sales = []
  datas.value.forEach((item) => {
    xAxisData.push(item.date ?? item.month)
    sales.push(item.sales)
  })

  const seriesData = []
  seriesData.push({
    name: '销售',
    type: 'line',
    smooth: true,
    symbol: 'circle',
    symbolSize: 5,
    showSymbol: false,
    areaStyle: {
      opacity: 0.1,
    },
    lineStyle: {
      width: 1,
    },
    data: sales,
  })

  setOptions({
    title: {
      textStyle: {
        fontWeight: 'normal',
        fontSize: 16,
        color: '#F1F1F3',
      },
      left: '6%',
      right: '20px',
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        lineStyle: {
          color: '#57617B',
        },
      },
    },
    legend: {
      icon: 'rect',
      itemWidth: 14,
      itemHeight: 5,
      itemGap: 13,
      // data: xAxisData,
      right: '4%',
      textStyle: {
        fontSize: 12,
        color: '#292f39',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        axisLabel:{
          // interval: 3,
          // formatter: function (value,i) {
          //   const xAxisDataLeg= xAxisData.length
          //   //只显示3个点的值，最后一个不显示，显示倒数第2个
          //   if(i===xAxisDataLeg-2){
          //     return value
          //   }
          // }
        },
        axisLine: {
          
          lineStyle: {
            color: '#999',
          },
        },
        data: xAxisData,
      },
    ],
    yAxis: [
      {
        type: 'value',
        axisTick: {
          show: false,
        },
        axisLine: {
          lineStyle: {
            color: '#57617B',
          },
        },
        axisLabel: {
          margin: 10,
          textStyle: {
            fontSize: 14,
          },
        },
        splitLine: {
          lineStyle: {
            color: '#eee',
          },
        },
      },
    ],
    series: seriesData,
  })
}

const onChange = (e) => {
  getData()
}
</script>
