<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import * as echarts from 'echarts';
import { averageDuration } from "@/api";

const chartRef = ref<HTMLElement | null>(null);
let chart: echarts.ECharts | null = null;

// 数据
const chartData = ref({
  dates: ['7/1', '7/2', '7/3', '7/4', '7/5'],
  values: [80, 90, 55, 82, 62]
});

// 过滤选项
const conversationTypes = ref([
  { label: '会话类型', value: 'all' }
]);
const selectedType = ref('all');

const timeFilters = ref([
  { label: '时间筛选', value: 'week' }
]);
const selectedTimeFilter = ref('week');

// 获取数据
const getChartData = () => {
  averageDuration({
    type: selectedType.value, 
    timeFilter: selectedTimeFilter.value
  }).then(res => {
    if (res.success) {
      // 假设API返回的数据格式是 {dates: [], values: []}
      // chartData.value = res.data;
      initChart();
    } else {
      // 如果API调用失败，使用模拟数据
      initChart();
    }
  }).catch(() => {
    // 如果发生错误，使用模拟数据
    initChart();
  });
};

// 监听筛选条件变化
const handleFilterChange = () => {
  getChartData();
};

const initChart = () => {
  if (!chartRef.value) return;
  
  chart = echarts.init(chartRef.value);
  
  const option = {
    grid: {
      top: '10%',
      left: '8%',
      right: '5%',
      bottom: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: chartData.value.dates,
      axisLine: {
        lineStyle: {
          color: '#E0E0E0'
        }
      },
      axisLabel: {
        color: '#999999',
        fontSize: 12
      }
    },
    yAxis: {
      type: 'value',
      max: 100,
      interval: 30,
      axisLabel: {
        color: '#99AABF',
        fontSize: 12,
        formatter: '{value}'
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#E0E0E0'
        }
      }
    },
    series: [
      {
        type: 'bar',
        data: chartData.value.values,
        barWidth: '30%',
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#4B96FF' // 蓝色渐变开始
              },
              {
                offset: 1,
                color: '#65CBFF' // 蓝色渐变结束
              }
            ]
          },
          borderRadius: [4, 4, 0, 0]
        }
      }
    ]
  };
  
  chart.setOption(option);
};

// 监听窗口大小变化，重绘图表
const handleResize = () => {
  if (chart) {
    chart.resize();
  }
};

onMounted(() => {
  getChartData();
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
  if (chart) {
    chart.dispose();
    chart = null;
  }
});
</script>

<template>
  <div class="average-duration">
    <div class="chart-header">
      <div class="filters">
        <div class="filter-item">
          <el-select v-model="selectedType" size="small" class="filter-select" @change="handleFilterChange">
            <el-option
              v-for="item in conversationTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div class="filter-item">
          <el-select v-model="selectedTimeFilter" size="small" class="filter-select" @change="handleFilterChange">
        <el-option
              v-for="item in timeFilters"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
        </div>
      </div>
    </div>
    <div class="chart-container" ref="chartRef"></div>
  </div>
</template>

<style scoped lang="scss">
.average-duration {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 0;
  box-sizing: border-box;
}

.chart-header {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 10px;
  
  .filters {
    display: flex;
    gap: 10px;
    
    .filter-item {
      .filter-select {
        width: 120px;
  
  :deep(.el-input__wrapper) {
    background-color: #f5f7fa;
    box-shadow: none;
          border-radius: 4px;
  }
  
        :deep(.el-input__inner) {
          font-size: 12px;
        }
      }
    }
  }
}

.chart-container {
  flex: 1;
  width: 100%;
}
</style> 