<template>
  <div>
    <el-row class="movie-list">
      <el-col :md="24">
        <el-card :if="!user" :body-style="{ padding: '0px' }" class="card">
          <div slot="header" class="clearfix">
            <el-row>
              <el-col :md="1">
                <el-avatar>
                  <el-image :src="user.avatarUrl"/>
                </el-avatar>
              </el-col>
              <el-col :md="23">
                <span>{{ user.screenName }}</span>
                <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'" />
                <el-button
                  type="danger"
                  size="mini"
                  :icon="followButton.icon"
                  @click="followUser(user.userId)"
                >
                  <span>{{followButton.text}}</span>
                </el-button>
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-message"
                  @click="sendMessage(user.userId)"
                >
                  <span>发消息</span>
                </el-button>
              </el-col>
            </el-row>
            <el-row>
              <span v-if="user.signature !== null" v-html="user.signature">
              </span>
              <span v-if="user.signature === undefined || user.signature === null">-</span>
            </el-row>
            <el-row>
              <br>
              <router-link target="_blank" :to="`/user/${user.userId}/following`">
                <span class="el-icon-user">关注数: {{ user.following }}</span>
              </router-link>
              <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'" />
              <router-link target="_blank" :to="`/user/${user.userId}/follower`">
                <span class="el-icon-user">粉丝数: {{ user.follower }}</span>
              </router-link>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :md="24" class="movie-list">
        <el-tabs v-model="activeName" @tab-click='tabClick'>
          <el-tab-pane name="video">
            <span slot="label">
              视频<el-badge :value="userContentData.videoCount" :max="9999" class="item" type="warning"/>
            </span>
            <div v-if="activeName === 'video'">
              <el-col v-for="(video, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
                <video-card :video="video" />
              </el-col>
            </div>
          </el-tab-pane>
          <el-tab-pane name="audio">
            <span slot="label">
              音频<el-badge :value="userContentData.audioCount" :max="9999" class="item" type="warning"/>
            </span>
            <div v-if="activeName === 'audio'">
              <el-col v-for="(audio, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
                <audio-card :audio="audio"/>
              </el-col>
            </div>
          </el-tab-pane>
          <el-tab-pane name="image">
            <span slot="label">
              相册<el-badge :value="userContentData.albumCount" :max="9999" class="item" type="warning"/>
            </span>
            <div v-if="activeName === 'image'">
              <el-col v-for="(album, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
                <image-album-card :imageAlbum="album" />
              </el-col>
            </div>
          </el-tab-pane>
          <el-tab-pane name="article">
            <span slot="label">
              文章<el-badge :value="userContentData.articleCount" :max="9999" class="item" type="warning"/>
            </span>
            <div v-if="activeName === 'article'">
              <el-col v-for="(article, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
                <article-card :article="article" />
              </el-col>
            </div>
          </el-tab-pane>
<!--          <el-tab-pane name="status">
            <span slot="label">
              状态<el-badge :value="userContentData.statusCount" :max="9999" class="item" type="warning"/>
            </span>
            <div v-if="activeName === 'status'">
              <el-col v-for="(status, index) in dataList" :key="index" :md="16" :sm="12" :xs="12">
                <span>发布的状态</span>
              </el-col>
            </div>
          </el-tab-pane>-->
        </el-tabs>
      </el-col>
      <el-col :span="24" class="pagination">
        <el-pagination
          background
          :small="screenWidth <= 768"
          hide-on-single-page
          layout="prev, pager, next"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="totalSize"
          @current-change="handleCurrentChange"
          @prev-click="handleCurrentChange"
          @next-click="handleCurrentChange"
        />
      </el-col>
    </el-row>

    <el-row v-if="showEmpty" class="not-result">
      <el-col :span="12" :offset="6">
        <img src="@/assets/img/icon/not-collection.png">
        <div>该用户还没发布任何东西呢</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import UserCard from '@/components/card/UserCard'
import StatusCard from '@/components/card/StatusCard'
import VideoCard from '@/components/card/VideoCard'
import AudioCard from '@/components/card/AudioCard'
import ImageAlbumCard from '@/components/card/ImageAlbumCard'
import ArticleCard from '@/components/card/ArticleCard'

import { getUserInfo, checkRelation, followUser, unfollowUser } from "@/api/user";
import { getUserContentData, getUserVideos } from "@/api/video";
import { getUserAlbums1 } from "@/api/image";
import { getUserAudios } from "@/api/audio";
import { getUserArticles } from "@/api/article";
import { userStatus } from "@/api/status";

export default {
  name: 'Home',
  components: { UserCard, StatusCard, VideoCard, ImageAlbumCard, AudioCard, ArticleCard },
  data() {
    return {
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      user: null,
      userId: null,
      followButton: {
        icon: 'el-icon-plus',
        text: '关注'
      },
      activeName: 'video',
      currentPage: 1,
      pageSize: 12,
      totalSize: 0,
      dataList: [],
      showEmpty: true,
      userContentData: null
    }
  },
  created() {
    this.userId = this.$route.params.id
    getUserInfo(this.userId).then(resp => {
      if (resp.code === 0) {
        this.user = resp.data
        const path = this.$route.path
        if (path.endsWith("video")) {
          this.activeName = 'video'
          document.title = this.user.screenName + '的视频'
        } else if (path.endsWith("image")) {
          this.activeName = 'image'
          document.title = this.user.screenName + '的相册'
        } else if (path.endsWith("audio")) {
          this.activeName = 'audio'
          document.title = this.user.screenName + '的音频'
        } else if (path.endsWith("article")) {
          this.activeName = 'article'
          document.title = this.user.screenName + '的文章'
        } else if (path.endsWith("status")) {
          this.activeName = 'status'
          document.title = this.user.screenName + '的状态'
        } else {
          document.title = this.user.screenName + '的个人主页'
        }
        this.getData()
      }
    })
    checkRelation(this.userId).then(resp => {
      if (resp.code === 0) {
        if (resp.data) {
          this.followButton.text = '已关注'
          this.followButton.icon = 'el-icon-check'
        }
      }
    })
    getUserContentData(this.userId).then(resp => {
      if (resp.code === 0) {
        this.userContentData = resp.data
      }
    })
  },
  mounted() {
    // 当窗口宽度改变时获取屏幕宽度
    window.onresize = () => {
      return () => {
        window.screenWidth = document.body.clientWidth
        this.screenWidth = window.screenWidth
      }
    }
  },
  watch: {
    $route(){
      this.$router.go()
    }
  },
  methods: {
    followUser(userId) {
      if (this.followButton.text === '关注') {
        followUser(userId).then(resp => {
          if (resp.code === 0) {
            this.followButton.text = '已关注'
            this.followButton.icon = 'el-icon-check'
          }
        })
      } else {
        unfollowUser(userId).then(resp => {
          if (resp.code === 0) {
            this.followButton.text = '关注'
            this.followButton.icon = 'el-icon-plus'
          }
        })
      }
    },
    sendMessage(userId) {
      console.log('发送消息')
    },
    handleCurrentChange(pageNumber) {
      this.currentPage = pageNumber
      this.getData()
      // 回到顶部
      scrollTo(0, 0)
    },
    tabClick(tab) {
      this.activeName = tab.name
      this.goToTab(this.activeName)
    },
    goToTab(activeName) {
      const path = '/user/' + this.userId + '/' + activeName
      if (this.$route.path === path) {
        this.$router.go(0)
        return
      }
      this.$router.push(path)
    },
    getData() {
      this.dataList = []
      if (this.activeName === 'video') {
        this.userVideoListWrapper(this.currentPage, this.userId)
      } else if (this.activeName === 'image') {
        getUserAlbums1(this.userId).then(resp => {
          if (resp.code === 0) {
            const respData = resp.data
            if (respData.length !== 0) {
              this.showEmpty = false
              for (const item of respData) {
                this.dataList.push(item)
              }
            } else {
              this.showEmpty = true
            }
          }
        })
      } else if (this.activeName === 'audio') {
        getUserAudios(this.userId, this.currentPage).then(resp => {
          if (resp.code === 0) {
            const respData = resp.data.list
            if (respData.length !== 0) {
              this.showEmpty = false
              for (const item of respData) {
                this.dataList.push(item)
              }
            } else {
              this.showEmpty = true
            }
          }
        })
      } else if (this.activeName === 'article') {
        getUserArticles(this.userId, this.currentPage).then(resp => {
          if (resp.code === 0) {
            const respData = resp.data.list
            if (respData.length !== 0) {
              this.showEmpty = false
              for (const item of respData) {
                this.dataList.push(item)
              }
            } else {
              this.showEmpty = true
            }
          }
        })
      } else if (this.activeName === 'status') {
        this.userStatusListWrapper(this.currentPage, this.userId)
      }
    },
    userVideoListWrapper(pageNumber, userId) {
      getUserVideos(userId, pageNumber).then(resp => {
        if (resp.code === 0) {
          const respData = resp.data
          this.dataList = respData.list
          this.totalSize = respData.totalSize
          if (this.dataList.length !== 0) {
            this.showEmpty = false
          } else {
            this.showEmpty = true
          }
        }
      })
    },
    userStatusListWrapper(pageNumber, userId) {
      userStatus(userId, pageNumber).then(resp => {
        if (resp.code === 0) {
          this.dataList = resp.data.list
          if (this.dataList.length === 0) {
            this.showEmpty = true
          } else {
            this.showEmpty = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.movie-list {
  padding-top: 15px;
  padding-left: 6%;
  padding-right: 6%;
}

.pagination {
  text-align: center;
  padding: 10px;
}

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

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

.item {
  margin-top: 10px;
  margin-right: 40px;
}
</style>
