<template>
  <a-divider />
  <div class="list-flex">
    <div class="item-box" v-for="(item, index) in projects.projects" :key="index">
      <div class="one-level h-70 cursor-pointer flex items-center" @click="toDetail(item)">
        <div class="icon" @click.stop="showActionCard(item)"><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" @click="changeShow(item)" />
          <DownOutlined v-else @click="changeShow(item)" />
        </div>
      </div>

      <div class="two-box" v-if="item.show">
        <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" @click="changeShow(sonItem)" />
              <DownOutlined v-else @click="changeShow(sonItem)" />
            </div>
          </div>

          <div class="three-box" v-if="sonItem.show">
            <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>

    <a-modal v-model:visible="visible" title="删除课题" @ok="handleOk">
      <p>是否删除课题？</p>
    </a-modal>
  </div>
</template>

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

const store = useStore();
const projects = reactive({ projects: [] });
const visible = ref(false);
const deleteId = ref(null);
const projectId = sessionStorage.getItem('projectId');
const newProject = computed(() => store.state.projects.newProject); // 新建项目
const startTime = computed(() => store.state.layout.startTime); // 当前选择时间
const endTime = computed(() => store.state.layout.endTime); // 当前选择时间
let start = startTime.value ? startTime.value : dayjs().startOf('day').format('x');
let end = endTime.value ? endTime.value : dayjs().startOf('day').format('x');

watch([newProject, startTime, endTime], () => {
  start = startTime.value ? startTime.value : dayjs().startOf('day').format('x');
  end = endTime.value ? endTime.value : dayjs().startOf('day').format('x');
  getProjectsList(start, end);
});

getProjectsList(start, end);

// 点击操作面试显示隐藏
const showActionCard = item => {
  visible.value = true;
  deleteId.value = item.id;
};

// 获取项目列表
async function getProjectsList(startData, endData) {
  try {
    const data = await getProjects(startData, endData);
    projects.projects = [];
    data.forEach(item => {
      item.show = false;

      if (item.id === projectId) {
        item.show = true;
      }

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

          if (sonItem.id === projectId) {
            sonItem.show = true;
            item.show = true;
          }
        });
      }

      projects.projects.push(item);
    });

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

function toDetail(item) {
  item.show = true;

  const obj = {
    id: item.id,
    name: item.name,
    url: item.url,
  };

  store.commit('projects/setProject', obj);
  store.commit('task/setTaskDetail', null);
}

function changeShow(item) {
  item.show = !item.show;
}

async function handleOk() {
  visible.value = false;
  await deleteProject(deleteId.value);
  getProjectsList(dayjs().startOf('day').format('x'), dayjs().endOf('day').format('x'));
}

// 删除项目
async function deleteProject(param) {
  try {
    await delProject(param);
  } catch (error) {
    throw new Error(error);
  }
}
</script>

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

.list-flex::-webkit-scrollbar {
  width: 0 !important;
}

.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;
}
</style>
