<template>
  <a-divider />
  <div class="list-flex">
    <div class="item-box" v-for="(item, index) in projects" :key="index">
      <div class="one-level h-70 cursor-pointer flex items-center" @click="toDetail(item)">
        <div class="icon"><img src="https://www.tall.wiki/staticrec/drag.svg" /></div>
        <div class="detail">
          <div class="name-box flex items-center">
            <div class="name truncate">{{ item.name }}</div>
            <div class="precent-num">50%</div>
          </div>

          <div class="time">{{ dayjs(item.startTime).format('HH:mm:ss') }} 至 {{ dayjs(item.endTime).format('HH:mm:ss') }}</div>
        </div>

        <div class="right" @click.stop="openMenu">
          <RightOutlined v-if="!item.show" />
          <DownOutlined v-else />
        </div>
      </div>

      <div class="two-box">
        <div class="two-flex" v-for="(sonItem, sonIndex) in item.sonProjectList" :key="sonIndex">
          <div class="two-level h-70 cursor-pointer flex items-center" @click="toDetail(sonItem)">
            <div class="icon"><img src="https://www.tall.wiki/staticrec/drag.svg" /></div>
            <div class="detail">
              <div class="name-box flex items-center">
                <div class="name truncate">{{ sonItem.name }}</div>
                <div class="precent-num">50%</div>
              </div>

              <div class="time">{{ dayjs(sonItem.startTime).format('HH:mm:ss') }} 至 {{ dayjs(sonItem.endTime).format('HH:mm:ss') }}</div>
            </div>

            <div class="right" @click.stop="openMenu">
              <RightOutlined v-if="!sonItem.show" />
              <DownOutlined v-else />
            </div>
          </div>

          <div class="three-box">
            <div
              class="three-level h-70 cursor-pointer flex items-center"
              v-for="(thirdItem, thirdIndex) in sonItem.sonProjectList"
              :key="thirdIndex"
              @click="toDetail(thirdItem)"
            >
              <div class="icon"><img src="https://www.tall.wiki/staticrec/drag.svg" /></div>
              <div class="detail">
                <div class="name-box flex items-center">
                  <div class="name truncate">{{ thirdItem.name }}</div>
                  <div class="precent-num">50%</div>
                </div>

                <div class="time">
                  {{ dayjs(thirdItem.startTime).format('HH:mm:ss') }} 至 {{ dayjs(thirdItem.endTime).format('HH:mm:ss') }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { useStore } from 'vuex';
import dayjs from 'dayjs';
import { getProjects } from 'apis';
import { RightOutlined, DownOutlined } from '@ant-design/icons-vue';

const store = useStore();
const projects = reactive([]);

// 获取项目列表
const getProjectsList = async (startTime, endTime) => {
  try {
    const data = await getProjects(startTime, endTime);

    data.forEach(item => {
      item.show = false;

      if (item.sonProjectList) {
        item.sonProjectList.forEach(sonItem => {
          sonItem.show = false;
        });
      }

      projects.push(item);
    });

    store.commit('projects/setProjects', data);
  } catch (error) {
    console.log(error);
  }
};

getProjectsList(dayjs().startOf('day').format('x'), dayjs().endOf('day').format('x'));

function toDetail(project) {
  const obj = {
    u: project.userId,
    p: project.id,
    pname: project.name,
    url: project.url,
  };

  store.commit('projects/setProject', obj);
}
</script>
<script>
export default { name: 'Projects' };
</script>

<style scoped>
.list-flex {
  height: calc(100vh - 48px - 272px - 56px - 16px - 2px);
  overflow-y: auto;
}

.ant-divider-horizontal {
  height: 16px;
  background: #eeeeee;
  margin: 0;
}
.project-list {
  padding: 16px 0;
}

.h-70 {
  height: 70px;
}

.one-level {
  padding: 0 16px;
}

.two-level {
  padding: 0 16px 0 32px;
}

.three-level {
  padding: 0 16px 0 48px;
}

.item-box .icon {
  margin-right: 8px;
  width: 24px;
  height: 24px;
}

.detail {
  width: calc(100% - 76px);
}

.name-box {
  margin-bottom: 8px;
}

.name {
  margin-right: 8px;
  font-size: 14px;
  line-height: 1;
  font-weight: 600;
  max-width: calc(100% - 56px);
  color: #333333;
}

.precent-num {
  width: 48px;
  height: 18px;
  line-height: 18px;
  text-align: center;
  border-radius: 18px;
  background-color: rgba(24, 144, 255, 0.2);
  color: #1890ff;
  font-size: 12px;
  font-weight: 600;
}

.time {
  font-size: 12px;
  color: #999999;
}

.right {
  width: 14px;
  margin-left: 30px;
}

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