<!--  -->
<template>
  <div class="followUp_box" :style="'height:' + fullHeight + 'px'">
    <!-- 头部个人信息 -->
    <div class="followUp_headrBox">
      <div class="followUp_divBox">
        <img
          src="../../assets/image/new/30.png"
          alt
          class="followUp_headrBox-img"
          @click="goBack()"
        />
        <div class="followUp_headrBox-tlt">我的跟帖</div>
      </div>
    </div>

    <!-- 主内容 -->
    <mescroll-vue
      ref="mescroll"
      class="followUp_box_mescroll"
      :down="mescrollDown"
      :up="mescrollUp"
      @init="mescrollInit"
    >
      <div
        class="followUp_main"
        v-loading="loading"
        element-loading-text="拼命加载中"
        v-if="interflowShow"
      >
        <ul
          class="followUp_main-ul"
          v-for="(item, index) in redata"
          :key="index"
        >
          <li class="followUp_main-list">
            <div class="followUp_main-list-div1">
              <img
                preview="1"
                v-lazy="'http://192.168.1.8:8002/' + item.user_picture"
                alt=""
                class="followUp_main-list-div1Img"
              />
              <div class="followUp_main-list-div1-div">
                <div class="followUp_main-list-div1-div-name">
                  我 评论
                  <span class="followUp_main-list-div1-div-name1">{{
                    item.username
                  }}</span>
                </div>
                <div class="followUp_main-list-div1-div-txt">
                  {{ item.problem }}
                </div>
              </div>
            </div>
            <div class="followUp_main-list-div2">
              <div class="followUp_main-list-div2-time">{{ item.time }}</div>
              <div
                class="followUp_main-list-div2-del"
                @click="delData(item.id)"
              >
                删除
              </div>
            </div>
          </li>
        </ul>
      </div>

      <!-- 暂空判断 -->
      <div class="collect_judge" id="empty" v-else>
        <img src="../../assets/image/wu.png" alt class="collect_judge_img" />
        <div class="collect_judge_text">抱歉，暂无跟帖</div>
      </div>
    </mescroll-vue>
  </div>
</template>

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    MescrollVue,
  },
  data() {
    //这里存放数据
    return {
      fullHeight: document.documentElement.clientHeight,
      redata: [],
      interflowShow: true, //暂无数据
      // 上拉加载下拉刷新
      mescroll: null, // mescroll实例对象
      mescrollDown: {}, //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了)
      mescrollUp: {
        // 上拉加载的配置.
        callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
        //以下是一些常用的配置,当然不写也可以的.
        page: {
          num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
          size: 6, //每页数据条数,默认10
        },
        isBounce: true, //根据记录的滚动条高度回弹
        htmlNodata: '<p class="upwarp-nodata">-- 已经到底了! --</p>',
        noMoreSize: 6, //如果列表已无数据,可设置列表的总数量要大于5才显示无更多数据;
        // 避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
        // 这就是为什么无更多数据有时候不显示的原因
        toTop: {
          //回到顶部按钮
          src:
            "http://pic.51yuansu.com/pic3/cover/00/65/23/5896fa4bc81fc_610.jpg", //图片路径,默认null,支持网络图
          offset: 500, //列表滚动1000px才显示回到顶部按钮
        },
        onScroll: function (mescroll, y, isUp) {
          //吸顶
          // if (y > 500) {
          //   self.navBarFixed = true;
          // } else {
          //   self.navBarFixed = false;
          // }
        },
        lazyLoad: {
          use: true, // 是否开启懒加载,默认false
        },
      },
      aggregate: "", //后端传来数据的总条数
      loading: true,
      // 上拉加载下拉刷新
    };
  },
  beforeRouteEnter(to, from, next) {
    // 如果没有配置顶部按钮或isBounce,则beforeRouteEnter不用写
    next((vm) => {
      // 滚动到原来的列表位置,恢复顶部按钮和isBounce的配置
      vm.$refs.mescroll && vm.$refs.mescroll.beforeRouteEnter();
    });
  },
  beforeRouteLeave(to, from, next) {
    // 如果没有配置顶部按钮或isBounce,则beforeRouteLeave不用写
    // 记录列表滚动的位置,隐藏顶部按钮和isBounce的配置
    this.$refs.mescroll && this.$refs.mescroll.beforeRouteLeave();
    next();
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function () {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },
  },
  //方法集合
  methods: {
    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    // mescroll组件初始化的回调,可获取到mescroll对象
    mescrollInit(mescroll) {
      this.mescroll = mescroll; // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
    },
    upCallback(page, mescroll) {
      var that = this;
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/reply_list",
          params: {
            page: that.mescrollUp.page.num,
          },
        })
        .then((res) => {
          var nums = res.data.nums;
          if (that.aggregate === "") {
            //存变量
            that.aggregate = nums;
            var b = "10";
            that.aggregate = that.aggregate / b;
            that.aggregate = Math.ceil(that.aggregate); //将小数点转换为整数
            console.log(that.aggregate);
          }

          if (res.data.nums !== 0) {
            // that.redata = res.data.data;
            // 如果是第一页需手动置空列表
            if (page.num === 1) that.redata = [];
            // 把请求到的数据添加到列表
            that.redata = that.redata.concat(res.data.data);
            that.interflowShow = true;
            // 数据渲染成功后,隐藏下拉刷新的状态
            that.$nextTick(() => {
              mescroll.endSuccess(res.data.data.length);
            });
          } else {
            that.interflowShow = false;
          }
          that.loading = false;
        })
        .catch((err) => {
          mescroll.endErr();
          console.log(err);
        });
    },

    // 返回
    goBack() {
      var that = this;
      that.$router.replace("/person");
    },

    // 跟帖列表数据接口
    listData() {
      var that = this;
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/reply_list",
          parsm: {
            page: 1,
          },
        })
        .then((res) => {
          if (res.data.nums !== 0) {
            that.redata = res.data.data;
            that.interflowShow = true;
          } else {
            that.interflowShow = false;
          }
        })
        .catch((err) => {});
    },

    // 跟帖删除事件
    delData(id) {
      var that = this;
      this.$confirm("确定删除当前评论？")
        .then((_) => {
          that
            .$axios({
              method: "get",
              url: "http://192.168.1.8:8002/user_reply",
              params: {
                id: id,
                ret: "reply",
              },
            })
            .then((res) => {
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                  duration: 1500,
                });
                that.listData();
              } else {
                this.$notify.error({
                  title: "错误",
                  message: "删除出错了！！",
                  duration: 1500,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch((_) => {
          console.log("取消删除");
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.get_boderHeight();
    // this.listData();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss'>
@import "../../assets/scss/bus.scss";
//@import url(); 引入公共css类

.followUp_box {
  background: #f7f8fd;
}
// 个人头部信息
.followUp_headrBox {
  background: -webkit-linear-gradient(right, #0dc6b6, #0cd3aa, #1dd69c);
  position: fixed;
  width: 100%;
  z-index: 100;
}
.followUp_divBox {
  display: flex;
  justify-content: space-between;
  width: 55%;
  margin: 0 0 0 0.5rem;
}
.followUp_headrBox-tlt {
  text-align: center;
  font-size: 18px;
  color: #fff;
  padding: 8px 0 8px 0;
}
.followUp_headrBox-img {
  width: 0.5rem;
  height: 0.5rem;
  vertical-align: middle;
  margin-top: 0.3rem;
}

// 主内容
.followUp_main {
}
.followUp_main-ul {
  background: #fff;
  //   border: 1px solid #000;
  margin: 0.15rem 0 0 0;
}

.followUp_box_mescroll {
  padding: 1.1rem 0 0 0;
}
.followUp_main-list {
  display: flex;
  justify-content: space-between;
  width: 95%;
  margin: 0 auto;
  padding: 0.25rem 0 0.25rem 0;
}
.followUp_main-list-div1 {
  display: flex;
}
.followUp_main-list-div1Img {
  width: 1rem;
  height: 1rem;
  border-radius: 50px;
}
.followUp_main-list-div1-div {
  margin: 0 0 0 0.15rem;
}
.followUp_main-list-div1-div-name {
  color: #737373;
  font-size: 11px;
  text-align: left;
}
.followUp_main-list-div1-div-name1 {
  color: #00be8f;
  font-size: 11px;
  text-align: left;
}
.followUp_main-list-div1-div-txt {
  color: #000;
  font-size: 11px;
  text-align: left;
  margin: 0.05rem 0 0 0;
}

.followUp_main-list-div2 {
}
.followUp_main-list-div2-time {
  color: #727272;
  text-align: right;
  font-size: 11px;
}
.followUp_main-list-div2-del {
  color: red;
  text-align: right;
  line-height: 0.8rem;
}

// elementui样式
.el-message-box {
  width: 355px;
}

// 暂空处理
.collect_judge {
  margin: 45% 0 0 0;
}

.collect_judge_img {
  margin: 0 auto;
}

.collect_judge_text {
  font-size: 25px;
  color: #bfbfbf;
  margin: 0.5rem 0 0 0;
  text-align: center;
}

.followUp_box_mescroll .mescroll-upwarp {
  background: #f7f8fd;
}
</style>