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

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

defineOptions({ name: 'LockerDaily' })

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()
    }
  }
)
onMounted(()=>{
  queryParams.startTime = props.startTime
  queryParams.endTime = props.endTime
  getList()
})
/** 获取数据 */
const getList = async () => {
  try {
    const response = await getDayLoanStatistics(queryParams)
    // 按日期排序
    const sortData = response.sort((a, b) => {
      const dateA = new Date(a.day)
      const dateB = new Date(b.day)
      return dateA.getTime() - dateB.getTime()
    })
    cabinetData.value = sortData
    renderChart()
  } catch (error) {
    console.error('获取每日领取归还统计数据失败:', error)
  }
}

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

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

  // 提取数据
  const day = cabinetData.value.map(item => item.day)
  const allCount = cabinetData.value.map(item => item.allCount)
  const returnCount = cabinetData.value.map(item => item.returnCount)
  const timeoutCount = cabinetData.value.map(item => item.timeoutCount)

  // 配置图表选项
  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: day,
        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',
        barWidth: '40%',
        tooltip: {
          valueFormatter: (value: number) => value + ' 次'
        },
        data: allCount,
        itemStyle: {
          color: '#5470c6'
        }
      },
      {
        name: '累计正常归还次数',
        type: 'bar',
        barWidth: '40%',
        stack: 'total',
        tooltip: {
          valueFormatter: (value: number) => value + ' 次'
        },
        data: returnCount,
        itemStyle: {
          color: '#a6d58f'
        }
      },
      {
        name: '累计超时归还次数',
        type: 'bar',
        barWidth: '40%',
        stack: 'total',
        tooltip: {
          valueFormatter: (value: number) => value + ' 次'
        },
        data: timeoutCount,
        itemStyle: {
          color: '#fac85a'
        }
      }
    ]
  }

  // 设置图表配置
  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;
}
.mt-3 {
  margin-top: 3%;
}
</style>
