<template>
  <ConfigProvider :locale="antdLocal">
    <AppProvider>
      <RouterView />
    </AppProvider>
  </ConfigProvider>
</template>

<script lang="ts" setup>
  import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN';
  import en from 'ant-design-vue/lib/locale-provider/en_US';
  import zhTW from 'ant-design-vue/lib/locale-provider/zh_TW';
  import { ConfigProvider } from 'ant-design-vue';
  import { AppProvider } from '/@/components/Application';
  import { useTitle } from '/@/hooks/web/useTitle';
  import { useLocale } from '/@/locales/useLocale';
  import { useAppStore } from '/@/store/modules/app';
  import 'dayjs/locale/zh-cn';
  import { computed } from 'vue';
  import { getLogoInfo } from './api/system/login';
  import { onBeforeUnmount } from 'vue';
  import { useExclusiveApprovalProcess } from './hooks/web/useExclusiveApprovalProcess';
  import { getFileUrl } from '/@/utils/event/design';

  // support Multi-language
  const { handleCLoseExclusiveApprovalProcess } = useExclusiveApprovalProcess();
  const { getLocale } = useLocale();
  const appStore = useAppStore();
  getLogoInfo().then((res) => {
    appStore.setLogoConfig({
      companyName: res.companyName,
      shortName: res.shortName,
      systemName: res.systemName,
      refreshLogoUrl: getFileUrl(res, 'refreshLogoUrl'),
      backgroundLogoUrl: getFileUrl(res, 'backgroundLogoUrl'),
      designerLogoUrl: getFileUrl(res, 'designerLogoUrl'),
      loginLogoUrl: getFileUrl(res, 'loginLogoUrl'),
      menuLogoUrl: getFileUrl(res, 'menuLogoUrl'),
      smallMenuLogoUrl: getFileUrl(res, 'smallMenuLogoUrl'),
    });
  });
  const antdLocal = computed(() => {
    if (getLocale.value === 'zh_CN') {
      return zhCN;
    }
    if (getLocale.value === 'en') {
      return en;
    }
    if (getLocale.value === 'zh_TW') {
      return zhTW;
    }
    return zhCN;
  });

  const beforeUnloadHandler = (e) => {
    if (e) {
      e.returnValue = '关闭提示';
    }
    return '关闭提示';
  };

  window.addEventListener('beforeunload', beforeUnloadHandler);

  onBeforeUnmount(() => {
    window.removeEventListener('beforeunload', beforeUnloadHandler);
  });
  //在onunload函数中执行真正离开前的代码
  window.addEventListener('unload', async () => {
    try {
      // 释放流程审批独占模式
      await handleCLoseExclusiveApprovalProcess();
    } catch (error) {}
  });
  // Listening to page changes and dynamically changing site titles
  useTitle();
</script>
<style lang="less">
  .ant-tree.ant-tree-directory .ant-tree-treenode {
    width: 100%;
    height: 40px;
    line-height: 40px;
  }

  .ant-tree.ant-tree-directory .ant-tree-node-content-wrapper {
    height: 40px;
    line-height: 40px;
  }

  .ant-input-affix-wrapper::before {
    width: 0;
    visibility: hidden;
    content: '' !important;
  }

  .ant-table-title .ant-select-selection-overflow {
    flex-wrap: nowrap !important;
    overflow: scroll;
  }
</style>
