<script lang="ts" setup>
import { useAppStore } from '@/stores';
import { IconExport, IconFile, IconCaretDown } from '@arco-design/web-vue/es/icon';
import { fetchMenusTree } from '@/api/all';
const lists = ref([]);
const router = useRouter();
const clickExit = () => {
  router.replace('/login');
};
const getMenus = async () => {
  const res = await fetchMenusTree();
  lists.value = res;
};
onMounted(() => {
  getMenus();
});
const appStore = useAppStore();


function setServerMenu() {
  appStore.fetchServerMenuConfig();
  console.log(appStore.serverMenu);
}
const handleSelect = (index: any) => {
  console.log(index);
  if (index == 0) {
    router.push('/workplace');
  } else {
    router.push('/dataEngine/dataEngine/hotTranslation');
  }
};

const handleDopdownClick = (index: any, ind: any) => {
  let children = lists.value[index].children;
  let indPath = children[ind];
  if (indPath.name == '行业热门话题洞察') {
    router.push('/dataEngine/dataEngine/hotTranslation');
  } else if (indPath.name == '行业词云') {
    router.push('/dataEngine/dataEngine/hotCloud');
  } else if (indPath.name == '行业关键词动向') {
    router.push('/dataEngine/dataEngine/keyWord');
  } else if (indPath.name == '用户痛点观察') {
    router.push('/dataEngine/dataEngine/userPainPoints');
  } else if (indPath.name == '重点品牌动向') {
    router.push('/dataEngine/dataEngine/keyBrandMovement');
  } else if (indPath.name == '用户画像') {
    router.push('/dataEngine/dataEngine/userPersona');
  }
};
</script>

<template>
  <div class="navbar">
    <div class="left-side">
      <a-space>
        <img src="@/assets/logo.svg" alt="" />
      </a-space>
    </div>
    <div class="center-side">
      <div class="menu-demo">
        <a-menu mode="horizontal" :default-selected-keys="['1']">
          <a-menu-item :key="'1'" @click="handleSelect(0)">
            <view>工作台</view>
          </a-menu-item>
          <a-menu-item v-for="(item, index) in lists" :key="index + 2">
            <a-dropdown :popup-max-height="false">
              <a-button>{{ item.name }}<icon-caret-down /></a-button>
              <template #content>
                <a-doption v-for="(child, ind) in item.children" :key="ind" @click="handleDopdownClick(index, ind)">{{
                  child.name
                }}</a-doption>
              </template>
            </a-dropdown>
          </a-menu-item>
        </a-menu>
      </div>
    </div>
    <ul class="right-side">
      <li>
        <a-dropdown trigger="click">
          <a-avatar class="cursor-pointer" :size="32">
            <img alt="avatar" src="@/assets/avatar.svg" />
          </a-avatar>
          <template #content>
            <div style="padding: 14px">
              <a-doption>
                <a-space @click="setServerMenu">
                  <img src="@/assets/option.svg" style="width: 16px; height: 16px" />
                  <span style="width: 140px; font-size: 12px">管理中心</span>
                  <icon-right />
                </a-space>
              </a-doption>
              <a-doption>
                <a-space>
                  <img src="@/assets/change.svg" style="width: 16px; height: 16px" />
                  <span style="width: 140px; font-size: 12px">切换企业账号</span>
                  <icon-right />
                </a-space>
              </a-doption>
              <a-doption>
                <a-space @click="clickExit">
                  <img src="@/assets/exit.svg" style="width: 16px; height: 16px" />
                  <span style="width: 140px; font-size: 12px">退出登录</span>
                  <icon-right />
                </a-space>
              </a-doption>
            </div>
          </template>
        </a-dropdown>
      </li>
    </ul>
  </div>
</template>

<style scoped lang="less">
.navbar {
  display: flex;
  justify-content: space-between;
  height: 100%;
  background-color: var(--color-bg-2);
  border-bottom: 1px solid var(--color-border);
}
.left-side {
  display: flex;
  align-items: center;
  padding-left: 20px;
}
.center-side {
  flex: 1;
  display: flex;
  align-items: center;

  margin-left: 40px;
}
.cneter-tip {
  font-size: 16px;
  font-weight: 400;
  color: var(--color-text-1);
}
.menu-demo {
  flex: 1;
}
.right-side {
  display: flex;
  padding-right: 20px;
  list-style: none;
  li {
    display: flex;
    align-items: center;
    padding: 0 10px;
  }
  a {
    color: var(--color-text-1);
    text-decoration: none;
  }
  .nav-btn {
    border-color: rgb(var(--gray-2));
    color: rgb(var(--gray-8));
    font-size: 16px;
  }
}
</style>
