<template>
  <module-container title="放行速率分析">
    <div class="module-four-content">
      <div class="chart-row">
        <div class="chart-item">
          <div class="chart-title">T1航站楼放行速率</div>
          <div ref="chart1" class="echarts"></div>
        </div>
        <div class="chart-item">
          <div class="chart-title">T2航站楼放行速率</div>
          <div ref="chart2" class="echarts"></div>
        </div>
      </div>
      <div class="chart-row">
        <div class="chart-item">
          <div class="chart-title">大队总平均速率对比（国内）</div>
          <div ref="chart3" class="echarts"></div>
        </div>
        <div class="chart-item">
          <div class="chart-title">大队总平均速率对比（国际+中转）</div>
          <div ref="chart4" class="echarts"></div>
        </div>
      </div>
    </div>
  </module-container>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
import ModuleContainer from './ModuleContainer.vue'
import { useEcharts } from '@/hooks/chart.js'

const chart1 = ref(null)
const chart2 = ref(null)
const chart3 = ref(null)
const chart4 = ref(null)

const { setOption: setOption1 } = useEcharts(chart1)
const { setOption: setOption2 } = useEcharts(chart2)
const { setOption: setOption3 } = useEcharts(chart3)
const { setOption: setOption4 } = useEcharts(chart4)

const generateData = () => {
  const data = []
  for (let i = 0; i < 30; i++) {
    data.push(Math.floor(Math.random() * 60) + 120)
  }
  return data
}

const xAxisData = []
for (let i = 1; i <= 30; i++) {
  xAxisData.push(i + '日')
}

const lineChartOption = (data1, data2, color1, color2) => ({
  grid: {
    left: '10%',
    top: '15%',
    right: '5%',
    bottom: '15%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: xAxisData,
    axisLine: { lineStyle: { color: '#999' } },
    axisLabel: { fontSize: 10, color: '#666' }
  },
  yAxis: {
    type: 'value',
    axisLine: { lineStyle: { color: '#999' } },
    axisLabel: { fontSize: 10, color: '#666' },
    splitLine: { lineStyle: { color: '#eee' } }
  },
  legend: {
    top: 0,
    right: 10,
    textStyle: { fontSize: 10 }
  },
  series: [
    {
      name: '高峰期时段',
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      data: data1,
      itemStyle: { color: color1 },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: color1 + '40' },
          { offset: 1, color: color1 + '10' }
        ])
      }
    },
    {
      name: '非高峰期时段',
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      data: data2,
      itemStyle: { color: color2 },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: color2 + '40' },
          { offset: 1, color: color2 + '10' }
        ])
      }
    }
  ]
})

const barLineChartOption = (data1, data2, data3, colors) => ({
  grid: {
    left: '10%',
    top: '15%',
    right: '5%',
    bottom: '15%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: xAxisData,
    axisLine: { lineStyle: { color: '#999' } },
    axisLabel: { fontSize: 10, color: '#666', rotate: 45 }
  },
  yAxis: {
    type: 'value',
    axisLine: { lineStyle: { color: '#999' } },
    axisLabel: { fontSize: 10, color: '#666' },
    splitLine: { lineStyle: { color: '#eee' } }
  },
  legend: {
    top: 0,
    right: 10,
    textStyle: { fontSize: 10 }
  },
  series: [
    {
      name: '安检三大队',
      type: 'bar',
      data: data1,
      itemStyle: { color: colors[0] },
      barWidth: 8
    },
    {
      name: '安检二大队',
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      data: data2,
      itemStyle: { color: colors[1] }
    },
    {
      name: '安检一大队',
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      data: data3,
      itemStyle: { color: colors[2] }
    }
  ]
})

onMounted(() => {
  setOption1(lineChartOption(generateData(), generateData(), '#3b82f6', '#22c55e'))
  setOption2(lineChartOption(generateData(), generateData(), '#2563eb', '#16a34a'))
  setOption3(barLineChartOption(generateData(), generateData(), generateData(), ['#3b82f6', '#22c55e', '#f97316']))
  setOption4(barLineChartOption(generateData(), generateData(), generateData(), ['#3b82f6', '#22c55e', '#f97316']))
})
</script>

<style lang="less" scoped>
.module-four-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chart-row {
  flex: 1;
  display: flex;
  gap: 10px;
}

.chart-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 6px;
  padding: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.chart-title {
  font-size: 12px;
  color: #333;
  margin-bottom: 5px;
  text-align: center;
}

.echarts {
  flex: 1;
  width: 100%;
  min-height: 0;
}
</style>
