import {createGlobalState,useStorage} from '@vueuse/core'
import {uniStorage} from "@/utils/uniStorage.js"
import { userInfoApi } from "@/api/user";

import {ref} from 'vue'
export const useAuth = createGlobalState(() => {
  const token = useStorage('token', '', uniStorage)
  // const token = ref("2046c3941ed4959f6d988d3d4a0fe40d4b52f33f3f5fc1001406064554641d9406bb13cacb92939b0ca223c17e2c2f2fe70212ef017dbae8965d5cf86bad48ce4316e605ca187bd9ffd4aa6b56865be4ad4e422701d330b52d60cfe649cd48cf3a21a2a6e9a9cabafff364ee9c311ec634b0afc09db0d3215bedce561e9d50e5a8da6092062e2ebe35f747d77d72a68ad492a4ab218c07887c9cd4867f2c2d28e4ae1fd671144cc20ef0632f9ce067289004d67f6adf41b20d6ef5cdbfb74aadc2d2736ececf07254f1a76552bde4f1161a0fca7bfe32a29685ce1e76366116b81ae2195b3713dbb04285e5ddfd36184fe671c5524d20b4fe74a555db755f8d939b0bc46fb0cb998323d54c9925729d7ca835b7925999a677faa0cbe1cbc67b5203d85317653883aec81d3e71d865b326376bea726cc66d9f7f5a160d43f671c")
  const refreshToken = useStorage('refreshToken', '', uniStorage)
  const userInfo = useStorage('userInfo', {}, uniStorage)
  const leaderList = useStorage('leaderList', [], uniStorage)
  const myCompany = useStorage('myCompany','', uniStorage)
  const isLeader=ref(false)
  // const leaderList=ref([])
  const getUserInfo=async ()=>{
    const data={
      ID:userInfo.value.ID
    }
  const res=  await userInfoApi(data)
    if (res.status===0){
      userInfo.value=res.data
    }
  }
  // const getUserLeader=async ()=>{
  //   const data={
  //     departmentId:userInfo.value.PositionUsers?.map(x=>x.DepartmentId)
  //   }

  //   const res=  await userLeaderApi(data)
  //   if (res.status===0){
  //     isLeader.value=!!res.data.departmentLeaders?.find((x) => {
  //       return x.userID === userInfo.value.ID
  //     })
  //     leaderList.value=res.data.departmentLeaders
  //     myCompany.value=res.data.company
  //   }

  // }
  return {
    leaderList,
    myCompany,
    // getUserLeader,
    getUserInfo,
    userInfo,
    token,
    refreshToken,
  }
})
