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:"管理员"
      },
      PUSHPARAMETER:{},
      pageCountInfo:{
        currentPageNum: 1,
        currentPageSize: 10,
      },
    }),
    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,this.userInfo.status,localStorage.getItem('user'),'****************************************************************************************');
          if (this.userInfo.status===undefined) {
          
            this.userInfo = JSON.parse(localStorage.getItem('user'))
          }
          return this.userInfo.status === 0 ? true : false;
        } catch (error) {
          ElMessage.error('this.userInfo.state is not valid');
        }
        
      },
      setPushParameter(data){
        console.log('setPushParameter',data)
        if(data){ 
          this.PUSHPARAMETER = data
        }else{
          ElMessage.error('setPushParameter data is not valid');
        }
 
      },
      setPageCountInfoCurrentPageNumData (data) { 
        if(data){ 
          this.pageCountInfo.currentPageNum = data
        }else{
          ElMessage.error('currentPageNum data is not valid');
        }
      },
      setPageCountInfoCurrentPageSizeData (data) { 
        if(data){ 
          this.pageCountInfo.CurrentPageSize = data
        }else{
          ElMessage.error('CurrentPageSize data is not valid');
        }
      }
    }
  })

export default useAppStore
