<template>
  <view :style="{ height: height }" class="flex flex-col overflow-hidden">
    <Title />
    <view class="container flex flex-col flex-1 overflow-hidden bg-gray-100">
      <Roles />
      <Globals />
      <TimeLine class="flex-1 overflow-hidden" />
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
export default {
  data() {
    return { title: 'Hello', height: '', scrollHeight: null };
  },

  computed: {
    ...mapState('user', ['user', 'token']),
    ...mapState('home', ['visibleRoles', 'roleId', 'timeNode', 'timeUnit']),
  },

  async onLoad(options) {
    console.log('options: ', options);
    // this.openPage();
    let TOKEN = uni.getStorageSync('anyringToken');
    if (!TOKEN) {
      await this.getUserId('1217651354919636992');
    }
    const params = { projectId: '1235555' };
    // 根据项目id获取项目信息
    await this.getProjectById(params);
    // 根据项目id获取角色列表
    await this.getRoles(params);
    this.setInitialRoleId(this.visibleRoles);
    // 根据时间基准点和角色查找定期任务
    await this.getTasks(0);
    await this.getTasks(1);
  },

  mounted() {
    this.height = window.screen.height + 'px';
  },

  methods: {
    ...mapMutations('user', ['setToken', 'setUser']),
    ...mapMutations('home', ['setProject', 'setInvisibleRoles', 'setVisibleRoles', 'setRoleId', 'setUpTasks', 'setDownTasks']),
    ...mapActions('user', ['getUserId']),
    ...mapActions('home', ['getProjectById', 'getRoles', 'handleRegularTask']),

    openPage() {
      console.log('open');
      this.$u.route('/pages/pinch/pinch');
    },

    // 设置 初始显示角色信息
    setInitialRoleId(visibleList) {
      const index = visibleList.findIndex(item => item.mine);
      const currentRole = index > 0 ? visibleList[index] : visibleList[0];
      const currentRoleId = currentRole ? currentRole.id : '';
      this.setRoleId(currentRoleId);
    },

    /**
     * 根据时间基准点和角色查找定期任务
     * @param {string} roleId 角色id
     * @param {string} timeNode 时间基准点 默认当前
     * @param {string} timeUnit 时间颗粒度 默认天
     * @param {string} queryNum 查找颗粒度数量 默认3个
     * @param {string} queryType 0向上查找 1向下查找(默认) 下查包含自己，上查不包含
     */
    async getTasks(queryType) {
      try {
        const { roleId, timeNode, timeUnit } = this;
        const params = {
          roleId,
          timeNode,
          timeUnit,
          queryNum: 3,
          queryType,
        };
        const res = await this.handleRegularTask(params);
        console.log('res: ', res);
        queryType === 0 ? this.setUpTasks(res) : this.setDownTasks(res);
      } catch (error) {
        console.log('error: ', error);
      }
    },
  },
};
</script>
