<template>
  <div class="line-chart">
    <h3 class="chart-title">{{ title }}</h3>
    <chartsCard
      :option="chartOption"
      :width="width"
      :height="height"
      @chart-ready="handleChartReady"
    />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import chartsCard from './chartsCard.vue'

const props = defineProps({
  title: {
    type: String,
    default: '折线图'
  },
  xData: {
    type: Array,
    required: true // X轴数据（通常是时间或类别）
  },
  yData: {
    type: Array,
    required: true // Y轴数据（数值）
  },
  seriesName: {
    type: String,
    default: '数据' // 系列名称
  },
  width: {
    type: String,
    default: '100%'
  },
  height: {
    type: String,
    default: '400px'
  },
  color: {
    type: String,
    default: '#5793f3' // 线条颜色
  },
  isSmooth: {
    type: Boolean,
    default: true // 是否平滑曲线
  }
})

const chartOption = computed(() => ({
  tooltip: {
    trigger: 'axis',
    backgroundColor: 'rgba(255, 255, 255, 0.9)',
    borderColor: '#ddd',
    borderWidth: 1,
    textStyle: { color: '#333' },
    formatter: (params: any) => {
      const p = params[0]
      return `${p.name}<br/>${p.seriesName}: ${p.value}`
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: props.xData,
    axisTick: {
      show: false
    },
    axisLine: {
      lineStyle: {
        color: '#999'
      }
    },
    axisLabel: {
      color: '#666'
    }
  },
  yAxis: {
    type: 'value',
    axisTick: {
      show: false,
      alignWithLabel: true
    },
    axisLine: {
      show: false
    },
    axisLabel: {
      color: '#666'
    },
    splitLine: {
      lineStyle: {
        color: '#eee'
      }
    }
  },
  series: [
    {
      name: props.seriesName,
      type: 'line',
      data: props.yData,
      smooth: props.isSmooth,
      lineStyle: {
        width: 2,
        color: props.color
      },
      itemStyle: {
        color: props.color,
        borderWidth: 2,
        normal: { label: { show: true } },
        symbolSize: 10 //设定实心点的大小
      },
      symbol: 'circle', // 标记点形状
      symbolSize: 6, // 标记点大小
      showSymbol: true, // 默认不显示标记点，鼠标悬停时显示
      emphasis: {
        symbol: 'circle',
        symbolSize: 8
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: `${props.color}80` }, // 半透明
            { offset: 1, color: `${props.color}10` } // 几乎透明
          ]
        }
      }
    }
  ]
}))

const handleChartReady = (chart: any) => {
  // 可添加交互逻辑
}
</script>

<style scoped lang="scss">
.line-chart {
  .chart-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
    color: #333;
  }
}
</style>
