// 用户相关仓库
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { login, boschLogin, singleLogin } from '@/api/login'
import { SET_TOKEN, REMOVE_TOKEN } from '@/utils/token'

const useUserStore = defineStore(
  'user',
  () => {
    const asyncRoutes = ref()
    const userType = ref('')
    const userInfo = ref({
      enterpriseStyle: {
        aliasName: ''
      }
    })
    //存储当前用户是否包含某一个按钮
    const buttons = ref<string[]>([])
    const version = ref('2.1.1')
    const classAliasName = ref('')
    const userLogin = async (data: any) => {
      //登录请求
      return new Promise(async (resolve, reject) => {
        try {
          const result: any = await login(data)
          userInfo.value = result.data
          SET_TOKEN(result.data.token)
          btnAuthority(result)
          getThemeColor(result.data.enterpriseStyle)
          userType.value = accounstType(result.data)
          resolve(userType.value)
        } catch {
          reject()
        }
      })

    }
    const boschUserLogin = async (data: any) => {
      //登录请求
      return new Promise(async (resolve, reject) => {
        try {
          
          const result: any = await boschLogin(data)
          userInfo.value = result.data
          SET_TOKEN(result.data.token)
          btnAuthority(result)
          getThemeColor(result.data.enterpriseStyle)
          userType.value = accounstType(result.data)
          resolve(userType.value)

        } catch {
          reject()
        }
      })

    }
    const singleLoginStore = async (data: any) => {
      //公用单点登录请求
      return new Promise(async (resolve, reject) => {
        try {
          const result: any = await singleLogin(data)
          userInfo.value = result.data
          SET_TOKEN(result.data.token)
          btnAuthority(result)
          getThemeColor(result.data.enterpriseStyle)
          userType.value = accounstType(result.data)
          resolve(userType.value)
        } catch {
          reject()
        }
      })

    }
    const getThemeColor = async (res) => {
     
          if (res) {
            userInfo.value.enterpriseStyle = res
            if(userInfo.value.enterpriseStyle.aliasName == 'bosch'){
              classAliasName.value = '-bosch'
            } else {
              classAliasName.value = ''
            }
          } else {
            classAliasName.value = ''
            userInfo.value.enterpriseStyle = {
              aliasName: '',
              errorTipColor: '#f56c6c',
              extraFileUrl: '',
              fontColor: '#6C6C6E',
              fontFileUrl: '',
              infoTipColor: '#909399',
              logoUrl: '',
              middleFontColor: '#909399',
              smallFontColor: '#A8ABB2',
              successTipColor: '#67C23A',
              themeColor: '#5CC4A7',
              warnTipColor: '#E6A23C',
              borderColor: '#ebeef5'
            }
          }
        
    }

    const userLogout = async () => {
      //退出登录请求
      REMOVE_TOKEN()
    }

   
    // 按钮权限
    const btnAuthority = (result) => {
      buttons.value = []
      if (result.data.purviewResponse.rbacResourceActions != null && result.data.purviewResponse.rbacResourceActions != 'null') {
        result.data.purviewResponse.rbacResourceActions.forEach(el => {
          buttons.value.push(el.identifying)
        });
      }
    }
    // 确定账号类型
    const accounstType = (res) => {
      // let userType = ''
      // 是否包含系统管理员
      const type = res.purviewResponse.roles.some((el) => {
        return el.id == import.meta.env.VITE_APP_ROLES_MAIN 
      })
      if (!type) {
        // 是否包含企业管理员
        const type2 = res.purviewResponse.roles.some((el) => {
          return el.id == import.meta.env.VITE_APP_ROLES_QY 
        })
        if (!type2) {
          // 是否包含普通用户
          const type3 = res.purviewResponse.roles.some((el) => {
            return el.id == import.meta.env.VITE_APP_ROLES_PT 
          })
          if (type3) {
            return 'user'
          }
        } else {
          return 'company'
        }
      } else {
        return 'system'
      }
    }

    return {
      userType,
      userInfo,
      asyncRoutes,
      buttons,
      version,
      classAliasName,
      userLogin,
      userLogout,
      getThemeColor,
      boschUserLogin,
      singleLoginStore
    }
  },
  {
    persist: true
  }
)

export default useUserStore
