<template>
  <div class="h-full flex flex-col">
    <div class="flex justify-between p-4">
      <div class="text-[#999] text-sm">彩种销售走势</div>
      <div class="w-50">
        <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="trendRef"></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 trendRef = ref(null)
const { setOptions } = useECharts(trendRef)

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

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

  datas.value.forEach((item) => {
    item.data.forEach((entry) => {
      const { name, sales } = entry
      if (!groupedData[name]) {
        groupedData[name] = []
      }
      groupedData[name].push({ date: item.date, sales })
    })
  })

  const xAxisData = []
  const seriesData = []
  Object.keys(groupedData).forEach((name) => {
    xAxisData.push(name)
    seriesData.push({
      name: name,
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 5,
      showSymbol: false,
      areaStyle: {
        opacity: 0.1,
        // normal: {
        //   // color: new echarts.graphic.LinearGradient(
        //   //   0,
        //   //   0,
        //   //   0,
        //   //   1,
        //   //   [
        //   //     {
        //   //       offset: 0,
        //   //       color: 'rgba(25,163,223,.3)',
        //   //     },
        //   //     {
        //   //       offset: 1,
        //   //       color: 'rgba(25,163,223, 0)',
        //   //     },
        //   //   ],
        //   //   false
        //   // ),
        //   opacity: 0.1,
        // },
      },
      lineStyle: {
        width: 1,
      },
      data: groupedData[name].map((item) => item.sales),
    })
  })

  setOptions({
    title: {
      textStyle: {
        fontWeight: 'normal',
        fontSize: 16,
        color: '#F1F1F3',
      },
      left: '6%',
    },
    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,
        axisLine: {
          lineStyle: {
            color: '#999',
          },
        },
        data: datas.value.map((e) => e.date ?? e.month),
      },
    ],
    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>
