<template>
  <!-- 今日图书借还 -->
  <div class="screen-item total-lending">
    <div class="common-title">今日图书借还</div>
    <div class="small-module module-content">
      <div class="chart-wrapper" style="height: calc(100%);">
        <LineChartService :chart-data="chartData" />
      </div>
    </div>
  </div>
</template>

<script>
import LineChartService from '@/components/echart/lineChartService'
import { FetchHalfYearBRNum, FetchTodayJH } from '@/api/library'
export default {
  name: 'TodayBorrowed',
  components: {
    LineChartService
  },
  data() {
    return {
      chartData: {
        returnData: [],
        borrowedData: []
      }
    }
  },
  created() {
  },
  mounted() {
    this.getTodayJH()
  },
  methods: {
    getHalfYearBRNum() {
      FetchHalfYearBRNum().then(res => {
        if (res.errCode === 0) {
          this.chartData.borrowedData = res.data.borrow
          this.chartData.returnData = res.data.return
        } else {
          this.$message.error('接口错误')
        }
      })
    },
    getTodayJH() {
      FetchTodayJH().then(res => {
        this.chartData = {
          timeData: [],
          returnData: [],
          borrowedData: []
        }
        const result = res.data
        const time = ['07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00']
        time.forEach((hour, index) => {
          // 查找 result 中是否有对应的 logHour
          const foundItem = result.find(item => item.logHour === index + 7) // 因为 logHour 是从 7 开始，所以需要加上 7
          if (foundItem) {
            // 如果找到了对应的 logHour，则将数据加入 chartDayData
            this.chartData.timeData.push(`${foundItem.logHour}:00`)
            this.chartData.returnData.push(foundItem.hccDay)
            this.chartData.borrowedData.push(foundItem.jccDay)
          } else {
            // 如果没找到对应的 logHour，则将默认值加入 chartDayData
            this.chartData.timeData.push(`${hour}`)
            this.chartData.returnData.push(0) // 默认值为 0
            this.chartData.borrowedData.push(0) // 默认值为 0
          }
        })
      }).catch(error => {
        console.error('Error', error)
      })
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/styles/index.scss";
</style>
