import Cookies from 'js-cookie'
import { defineStore } from 'pinia'
import setting from '@/settings'
import i18n from '@/i18n'
import { organizationtreeList, _view, _export, _add, _adds, _up, _ups, _del, _list_get } from '@/api/app'
import useUserStore from '@/store/modules/user'

// 遍历多维数组
function duoweiArr(data) {
  // 遍历后台传来的路由字符串，转换为组件对象
  let accessData = data.filter(router => {

    //label
    router.label = router.organizationname;
    router.value = router.organizationid;

    //存在子集
    if (router.next && router.next.length) {
      router.children = duoweiArr(router.next);
    }

    return true;
  });
  return accessData;
}

const useAppStore = defineStore('app', {
  state: () => ({
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!+(Cookies.get('sidebarStatus') ?? '') : true,
      withoutAnimation: false,
      hide: false,
    },
    device: 'desktop',
    size: Cookies.get('size') || 'default',
    language: Cookies.get('language') || setting.defaultLanguage,
    organizationtreeData: [],
    dlsData: [],
  }),
  actions: {
    toggleSideBar(withoutAnimation?: boolean) {
      if (this.sidebar.hide) {
        return false
      }
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = Boolean(withoutAnimation)
      if (this.sidebar.opened) {
        Cookies.set('sidebarStatus', '1')
      } else {
        Cookies.set('sidebarStatus', '0')
      }
    },
    async setDlsData() {
      const params = {
        pagevalue: 307,
        pageindex: 1,
        rows: 99,
      }
      console.log(params, 'params')
      const { Total, Data } = await _list_get(params)
      this.dlsData = Data.Data
    },
    closeSideBar({ withoutAnimation }: { withoutAnimation: boolean }) {
      Cookies.set('sidebarStatus', '0')
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },
    toggleDevice(device: string) {
      this.device = device
    },
    setSize(size: string) {
      this.size = size
      Cookies.set('size', size)
    },
    toggleSideBarHide(status: boolean) {
      this.sidebar.hide = status
    },
    setLanguage(language: string) {
      this.language = language
      Cookies.set('language', language)
      i18n.global.locale.value = language  // 动态切换语言
    },
    getOrganizationView() {
      // console.log(useUserStore().token, 'useUserStore')
      if (useUserStore().token) {
        console.log('初始化系统数据')
      }
    }
  },
})

export default useAppStore
