<template>
  <!-- 时间间隔栏 -->
  <!-- <Barrier /> -->

  <scroll-view
    :lower-threshold="300"
    :scrollTop="top"
    :scrollY="true"
    :upper-threshold="300"
    @scroll="scroll"
    @scrolltolower="handleScrollBottom"
    @scrolltoupper="handleScrollTop"
    id="scroll"
  >
    <!-- 时间轴 -->
    <u-divider bg-color="#f3f4f6" class="pt-5" fontSize="14px" v-if="topEnd">到顶啦</u-divider>

    <TimeBox />

    <u-divider bg-color="#f3f4f6" class="pb-5" fontSize="14px" v-if="bottomEnd">到底啦</u-divider>
  </scroll-view>
</template>

<script>
// import Barrier from './component/Barrier.vue';
import { mapState, mapMutations } from 'vuex';
import TimeBox from './component/TimeBox.vue';

export default {
  name: 'TimeLine',
  components: { TimeBox },
  data() {
    return { top: 0 };
  },

  computed: {
    ...mapState('role', ['visibleRoles']),
    ...mapState('task', ['scrollTop', 'showTips', 'tasks', 'topEnd', 'bottomEnd']),
  },

  mounted() {
    this.setDatumPoint();
  },

  methods: {
    ...mapMutations('role', ['setRoleId']),
    ...mapMutations('task', ['setScrollTop', 'setShrink', 'setUpTasks', 'setDownTasks']),

    // 滚动
    scroll(e) {
      this.top = e.detail.scrollTop;
      this.setShrink(this.top > this.scrollTop);
      this.setScrollTop(this.top);
    },

    // 滚动到顶部
    async handleScrollTop() {
      if (this.topEnd) return;
      const startTime = this.tasks[0].planStart - 0;
      console.log('this.topEnd: ', this.topEnd);
      console.log('this.tasks: ', this.tasks);
      console.log('this.tasks[0].plugins: ', this.tasks[0].plugins);
      if (this.tasks[0].plugins && this.tasks[0].plugins.length === 0 && !this.topEnd) {
        // 没有数据时 自动加载数据
        console.log('没有数据时: ');
        const addTasks = [
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, -3, 'day').valueOf(),
          },
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, -2, 'day').valueOf(),
          },
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, -1, 'day').valueOf(),
          },
        ];
        this.setUpTasks([...addTasks.concat(this.tasks)]);
      } else {
        // 有数据时
        console.log('有数据时: ');
        const upQuery = {
          timeNode: startTime,
          queryType: 0,
          queryNum: 6,
        };
        await this.$emit('getTasks', upQuery);
      }
    },

    // 滚动到底部
    async handleScrollBottom() {
      if (this.bottomEnd) return;
      const startTime = this.tasks[this.tasks.length - 1].planStart - 0;
      if (this.tasks[0].plugins && this.tasks[0].plugins.length === 0 && !this.topEnd) {
        // 没有数据时 自动加载数据
        console.log('没有数据时: ');
        const addTasks = [
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, 1, 'day').valueOf(),
          },
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, 2, 'day').valueOf(),
          },
          {
            panel: {},
            plugins: [],
            planStart: this.$t.time.add(startTime, 3, 'day').valueOf(),
          },
        ];
        this.setDownTasks([...this.tasks.concat(addTasks)]);
      } else {
        // 时间基准点=最后一个任务的开始时间+当前时间颗粒度
        const cycle = this.$t.time.computeCycle('天');
        const timeNode = this.$t.time.add(startTime, 1, cycle).valueOf();
        const downQuery = {
          timeNode,
          queryType: 1,
          queryNum: 6,
        };
        await this.$emit('getTasks', downQuery);
      }
    },

    // 设置基准点
    setDatumPoint() {
      const { tasks } = this;
      if (tasks && tasks.length) {
        let tasksHeight = 0;
        const scrollHeight = document.getElementById('scroll').clientHeight;
        let width = document.documentElement.clientWidth;
        for (let i = 0; i < 3; i++) {
          if (tasks[i].panel && tasks[i].panel.height) {
            // 如果后台返回了高度，就用返回得任务面板高度 + 固定边距42
            tasksHeight += +tasks[i].panel.height + 42;
          } else {
            // 如果没有返回高度，就用rem + 固定边距42
            // 因为 u-card 在没有返回高度时 固定高度为 h-16 = 4rem
            // 所里这里 += 1rem + 42 = width / 20 + 42
            tasksHeight += (width / 20) * 4 + 42;
          }
        }
        this.top = tasksHeight - scrollHeight / 2;
        console.log('this.top: ', this.top);
      }
    },
  },
};
</script>
