<template>
  <ContentWrap>
    <div ref="stackedBarChart" class="h-350px"></div>
  </ContentWrap>
</template>

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

defineOptions({ name: 'LockerMistake' })

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

const stackedBarChart = 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()
    }
  }
)
onMounted(()=>{
  getList()
})
/** 获取数据 */
const getList = async () => {
  try {
    const response = await getCabinetStatistics(queryParams)
    cabinetData.value = response
    nextTick(() => {
      renderChart()
    })
  } catch (error) {
    console.error('获取物资柜异常统计数据失败:', error)
  }
}

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

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

  // 提取数据
  const cabinetNames = cabinetData.value.map(cabinet => cabinet.cabinetName)
  const misplaceData = cabinetData.value.map(cabinet => cabinet.misplaceCount || 0)
  const timeoutData = cabinetData.value.map(cabinet => cabinet.openTimeoutCount || 0)

  // 配置图表选项
  const options = {
    title: {
      text: '物资柜异常统计',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['物资错放', '超时未关'],
      top: '6%',
      left: 'center',
      textStyle: {
        fontSize: 14
      }
    },
    xAxis: {
      type: 'category',
      data: cabinetNames,
      axisLabel: {
        rotate: 0,
        fontSize: 12
      }
    },
    dataZoom: {
      show: true,
      start: 0,
      end: 100,
      maxSpan: 100
    },
    yAxis: {
      type: 'value',
      name: '次数',
      splitLine: {
        show: false
      }
    },
    series: [
      {
        name: '物资错放',
        type: 'bar',
        stack: '异常情况',
        barWidth: '25',
        data: misplaceData,
        itemStyle: {
          color: '#5470c6'
        }
      },
      {
        name: '超时未关',
        type: 'bar',
        stack: '异常情况',
        data: timeoutData,
        itemStyle: {
          color: '#91cc75'
        }
      }
    ]
  }

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

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

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

<style lang="scss" scoped>
.h-350px {
  height: 350px;
}
</style>
