<template>
  <!-- 新书推荐 -->
  <div class="screen-item read-star">
    <div class="common-title">阅读之星</div>
    <div ref="starList" class="medium-module module-content">
      <div v-for="(item,index) in readstarList" :key="index" :class="['readstart-item', {'star1-bg':index === 0}, {'star2-bg':index === 1}, {'star3-bg':index === 2}]">
        <svg v-if="index === 0" class="icon icon-star-1" aria-hidden="true">
          <use xlink:href="#icon-a-1" />
        </svg>
        <svg v-else-if="index === 1" class="icon icon-star-2" aria-hidden="true">
          <use xlink:href="#icon-a-21" />
        </svg>
        <svg v-else-if="index === 2" class="icon icon-star-3" aria-hidden="true">
          <use xlink:href="#icon-a-3" />
        </svg>
        <span v-else class="star-num">{{ index+1 }}</span>
        <p class="star-info title-item"> 读者{{ item.readerName }}上周借阅图书{{ item.borrowNum }}册</p>
        <p class="star-date">{{ mondayDate }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import { FetchBorrowStar } from '@/api/library'
import { parseTime } from '@/utils/index.js'
export default {
  name: 'ReadStar',
  data() {
    return {
      readstarList: [],
      mondayDate: null
    }
  },
  created() {
  },
  mounted() {
    this.getBorrowStar()
  },
  methods: {
    // 获取list
    getBorrowStar() {
      FetchBorrowStar().then(res => {
        if (res.errCode === 0) {
          console.log(res)
          this.readstarList = res.data
          this.getMondayTime()
        } else {
          this.$message.error('接口错误')
        }
      })
    },
    // 获取本周一
    getMondayTime() {
      const today = new Date()
      const year = today.getFullYear()
      const month = today.getMonth() + 1
      const day = today.getDate()
      const newDate = new Date(year + '-' + month + '-' + day + ' 00:00:00')

      const nowTime = newDate.getTime()
      const weekDay = newDate.getDay()
      const oneDayTime = 24 * 60 * 60 * 1000

      const mondayTime = (1 - weekDay) * oneDayTime + nowTime
      this.mondayDate = parseTime(mondayTime, '{y}-{m}-{d}')
    }
  }
}
</script>

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