import {getStaticImg,useUserInfo} from '@/hooks/common'
import {ref} from 'vue'
import apiChart from '@/api/chart'
import {chartLibBtn,useAuthBtn} from '@/hooks/useAuthBtn'

const {checkAuthBtn} = useAuthBtn()
export function useCatalogList(){
    //操作栏图标的地址
    const iconSrc = {
        'addNext':getStaticImg('ppt/icon_action_copy.png'),
        'reName':getStaticImg('ppt/icon_action_copy.png'),
        'delete':getStaticImg('ppt/icon_action_copy.png'),
        'moveTo':getStaticImg('ppt/icon_action_copy.png'),
        'addChart':getStaticImg('ppt/icon_action_copy.png')
    }
    //目录,图表的操作栏
    const optArrNode=[
        {
            label:'添加子分类',
            icon:iconSrc.addNext,//图标地址
            id:'addNext',
            authRole:'admin',
        },
        {
            label:'重命名',
            icon:iconSrc.reName,
            id:'reName',
            authRole:'admin',
        },
        {
            label:'删除',
            icon:iconSrc.delete,
            id:'delete',
            authRole:'admin',
        },
        /* {id:'addNew',label:'添加图表分类'} */
    ]
    const optArrItem=[
        {
            label:'移动至',
            icon:iconSrc.moveTo,
            id:'moveTo'
        },
        {
            label:'重命名',
            icon:iconSrc.reName,
            id:'reName',
            authRole:'admin',
        },
        {
            label:'删除',
            icon:iconSrc.delete,
            id:'delete',
            authRole:'admin',
        }
    ]
    const optArrChart=[
        {
            label:'加入我的图库',
            icon:iconSrc.addChart,
            id:'addChart'
        },
        {
            label:'移动至',
            icon:iconSrc.moveTo,
            id:'moveChart',
        },
    ]
    //目录列表
    const catalogNodes = ref([])
    const currentLang = ref('')
    //获取目录列表
    async function getCatalogList(){
        const res = await apiChart.ETAChartClassifyList()
        if(res.Ret!==200) return 
        currentLang.value = res.Data.Language
        localStorage.setItem('chartETALange',currentLang.value)
        catalogNodes.value = res.Data?res.Data.AllNodes:[]||[]
        catalogNodes.value = catalogNodes.value.map(node=>{
            if(node.Children){
                node.Children = node.Children.map(child=>{
                    child.parentName = node.ChartClassifyName //添加子分类时需要显示父级分类名称
                    if(child.Children){//改成三级目录了
                        child.Children = child.Children.map(_child=>{
                            _child.parentName = child.ChartClassifyName
                            return _child
                        })
                    }
                    return child
                })
            }
            return node
        })
    }

    const UserInfo = useUserInfo()
    //根据权限过滤操作栏
    function authOptArr(node,optArr){
        let currentArr = []
        if(node.Level===2){ //目录改为三级后，二级目录也可以移动
            !optArr.find(i=>i.id==='moveTo')&&optArr.push({
                label:'移动至',
                icon:iconSrc.moveTo,
                id:'moveTo'
            })
        }
        optArr.forEach(opt=>{
            if(checkOpt(node,opt)){
                currentArr.push(opt)
            }
        })
        return currentArr
    }
    function checkOpt(node,opt){
        const {authRole} = opt
        const {AdminId,RoleTypeCode} = UserInfo.value
        const roleType = ['rai_admin', 'ficc_admin','admin'].includes(RoleTypeCode)?'admin':RoleTypeCode
        //在原先的基础上加上权限设置
        const authMap = {
            'addNext':chartLibBtn.chartLib_classifyOpt_add,
            'reName':chartLibBtn.chartLib_classifyOpt_add,
            'delete':chartLibBtn.chartLib_del,
            'addChart':chartLibBtn.chartLib_addMy
        }
        if(opt.id==='moveTo'||opt.id==='moveChart'){
            return node.SysUserId===AdminId||roleType==='admin'
        }
        if(authRole){
            return (authRole===roleType)&&checkAuthBtn(authMap[opt.id])
        }
        return checkAuthBtn(authMap[opt.id])
    }





    return {
        optArrNode,
        optArrItem,
        optArrChart,

        catalogNodes,
        currentLang,
        getCatalogList,

        UserInfo,
        authOptArr
    }
}