<template>
  <div>
    <el-row id="movie-list">
      <el-col :md="6">
        <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
          <site-notice />
        </el-row>
        <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
          <hot-video />
        </el-row>
      </el-col>
      <el-col :md="18">
        <!--电影列表，与推荐列表-->
        <el-row
          v-if="dataList.length !== 0"
          v-infinite-scroll="load"
          infinite-scroll-disabled="loading"
          infinite-scroll-distance="10"
        >
          <!--电影列表-->
          <el-col :md="24">
            <el-col v-for="(item, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
              <video-card :video="item" />
            </el-col>
          </el-col>
        </el-row>
        <el-row v-else class="not-result">
          <el-col :span="12" :offset="6">
            <img src="@/assets/img/icon/not-collection.png">
            <div>推荐数据正在计算中</div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import VideoCard from '@/components/card/VideoCard'
import HotVideo from '@/components/card/HotVideo'
import SiteNotice from '@/components/card/SiteNotice'
import { videoRecommend } from '@/api/video'

export default {
  name: 'Index',
  components: { VideoCard, HotVideo, SiteNotice },
  data() {
    return {
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      nextId: '1',
      dataList: [],
      loading: false,
      max: 0
    }
  },
  created() {
    this.videoRecommendWrapper(this.nextId)
  },
  mounted() {
    // 当窗口宽度改变时获取屏幕宽度
    window.onresize = () => {
      return () => {
        window.screenWidth = document.body.clientWidth
        this.screenWidth = window.screenWidth
      }
    }
  },
  methods: {
    videoRecommendWrapper(nextId) {
      videoRecommend(nextId).then(resp => {
        if (resp.code === 0) {
          const respData = resp.data
          if (respData.length === 0) {
            this.loading = false
            return
          }

          for (const item of respData) {
            this.dataList.push(item)
          }
          this.loading = false
        } else {
          this.$notify(
            {
              title: '提示',
              message: '获取数据失败, 请重新刷新页面',
              type: 'warning',
              duration: 3000
            }
          )
        }
      })
    },
    load() {
      this.max++
      if (this.max > 1) {
        return
      }

      this.loading = true
      setTimeout(() => {
        this.videoRecommendWrapper(this.nextId)
      }, 1000)
    }
  }
}
</script>

<style scoped>
/*处于手机屏幕时*/
@media screen and (max-width: 768px){
  #movie-list {
    padding-top: 8px;
    padding-left: 0.5%;
    padding-right: 0.5%;
  }
}

#movie-list {
  padding-top: 15px;
  padding-left: 6%;
  padding-right: 6%;
}

.not-result {
  padding-top: 100px;
  padding-bottom: 100px;
  text-align: center;
}
</style>
