<template>
  <ContentWrap>
    <div ref="barChart" class="h-350px mt-3"></div>
  </ContentWrap>
</template>

<script lang="ts" setup>
import { getMaterialsChangeStatistics } from '@/api/material/statistics/index'
import * as echarts from 'echarts'

defineOptions({ name: 'LockerChange' })

const props = defineProps({
  startTime: {
    type: String,
    default: ''
  },
  endTime: {
    type: String,
    default: ''
  }
})

const barChart = ref<HTMLElement>() // 图表容器
const chartInstance = ref<echarts.ECharts>() // 图表实例
const cabinetData = ref([]) // 存储请求到的柜子数据

const queryParams = reactive({
  startTime: undefined as string | undefined,
  endTime: undefined as string | undefined
})

// 监听日期变化
watch(
  () => [props.startTime, props.endTime],
  ([newStartTime, newEndTime]) => {
    if (newStartTime && newEndTime) {
      queryParams.startTime = newStartTime
      queryParams.endTime = newEndTime
      getList()
    }
  }
)

/** 获取数据 */
const getList = async () => {
  try {
    const response = await getMaterialsChangeStatistics(queryParams)
    console.log(response,'数据格式')
    cabinetData.value = response
    renderChart()
  } catch (error) {
    console.error('获取物资更换统计数据失败:', error)
  }
}

/** 渲染图表 */
const renderChart = () => {
  if (!cabinetData.value.length || !barChart.value) return

  // 初始化图表实例
  chartInstance.value = echarts.init(barChart.value)

  // 提取数据
  const materialTypeName = cabinetData.value.map(item => item.materialsTypeName)
  const allCount = cabinetData.value.map(item => item.allCount)
  const expireCount = cabinetData.value.map(item => item.expireCount)
  const badCount = cabinetData.value.map(item => item.badCount)

  // 配置图表选项
  const options = {
    title: {
      text: '物资更换统计',
      left: 'center',
      top: '0%'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['正常更换次数', '过期更换次数', '损坏更换次数'],
      top: '6%',
      left: 'center'
    },
    xAxis: [
      {
        type: 'category',
        data: materialTypeName,
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    dataZoom: {
      show: true,
      start: 0,
      end: 100,
      maxSpan: 100
    },
    yAxis: [
      {
        type: 'value',
        name: '次数',
        axisLabel: {
          formatter: '{value} '
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '正常更换次数',
        type: 'bar',
        stack: 'Ad',
        barWidth: '25%',
        tooltip: {
          valueFormatter: (value: number) => value + ' 次'
        },
        data: allCount,
        itemStyle: {
          color: '#5470c6'
        }
      },
      {
        name: '过期更换次数',
        type: 'bar',
        stack: 'Ad',
        emphasis: {
          focus: 'series'
        },
        data: expireCount,
        itemStyle: {
          color: '#91cc75'
        }
      },
      {
        name: '损坏更换次数',
        type: 'bar',
        stack: 'Ad',
        emphasis: {
          focus: 'series'
        },
        data: badCount,
        itemStyle: {
          color: '#fac858'
        }
      }
    ]
  }

  // 设置图表配置
  chartInstance.value.setOption(options)
}

/** 组件销毁前清理图表实例 */
onBeforeUnmount(() => {
  if (chartInstance.value) {
    chartInstance.value.dispose()
  }
})

/** 初始化 */
onMounted(() => {
  getList()
  if (props.startTime && props.endTime) {
    queryParams.startTime = props.startTime
    queryParams.endTime = props.endTime
    getList()
  }
})
</script>

<style lang="scss" scoped>
.h-350px {
  height: 350px;
}
.mt-3 {
  margin-top: 3%;
}
</style>
