<template>
  <div class="navbar flex items-center justify-between">
    <div class="project-name">{{ projectObj.obj.pname }}</div>
    <div class="project-action">
      <img />
      <img @click="toShowMask" />
    </div>
  </div>

  <div class="role-list flex items-center">
    <div class="role-box relative" v-for="(item, index) in roles.arr" :key="index">
      <div class="role-name" :class="{ mine: item.mine === 1 && currRoleId === item.id }">{{ item.name }}</div>
      <div class="line-box absolute flex justify-center" v-if="item.mine === 1 && currRoleId === item.id"><div class="line"></div></div>
    </div>
  </div>

  <div class="global">
    <div class="global-box" v-if="permanentObj.permanentList && permanentObj.permanentList.length > 0">
      <div class="global-task cursor-pointer" v-for="(item, index) in permanentObj.permanentList" :key="index" @click="toDetail(item)">
        {{ item.name }}
      </div>
    </div>
  </div>
  <div class="task-list" :style="{ height: 'calc(100vh - 160px - (' + globalHeight + 'px))' }">
    <div class="task-box" v-for="(item, index) in taskObj.tasks" :key="index">
      <div class="task-time flex items-center justify-between">
        <div class="flex items-center">
          <img />
          <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)">{{ item.name }}</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 @change="handleChange">{{ val.name }}</a-checkbox>
              </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, ref } from 'vue';
import { useStore } from 'vuex';
import dayjs from 'dayjs';
import { findShowRole, getRegularTask, findSonTask } from 'apis';

const store = useStore();
const projectObj = reactive({ obj: { u: '', p: '', pname: '', url: '' } });
const project = computed(() => store.state.projects.project); // 项目信息
const currRoleId = computed(() => store.state.role.roleId); // 当前角色
const roles = reactive({
  // 角色信息
  arr: [
    { id: 1, name: '项目经理', mine: 1, pm: 1, sequence: 1 },
    { id: 2, name: '运维', mine: 0, pm: 0, sequence: 2 },
  ],
});
const permanents = computed(() => store.state.role.permanents); // 日常任务
const permanentObj = reactive({ permanentList: [] });
const tasks = computed(() => store.state.role.tasks); // 定期任务
const taskObj = reactive({ tasks: [] });
const globalHeight = ref(0); // 日常任务面板高度

// 设置 初始显示角色信息
const setInitialRoleId = visibleList => {
  if (!visibleList || !visibleList.length) return;

  roles.arr = [];
  visibleList.forEach(item => {
    roles.arr.push(item);
  });

  const index = visibleList.findIndex(item => +item.mine === 1);
  const currentRole = index > 0 ? visibleList[index] : visibleList[0];
  // const storageRoleId = this.$t.storage.getStorageSync('roleId');
  // const currentRoleId = storageRoleId ? storageRoleId : currentRole ? currentRole.id : '';
  const currentRoleId = currentRole ? currentRole.id : '';
  currRoleId.value = currentRoleId;
  store.commit('role/setRoleId', currentRoleId);
  // 清空storage
  // this.$t.storage.setStorageSync('roleId', '');
};

/**
 * 通过项目id获取角色信息
 * @param {string} projectId
 * @param {object} params 提交的参数
 */
const getRoles = async projectId => {
  try {
    const data = await findShowRole(projectId);
    store.commit('role/setInvisibleRoles', data ? data.invisibleList : []);
    store.commit('role/setVisibleRoles', data ? data.visibleList : []);
    setInitialRoleId(data ? data.visibleList : []);
  } catch (error) {
    throw new Error(error);
  }
};

// 日常任务
const getPermanentData = async roleId => {
  const params = { param: { roleId } };
  try {
    const data = await store.dispatch('task/getPermanent', params);
    permanentObj.permanentList = data;
    globalHeight.value = data.length * 38 + 26;
  } catch (error) {
    throw new Error(error);
  }
};

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

/**
 * 根据时间基准点和角色查找定期任务
 * @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向下查找(默认) 下查包含自己，上查不包含
 */
const getTasks = async query => {
  const params = { param: query };
  try {
    const data = await getRegularTask(params);
    taskObj.tasks = data;

    data.forEach(item => {
      getSonTask(item.detailId);
    });

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

// 监听项目信息
watch(project, async newVal => {
  projectObj.obj = newVal;
  store.commit('projects/setProject', newVal);

  permanentObj.permanentList = permanents.value;
  taskObj.tasks = tasks.value;

  await getRoles(projectObj.obj.p); // 通过项目id获取角色信息
  await getPermanentData(currRoleId.value); // 根据角色查找永久的日常任务
  await getTasks({ roleId: currRoleId.value });
});

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

<style scoped>
.navbar {
  padding: 0 16px;
  height: 44px;
}

.project-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.role-list {
  padding: 0 16px;
  height: 36px;
  border-bottom: 1px solid #cccccc;
}

.role-box {
  margin-right: 16px;
  height: 36px;
}

.role-name {
  font-size: 14px;
  line-height: 36px;
  color: #333333;
}

.role-name.mine {
  font-weight: 600;
  color: #1890ff;
}

.role-box .line-box {
  width: 100%;
  bottom: 0;
}

.line-box .line {
  width: 16px;
  height: 2px;
  background-color: #1890ff;
}

.global {
  padding: 16px;
}

.global-box {
  border: 1px solid #cccccc;
  border-radius: 10px;
  padding: 12px 16px;
}

.global-task {
  padding: 8px 0;
  line-height: 22px;
}

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

.task-box {
}

.task-time {
  height: 32px;
}

.task-time img {
  width: 23px;
  height: 23px;
  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;
}

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

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

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