<template>
  <div class="content">
    <div class="list">
      <div class="card-item" v-for="item in list" @click="toDetail(item.id)">
        <NewsRecommendCard
          :info="item"
          style="width: 80%; margin: 0 auto"
          v-if="item.images_arr.length < 3"
          @handleLike="handleLike(item)"
          @handleCollect="handleCollect(item)"
          @handleForward="handleForward(item)"
          @handleComment="
            info = item;
            commentVisible = true;
          "
        />
        <NewsRecommendImageCard
          v-else
          :info="item"
          style="width: 80%; margin: 0 auto"
        />
      </div>
    </div>
    <div class="loading" v-if="loading">
      <el-col :span="24" v-loading="loading"></el-col>
    </div>
    <div class="empty">
      <el-empty v-if="finished" description="没有更多数据"></el-empty>
    </div>
    <CommentAll
      v-model="commentVisible"
      :info="info"
      :option="comment_option"
      :reportType="comment_option.reportType"
    />
  </div>
</template>

<script>
import { PressService } from "@/common/service";
import NewsRecommendCard from "@/components/news/news-recommend-card.vue";
import NewsRecommendImageCard from "@/components/news/news-recommend-image-card.vue";
import CommentAll from "@/components/module/comment-all.vue";

export default {
  name: "Content",
  components: {
    NewsRecommendCard,
    NewsRecommendImageCard,
    CommentAll,
  },
  data() {
    return {
      params: {
        page: 1, // 页数
        page_num: 20, // 每页数
      },
      list: [],
      loading: false,
      finished: false,
      commentVisible: false,
      info: {},
    };
  },
  computed: {
    comment_option() {
      return {
        // 列表api
        listApi: (params) =>
          PressService.getPressCommentList({
            ...params,
            id: this.info.id,
          }),
        // 点赞
        likeApi: (comment) =>
          PressService.pressCommentTags({
            comment_id: comment.id,
          }),
        // 发布评论
        postApi: (params) =>
          PressService.pressComment({
            ...params,
            id: this.info.id,
          }),
        // 回复评论
        postReplyApi: (params, comment) =>
          PressService.pressSecondComment({
            ...params,
            id: comment.id,
          }),
        reportType: 9,
        // 删除评论
        delApi: (params) => PressService.delComment({ id: params.id }),
      };
    },
  },
  mounted() {
    window.scrollTo(0, 0);
    this.getList();
    if (localStorage.getItem("user_info"))
      this.$store.dispatch("getMarkNum", "news");
  },
  methods: {
    // 刷新
    handleRefresh() {
      this.loading = false;
      this.finished = false;
      this.params.page = 1;
      this.getList();
    },
    // 触底
    TouchBottom() {
      if (!this.finished && !this.loading) {
        this.getList();
      }
    },
    // 获取推荐数据
    getList() {
      this.loading = true;
      let loadingFullscreen = "";
      if (this.params.page == 1) {
        loadingFullscreen = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
      }
      PressService.getRecommendList(this.params)
        .then(({ data }) => {
          const list = data.list;
          loadingFullscreen ? loadingFullscreen.close() : "";
          this.list = this.params.page === 1 ? list : [...this.list, ...list];
          if (
            list.length < this.params.page_num ||
            this.list.length == data.total_count
          ) {
            this.finished = true;
          } else {
            this.params.page++;
          }
        })
        .catch(() => {
          this.finished = true;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 点赞
    handleLike(item) {
      PressService.PressTags({ id: item.id }).then(({ data, msg }) => {
        item.is_praise = data.tags;
        item.praise_num = data.tags
          ? Number(item.praise_num) + 1
          : Number(item.praise_num) - 1;
        this.$message.success(msg);
      });
    },
    // 收藏
    handleCollect(item) {
      PressService.pressCollect({
        id: item.id,
      }).then(({ data, msg }) => {
        item.is_collect = data.status;
        item.collect_num = data.status
          ? Number(item.collect_num) + 1
          : Number(item.collect_num) - 1;
        this.$message.success(msg);
      });
    },
    // 分享
    handleForward(item) {
      PressService.pressTransmit({ id: item.id });
    },
    // 跳转详情
    toDetail(id) {
      this.$router.push({
        path: "/news-details",
        query: {
          press_id: id,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  width: calc(100% - 3rem);
  margin-left: 3rem;
  min-width: 750px;
  padding-left: 10px;
  box-sizing: border-box;
  .tab {
    margin-bottom: 70px;
  }
  .list {
    padding-right: 0;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    .card-item {
      width: 100%;
      margin: 0 0 20px;
      height: 226px;
    }
  }
  .all {
    padding: 15px;
    box-sizing: border-box;
    .screen {
    }
    .sort {
      margin: 30px 0;
    }
    .card-item {
      margin-bottom: 20px;
    }
  }
  .loading {
    height: 40px;
  }
  .empty {
    margin-top: 40px;
  }
}
</style>
