import { createApp ,unref} from 'vue';
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import locale from 'element-ui/lib/locale/lang/zh-CN'
import '@/request/setupAxios'
import '@/assets/style/public.scss'
import user from '@/state/user'
const app = createApp(App);
app.use(router)
app.use(ElementPlus, { locale })
app.mount('#app')


// 挂在全局方法
app.config.globalProperties.$power = (fun,val) =>{
    let replacelist = {
        'organizationlist':'organization',
    }
    let routeName = router.currentRoute._value.name
    let roleKeyList = user._value.roleKeyList
    let rokeName =  `${replacelist[routeName] || routeName}:${val}`
    console.log('$power',rokeName,roleKeyList)
    // if(roleKeyList.includes(rokeName)){
    //     fun()
    // }else{
    //     console.warn('暂无权限',routeName)
    //     return false
    // }
}
app.directive('power',{
    mounted(el,binding) {
        let replacelist = {
            'organizationlist':'organization',
        }
        let routeName = router.currentRoute._value.name
        let {roleKeyList,roleData,info} = unref(user);
        let item = null,type,{value} = binding,qxdj=false,rokeName=''
        if(typeof(value)=='string'){
            type = value
            rokeName =  `${replacelist[routeName] || routeName}:${type}`
        }else{
            rokeName =  `${replacelist[routeName] || routeName}:${value.roleType}`
            type = value.roleType
            item = value
            switch(roleData[rokeName]){
                case 4://所属组织内部数据
                    if(item.deptId != info.deptId){
                        qxdj = true;
                    }
                    break;
                case 5:
                    if(item.creatorId != info.id){
                        qxdj = true;
                    }
                    break;
            }
        }
        var klses = el.className;
        if((!roleKeyList.includes(rokeName))){//判断无权
            el.className = klses + ' disable' 
        }else if(qxdj){//判断有权查询是否权限足够
            el.className = klses + ' disable' 
        }
    }
})