<!--
 * @Author: chris
 * @Date: 2025-10-21 11:42:40
 * @LastEditors: chris
 * @LastEditTime: 2025-11-17 10:17:04
-->
<script setup name="line-chart">
import LEchart from '@/uni_modules/lime-echart_1.0.5/components/l-echart/l-echart.vue'

const props = defineProps({
  echarts: {
    type: Object,
    required: true,
  },
  chartData: {
    type: Object,
    default: () => ({
      dates: ['10/1', '10/2', '10/3', '10/4', '10/5', '10/6', '10/7'],
      values: [42, 49, 56, 32, 71, 33, 22],
    }),
  },
})

const lineChartRef = ref(null)
let lineChart = null

watch(() => props.chartData, () => {
  refreshChart()
})

onMounted(async () => {
  nextTick(async () => {
    await initChart()
  })
})

onUnmounted(() => {
  if (lineChart)
    lineChart.dispose()
})

async function initChart() {
  if (!lineChartRef.value || !props.echarts)
    return
  lineChart = await lineChartRef.value.init(props.echarts)
  refreshChart()
}

function refreshChart() {
  if (!lineChart)
    return
  lineChart.showLoading()
  lineChart.setOption(createOptions())
  lineChart.hideLoading()
}

function createOptions() {
  const gradient = new props.echarts.graphic.LinearGradient(0, 0, 0, 1, [
    { offset: 0, color: 'rgba(77, 171, 247, 0.5)' },
    { offset: 1, color: 'rgba(77, 171, 247, 0.1)' },
  ])

  const option = {
    title: {
      text: '7天虫情数量趋势',
      top: '2%',
      textStyle: {
        color: '#333',
        fontSize: 16,
        fontWeight: 'bold',
      },
      left: 'center',
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.7)',
      borderColor: '#333',
      textStyle: {
        color: '#fff',
      },
      formatter: (params) => {
        const data = params[0]
        return `虫情数量: ${Math.round(data.value)}`
      },
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985',
        },
      },
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      top: '20%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: props.chartData.dates,
      axisLine: {
        lineStyle: {
          color: '#464646',
        },
      },
      axisLabel: {
        color: '#ddd',
      },
    },
    yAxis: [
      {
        type: 'value',
        name: '数量',
        nameTextStyle: {
          color: '#ddd',
        },
        min: 0,
        max: 'dataMax + 50',
        axisLine: {
          lineStyle: {
            color: '#464646',
          },
        },
        axisLabel: {
          color: '#ddd',
        },
        splitLine: {
          lineStyle: {
            color: '#2a2a2a',
            type: 'dashed',
          },
        },
      },
    ],
    series: [
      {
        name: '虫情数量',
        type: 'line',
        data: props.chartData.values,
        smooth: true,
        symbol: 'circle',
        symbolSize: 6,
        itemStyle: {
          color: '#4dabf7',
        },
        areaStyle: {
          color: gradient,
        },
        emphasis: {
          focus: 'series',
        },
      },
    ],
  }

  return option
}
</script>

<template>
  <view class="line-chart">
    <l-echart id="lineChart" ref="lineChartRef" type="2d" />
  </view>
</template>

<style lang="scss" scoped>
.line-chart {
  width: 100%;
  height: 300px;
}
</style>
