<script setup lang="ts">
import { computed, watch } from 'vue'
import { useAppStore } from './store/modules/app'
import { ConfigGlobal } from './components/ConfigGlobal'
import { useDesign } from './hooks/web/useDesign'
import { getCodeList, getDeptTree, getDistrictTree } from './api'
import { useUserStore } from './store/modules/user'

const { getPrefixCls } = useDesign()

const prefixCls = getPrefixCls('app')

const appStore = useAppStore()

const currentSize = computed(() => appStore.getCurrentSize)

const greyMode = computed(() => appStore.getGreyMode)

const userStore = useUserStore()

appStore.initTheme()

// 全局存一下字典数据
const getGlobalCodeList = async () => {
  const {
    success,
    data: { list }
  } = await getCodeList({
    current: 1,
    size: 10000000
  })
  if (success) {
    const formatter = list.map((i) => {
      return {
        codeName: i.codeName,
        codeType: i.codeType,
        codeValue: i.codeValue
      }
    })
    sessionStorage.setItem('CODE_ENUM', JSON.stringify(formatter))
  }
}
// 存一下公共部门树
const getDeptTreeFunc = async () => {
  const { data, success } = await getDeptTree()
  if (success) {
    sessionStorage.setItem('DEPT_TREE', JSON.stringify(data))
  }
}
// 存一下行政区划
const getDistrictFunc = async () => {
  const { data, success } = await getDistrictTree()
  if (success) {
    sessionStorage.setItem('DISTRICT_TREE', JSON.stringify(data))
  }
}
// watch(
//   () => userStore.getToken,
//   (val) => {
//     if (val) {
//       getGlobalCodeList()
//       getDeptTreeFunc()
//       getDistrictFunc()
//     }
//   },
//   {
//     immediate: true
//   }
// )
</script>

<template>
  <ConfigGlobal :size="currentSize">
    <RouterView :class="greyMode ? `${prefixCls}-grey-mode` : ''" />
  </ConfigGlobal>
</template>

<style lang="less">
@prefix-cls: ~'@{adminNamespace}-app';

.size {
  width: 100%;
  height: 100%;
}

html,
body {
  padding: 0 !important;
  margin: 0 !important;
  overflow: hidden;
  .size;

  #app {
    .size;
  }
}

.@{prefix-cls}-grey-mode {
  filter: grayscale(100%);
}
</style>
