<template>
  <div class="report-container" ref="listBox" @scroll="getMore">
    <div class="top">
      <div class="sort">
        <div class="btn" @click="_sort">
          {{ sort ? '倒序浏览' : '正序浏览' }}
        </div>
      </div>
      <div class="count">共{{ count }}条报道</div>
    </div>
    <div class="list" ref="list">
      <div class="item" v-for="item in newList" :key="Number(item.id)">
        <div class="top">
          <div class="dot"></div>
          <div class="time">{{ item.createTime }}</div>
        </div>
        <div class="content">
          <div class="content-box">
            <div class="text" v-if="item.content">
              <report-text :content="item.content"></report-text>
            </div>
            <div class="img" v-if="item.type == 1">
              <report-img :imgs="item.imgList || []"></report-img>
            </div>
            <div class="video" v-if="item.type == 2">
              <report-video
                :video="item.videoPlayUrl"
                :poster="item.videoCoverImg"
                :source="item.source"
              ></report-video>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getReportListFront, getReportInfo } from '@/plugins/API/apiReport';
import reportText from './reportType/reportText';
import reportImg from './reportType/reportImg';
import reportVideo from './reportType/reportVideo';

export default {
  name: 'menu-report',
  components: {
    reportText,
    reportImg,
    reportVideo
  },
  props: {
    id: {}
  },
  data() {
    return {
      isMore: true,
      num: 10,
      sort: true,
      count: 0,
      list: []
    };
  },
  computed: {
    newList() {
      const list = this.list;
      return list.sort((a, b) => {
        if (this.sort) {
          return new Date(b.createTime) - new Date(a.createTime);
        } else {
          return new Date(a.createTime) - new Date(b.createTime);
        }
      });
    },
    page() {
      return parseInt(this.list.length / this.num, 10) + 1;
    }
  },
  mounted() {
    this.$Bus.$on('bus-reportAdd', this.addList);
    this.getList();
  },
  methods: {
    getMore() {
      const listBoxT = this.$refs.listBox && this.$refs.listBox.scrollTop;
      const listBoxH = this.$refs.listBox && this.$refs.listBox.clientHeight;
      const listH = this.$refs.list && this.$refs.list.clientHeight;
      if (listBoxT + listBoxH > listH - 100) {
        this.getList();
      }
    },
    async getList() {
      if (!this.id || !this.isMore) {
        return false;
      }
      this.isMore = false;
      const res = await getReportListFront({
        liveId: this.id,
        page: this.page,
        num: this.num,
        sort: this.sort ? 1 : 2
      });
      this.isMore = true;
      if (res.code === 200 && res.errorCode === 0) {
        this.count = res.data.count;
        const data = res.data.list;
        if (data.length < this.num) {
          this.isMore = false;
        }
        this.list = this.list.filter(item => {
          return !data.some(x => {
            return Number(x.id) === Number(item.id);
          });
        });
        this.list = [...this.list, ...data];
      } else {
        this.$layer && this.$layer.msg(res.errorMessage);
      }
    },
    addList(obj) {
      if (obj.status === 1) {
        this.getReportInfo(obj);
      } else {
        if (
          this.list.some(x => {
            return Number(x.id) === Number(obj.id);
          })
        ) {
          this.count--;
          this.list = this.list.filter(item => {
            return Number(item.id) !== Number(obj.id);
          });
        }
      }
    },
    async getReportInfo(obj) {
      const res = await getReportInfo({
        id: obj.id,
        time: obj.time
      });
      if (res.code === 200 && res.errorCode === 0) {
        const data = res.data;
        if (
          this.list.some(x => {
            return Number(x.id) === Number(data.id);
          })
        ) {
          this.list = this.list.map(item => {
            if (Number(item.id) === Number(data.id)) {
              return data;
            } else {
              return item;
            }
          });
        } else {
          this.count++;
          this.list = [data, ...this.list];
        }
      } else {
        this.$layer.msg(res.errorMessage);
      }
    },
    _sort() {
      this.sort = !this.sort;
      this.isMore = true;
      this.list = [];
      this.getList();
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-reportAdd', this.addList);
  }
};
</script>
<style lang="less" scoped>
.report-container {
  height: 100%;
  background-color: #f0f1f3;
  overflow: auto;
}
.top {
  display: flex;
  padding: 0.05rem 0.1rem 0 0.1rem;
  line-height: 0.3rem;

  .sort {
    flex: 0 0 auto;
    margin-right: 0.1rem;
    font-size: 0.1rem;
    color: #808080;

    .btn {
      display: inline-block;
      height: 0.2rem;
      line-height: 0.2rem;
      padding: 0 0.05rem 0 0.15rem;
      font-size: 0.12rem;
      color: #666;
      border-radius: 0.02rem;
      background: #ffffff url(./img/icon-sort.png) no-repeat 0.04rem center;
      background-size: 0.1rem auto;
      cursor: pointer;
    }
  }
  .count {
    flex: 0 0 auto;
    font-size: 0.1rem;
    color: #808080;
  }
}
.list {
  position: relative;
  padding: 0 0.04rem 0 0.12rem;
  max-width: 6.4rem;

  &:before {
    content: '';
    display: block;
    position: absolute;
    left: 0.16rem;
    top: 0.1rem;
    bottom: 0.1rem;
    height: 100%;
    width: 0.01rem;
    background: #ccc;
  }
  .item {
    position: relative;
    margin-bottom: 0.1rem;

    .top {
      display: flex;
      position: relative;
      padding: 0 0.1rem;
      line-height: 0.25rem;

      .dot {
        flex: 0 0 auto;
        position: absolute;
        left: 0;
        top: 0.07rem;
        width: 0.09rem;
        height: 0.09rem;
        background-color: #ccc;
        border-radius: 100%;
      }
      .time {
        flex: 0 0 auto;
        margin: 0 0.16rem 0 0.08rem;
        font-size: 0.12rem;
        color: #b7bbc3;
      }
    }
    .content {
      padding: 0 0.16rem;

      .content-box {
        background-color: #fff;
        padding: 0.1rem;
        border-radius: 0.04rem;

        .info {
          display: flex;
          line-height: 0.25rem;
          padding-bottom: 0.02rem;

          .head {
            flex: 0 0 auto;
            display: block;
            margin-right: 0.1rem;
            width: 0.25rem;
            height: 0.25rem;
            border-radius: 100%;
          }
          .text {
            vertical-align: top;
            flex: 1;
            font-size: 0.12rem;
            color: #333;
            word-break: break-all;
            text-align: justify;
          }
        }
      }
    }
  }
}
</style>
