import Cookies from 'js-cookie'
import { ElMessage } from 'element-plus';
import { tr } from 'element-plus/es/locales.mjs';
const useAppStore = defineStore(
  'app',
  {
    state: () => ({
      sidebar: {
        opened: true,
        withoutAnimation: false,
        hide: false
      },
      device: 'desktop',
      size: Cookies.get('size') || 'default',
      num:1,
      isCurUserLogin: false,
      userInfo: {},
      showavatarUrl :'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      position:{
        10:'超级管理员',
        1:'巡检人员',
        0:"管理员"
      }
    }),
    actions: {
      toggleSideBar(withoutAnimation) {
        if (this.sidebar.hide) {
          return false;
        }
        this.sidebar.opened = true
        this.sidebar.withoutAnimation = withoutAnimation
        if (this.sidebar.opened) {
          Cookies.set('sidebarStatus', 1)
        } else {
          Cookies.set('sidebarStatus', 0)
        }
      },
      closeSideBar({ withoutAnimation }) {
        Cookies.set('sidebarStatus', 0)
        this.sidebar.opened = true
        this.sidebar.withoutAnimation = withoutAnimation
      },
      toggleDevice(device) {
        this.device = device
      },
      setSize(size) {
        this.size = size;
        Cookies.set('size', size)
      },
      toggleSideBarHide(status) {
        this.sidebar.hide = status
      },
      
      initavatarUrlFn () {
        
        try {
          if (!(this.userInfo)) {
          this.userInfo = JSON.parse(localStorage.getItem('user'))
          console.log('initavatarUrlFn-----我是空------------',this.userInfo)
          }else{
            (this.userInfo).avatarUrl =  (this.userInfo).avatarUrl ? (this.userInfo).avatarUrl : 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png';
            console.log('initavatarUrlFn-----我不是空------------',this.userInfo)
          }
        } catch (error) {
          ElMessage.error('avatarUrl is not valid',error);
        
        }
        
        
      },
      chackCurPersionOpition() {
        try {
          // console.log(this.userInfo,'********************************************');
          return this.userInfo.status === 0 ? true : false;
        } catch (error) {
          ElMessage.error('this.userInfo.state is not valid');
        }
        
      }
    }
  })

export default useAppStore
