<template>
  <div v-if="!item.hidden">
    <template v-if="hasOneShowingChild(item, item.children) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow">
      <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{ 'submenu-title-noDropdown': !isNest }">
          <svg-icon :icon-class="onlyOneChild.meta.icon || (item.meta && item.meta.icon)" />
          <template #title>
            <span class="menu-title" :title="hasTitle(onlyOneChild.meta.title)">{{ onlyOneChild.meta.title }}</span>
            <span class="bage" v-if="onlyOneChild.meta?.title == '我的待办' && total > 0">{{ total }}</span>
            <span class="bage" v-if="onlyOneChild.meta?.title == '我的抄送' && totalChao > 0">{{ totalChao }}</span>
          </template>
        </el-menu-item>
      </app-link>
    </template>

    <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" teleported>
      <template v-if="item.meta" #title>
        <svg-icon :icon-class="item.meta ? item.meta.icon : ''" />
        <span class="menu-title" :title="hasTitle(item.meta?.title)">{{ item.meta?.title }}</span>
        <span class="bage" v-if="item.meta?.title == '我的任务' && total > 0">{{ total }}</span>
      </template>

      <sidebar-item
        v-for="(child, index) in item.children"
        :key="child.path + index"
        :is-nest="true"
        :item="child"
        :base-path="resolvePath(child.path)"
        class="nest-menu"
      />
    </el-sub-menu>
  </div>
</template>

<script setup lang="ts">
import { isExternal } from '@/utils/validate';
import AppLink from './Link.vue';
import { getNormalPath } from '@/utils/ruoyi';
import { RouteRecordRaw } from 'vue-router';
import { pageByTaskWait, pageByTaskCopy } from '@/api/workflow/task';
import useUserStore from '@/store/modules/user';
import useNoticeStore from '@/store/modules/notice';
const userStore = useUserStore();
const noticeStore = storeToRefs(useNoticeStore());
const props = defineProps({
  item: {
    type: Object as PropType<RouteRecordRaw>,
    required: true
  },
  isNest: {
    type: Boolean,
    default: false
  },
  basePath: {
    type: String,
    default: ''
  }
});
const total = ref(0);
const totalChao = ref(0);
onMounted(() => {
  if (onlyOneChild.value.meta?.title == '我的待办' || props.item.meta?.title == '我的任务') {
    getWaitingList();
  }
  if (onlyOneChild.value.meta?.title == '我的抄送') {
    getChaoList();
  }
});
// 获取我的待办
const getWaitingList = () => {
  pageByTaskWait({ pageNum: 1, pageSize: 10 }).then((resp) => {
    total.value = resp.total;
  });
};
// 获取我的抄送
const getChaoList = () => {
  pageByTaskCopy({ pageNum: 1, pageSize: 10 }).then((resp) => {
    totalChao.value = resp.total;
  });
};
const onlyOneChild = ref<any>({});

const hasOneShowingChild = (parent: RouteRecordRaw, children?: RouteRecordRaw[]) => {
  if (!children) {
    children = [];
  }
  const showingChildren = children.filter((item) => {
    if (item.hidden) {
      return false;
    }
    onlyOneChild.value = item;
    return true;
  });

  // 只有一个子路由时默认显示子路由
  if (showingChildren.length === 1) {
    return true;
  }

  // 没有子路由可显示时显示父路由
  if (showingChildren.length === 0) {
    onlyOneChild.value = { ...parent, path: '', noShowingChildren: true };
    return true;
  }

  return false;
};

const resolvePath = (routePath: string, routeQuery?: string): any => {
  if (isExternal(routePath)) {
    return routePath;
  }
  if (isExternal(props.basePath as string)) {
    return props.basePath;
  }
  if (routeQuery) {
    let query = JSON.parse(routeQuery);
    return { path: getNormalPath(props.basePath + '/' + routePath), query: query };
  }
  return getNormalPath(props.basePath + '/' + routePath);
};

const hasTitle = (title: string | undefined): string => {
  if (!title || title.length <= 5) {
    return '';
  }
  return title;
};
//用深度监听 消息
watch(
  () => noticeStore.state.value.notices,
  (newVal) => {
    if (onlyOneChild.value.meta?.title == '我的待办' || props.item.meta?.title == '我的任务') {
      let time = setTimeout(() => {
        getWaitingList();
        clearTimeout(time);
      }, 500);
    }
  },
  { deep: true }
);
</script>

<style lang="scss" scoped>
.bage {
  position: absolute;
  top: 6px;
  right: 36px;
  padding: 0 6px;
  height: 16px;
  line-height: 16px;
  background: #ff7a7a;
  border-radius: 8px;
  font-size: 12px;
  color: #fff;
  white-space: nowrap;
  box-sizing: border-box;
}

.el-menu-item .el-menu-item__content {
  position: relative;
  padding-right: 24px;
}

.menu-title {
  display: inline-block;
  max-width: calc(100% - 24px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
