<template>
  <v-container fill-height>
    <div>
      <v-row>
        <v-col>
          <h3>标签结果</h3>
        </v-col>
      </v-row>
      <v-row>
        <v-col>
          <v-divider />
        </v-col>
      </v-row>
      <v-row>
        <v-col
          v-for="item in videoList"
          :key="item.id"
        >
          <ItemCard :video="item" />
        </v-col>
      </v-row>
      <v-row justify="center">
        <v-pagination
          v-model="page"
          :length="length"
          :total-visible="7"
          @input="pageChange"
        />
      </v-row>
    </div>
  </v-container>
</template>

<script>
import { videoTag } from '@/api/media/video'
import ItemCard from '@/components/card/item-card.vue'

export default {
  name: 'TagResult',
  components: {
    ItemCard
  },
  data() {
    return {
      page: 1,
      currentPage: 1,
      size: 12,
      length: 1,
      videoList: []
    }
  },
  created() {
    this.page = parseInt(this.$route.query.page)
    this.getVideos(this.$route.query.tag, this.$route.query.page)
  },
  methods: {
    getVideos(tag, page) {
      videoTag(tag, page)
        .then(res => {
          if (res.code === 0) {
            const pageList = res.data
            this.videoList = pageList.list
            this.currentPage = pageList.currentPage
            this.length = pageList.totalPages

            // 页面跳转后滚动到页面顶部
            this.$vuetify.goTo(0)
          } else {
            console.error(res.msg)
          }
        }).catch(error => {
          console.error(error.message)
        })
    },
    pageChange(page) {
      if (page !== this.currentPage) {
        this.currentPage = page
        this.$router.push({
          path: this.$route.path,
          query: {
            tag: this.$route.query.tag,
            page: page
          }
        })
      }
    }
  }
}
</script>

<style>
</style>
