<template>
  <div class="task-list" :style="{ height: 'calc(100vh - 160px - (' + globalHeight + 'px))' }">
    <div class="task-box" v-for="(item, index) in regularTasks" :key="index">
      <div class="task-time flex items-center justify-between">
        <div class="flex items-center">
          <PlayCircleOutlined style="font-size: 23px; color: #999999" />
          <span>{{ dayjs(item.planStart).format('D日 HH:mm') }}</span>
        </div>
        <div class="task-action"></div>
      </div>

      <div class="task-info">
        <div>
          <div class="task-card">
            <div class="task-name cursor-pointer" @click="toDetail(item)">
              <template v-for="v in item.plugins">
                <template v-if="v[0].pluginId == 1">{{ item.name }}</template>
              </template>
            </div>

            <div class="task-con" v-if="item.sonList && item.sonList.length > 0">
              <div v-for="(val, key) in item.sonList" :key="key">
                <!-- <a-checkbox>{{ val.name }}</a-checkbox> -->
                <span class="son-task-name cursor-pointer" @click.stop="toSonDetail(item, val.detailId)">{{ val.name }}</span>
              </div>
            </div>
            <!-- <div class="open-icon" v-if="item.sonList" @click="openCard">
              <img />
            </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, watch, reactive } from 'vue';
import { useStore } from 'vuex';
import dayjs from 'dayjs';
import { PlayCircleOutlined } from '@ant-design/icons-vue';
import { getRegularTask, findSonTask } from 'apis';

const store = useStore();
const project = computed(() => store.state.projects.project); // 项目信息
const roleId = computed(() => store.state.role.roleId); // 当前角色
const globalHeight = computed(() => store.state.task.globalHeight); // 日常任务面板高度
const sessionGlobalHeight = sessionStorage.getItem('globalHeight'); // 日常任务面板高度缓存
const regularTasks = computed(() => store.state.task.regularTasks); // 定期任务
const taskObj = reactive({ tasks: [] }); // 定期任务
const sessionTasks = sessionStorage.getItem('regularTasks'); // 定期任务缓存
const subProjectInfo = computed(() => store.state.task.subProjectInfo); // 子课题信息

if (sessionGlobalHeight) {
  store.commit('task/setGlobalHeight', sessionGlobalHeight);
}

if (sessionTasks) {
  const arr = JSON.parse(sessionTasks);
  store.commit('task/setRegularTasks', arr);
}

watch([project, roleId, subProjectInfo], async () => {
  if (roleId.value) {
    await getTasks({ roleId: roleId.value }); // 根据角色查找定期任务
  }
});

/**
 * 根据时间基准点和角色查找定期任务
 * @param {object} query
 * @param {string} query.roleId 角色id
 * @param {string} query.timeNode 时间基准点 默认当前
 * @param {string} query.timeUnit 时间颗粒度 默认天
 * @param {string} query.queryNum 查找颗粒度数量 默认3个
 * @param {number} query.queryType 0向上查找 1向下查找(默认) 下查包含自己，上查不包含
 */
async function getTasks(query) {
  const params = { param: query };
  try {
    const data = await getRegularTask(params);
    taskObj.tasks = data;

    data.forEach(item => {
      item.plugins.forEach(val => {
        if (Number(val[0].pluginId) === 6) {
          getSonTask(item.detailId);
        }
      });
    });

    store.commit('task/setRegularTasks', data);
  } catch (error) {
    throw new Error(error);
  }
}

// 根据id获取子任务
async function getSonTask(id) {
  const params = { param: { detailId: id } };
  try {
    const data = await findSonTask(params);
    taskObj.tasks.forEach(item => {
      if (item.detailId === id) {
        item.sonList = data;
      }
    });

    store.commit('task/setRegularTasks', taskObj.tasks);
  } catch (error) {
    throw new Error(error);
  }
}

function toDetail(item) {
  store.commit('task/setTaskDetail', item);
  store.commit('task/sonDetailId', '');
}

function toSonDetail(item, id) {
  store.commit('task/setTaskDetail', item);
  store.commit('task/sonDetailId', id);
}
</script>

<style scoped>
.task-list {
  padding: 0 16px 50px;
  overflow-y: auto;
}

.task-box {
}

.task-time {
  height: 32px;
}

.task-time .anticon {
  margin-right: 16px;
}

.task-time span {
  font-size: 14px;
  color: #595959;
}

.task-info {
  margin: 8px 0;
  padding-left: 11px;
}

.task-info > div {
  padding-left: 27px;
  border-left: 1px solid #d2d2d2;
}

.task-info .task-card {
  padding: 16px;
  border-radius: 8px;
  -moz-box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.12);
  -webkit-box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.12);
  box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.12);
}

.task-con {
  margin-top: 16px;
  padding-left: 16px;
}

.task-con > div {
  height: 30px;
}

:deep(.ant-checkbox + span) {
  color: #607d8b;
}

.son-task-name {
  color: #607d8b;
}

.task-list::-webkit-scrollbar {
  width: 0 !important;
}
</style>
