<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: 'pc-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%;
  overflow: auto;
}
.top {
  display: flex;
  padding: 5px 10px 0 10px;
  line-height: 30px;
  .sort {
    flex: 0 0 auto;
    margin-right: 10px;
    font-size: 10px;
    color: #808080;
    .btn {
      display: inline-block;
      height: 20px;
      line-height: 20px;
      padding: 0 5px 0 15px;
      font-size: 12px;
      color: #666;
      border-radius: 2px;
      background: #ffffff url(./img/icon-sort.png) no-repeat 4px center;
      background-size: 10px auto;
      cursor: pointer;
    }
  }
  .count {
    flex: 0 0 auto;
    font-size: 10px;
    color: #808080;
  }
}
.list {
  position: relative;
  padding: 0 4px 0 12px;
  max-width: 640px;
  &:before {
    content: '';
    display: block;
    position: absolute;
    left: 16px;
    top: 10px;
    bottom: 10px;
    height: 100%;
    width: 1px;
    background: #ccc;
  }
  .item {
    position: relative;
    margin-bottom: 10px;
    .top {
      display: flex;
      position: relative;
      padding: 0 10px;
      line-height: 25px;
      .dot {
        flex: 0 0 auto;
        position: absolute;
        left: 0px;
        top: 7px;
        width: 9px;
        height: 9px;
        background-color: #ccc;
        border-radius: 100%;
      }
      .time {
        flex: 0 0 auto;
        margin: 0 16px 0 8px;
        font-size: 12px;
        color: #b7bbc3;
      }
    }
    .content {
      padding: 0 16px;
      .content-box {
        background-color: #383940;
        padding: 10px;
        border-radius: 4px;
        .info {
          display: flex;
          line-height: 25px;
          padding-bottom: 2px;
          .head {
            flex: 0 0 auto;
            display: block;
            margin-right: 10px;
            width: 25px;
            height: 25px;
            border-radius: 100%;
          }
          .text {
            vertical-align: top;
            flex: 1;
          }
        }
      }
    }
  }
}
</style>
