import { useSetState,useDebounceEffect } from "ahooks"
import {useEffect} from "react"
import {metaManageService,metaAdminManageService,metaUserManageService} from "@/services"
import {httpUtil} from "@/utils"

export const useGetMetaCnName=()=>{
    const [state,setState] = useSetState({
        cnNameLoad:false,
        modelCnName:{}
    })

    useEffect(()=>{
        getCnNameInfo()
    },[])

    const getCnNameInfo=async()=>{
        setState({cnNameLoad:true})
        const result:any = await metaManageService.getModelsCnName({});
        if(httpUtil.checkSuccess(result,1)){
            setState({cnNameLoad:true,modelCnName:result.data})
        }else{
            setState({cnNameLoad:true,modelCnName:{}})
        }
    }

    return state
}

export const useGetSearchType=()=>{
    const [state,setState] = useSetState({
        typelistload:false,
        typelist:[]
    })

    useEffect(()=>{
        getCnNameInfo()
    },[])

    const getCnNameInfo=async()=>{
        setState({typelistload:true})
        const result:any = await metaManageService.getModelsCnName({});
        if(httpUtil.checkSuccess(result,1)){
            let list:any=[]
            for(let key in result.data){
                list.push({value:result.data[key],name:key})
            }
            setState({typelistload:true,typelist:list})
        }else{
            setState({typelistload:true,typelist:[]})
        }
    }

    return state
}

interface p3{
    treeNode:any,
    currentDomain:any,
    type?:string,
    keyword?:string
}

export const useGetMetaTableInfo=(props:p3)=>{
    const {treeNode,currentDomain,type='admin',keyword} = props

    const [state,setState] = useSetState({
        pageable:{
            pageNum:1,
            pageSize:20,
        },
        order:{
            order:'',
            orderBy:''
        },
    })

    const [select,setSelect] = useSetState({
        selectedRowKeys:[],
        selectedRowDatas:[]
    })

    const [payload,setPayload] = useSetState({
        tablelist:[],
        tableload:false,
        tabletotal:0
    })

    useEffect(()=>{
        setState({pageable:{
            pageNum:1,
            pageSize:20,
        }})
    },[treeNode,keyword])

    useDebounceEffect(()=>{
        if(currentDomain.domainId)
            getTableInfo()
    },[treeNode,state,currentDomain],{wait:600})

    const getTableInfo=async()=>{
        setPayload({tableload:true})
        let payload:any={}
        payload={
            data:treeNode.path?[treeNode.path]:[],
            params:{
                parentClass:'Catalog',
                parentNamePath:currentDomain.domainId,
                system:treeNode.system?treeNode.system:treeNode.value,
                pageNum:state.pageable.pageNum,
                pageSize:state.pageable.pageSize,
                keyword:keyword,
                catalog:currentDomain.domainId,
                parentId:treeNode.path?treeNode.parentId:treeNode.value,
                ...state.order,
                sortField:state.order.orderBy
            }
        }
        let result = null;
        if(typeof((treeNode.value?treeNode.value:treeNode.key))==='string'){
            if(type==='admin'){
                result = await metaAdminManageService.getChildByIdAndSysByPage(payload,undefined,true)
            }else{
                result = await metaUserManageService.getChildByIdAndSysByPage(payload,undefined,true)
            }
            //const result:any= await metaManageService.getChildByIdAndSysByPage(payload,undefined,true)
            if(httpUtil.checkSuccess(result,1)){
                //dispatch(setField({tablelist:result.data.content,tableload:false,tabletotal:result.data.totalElements}))
            }else{
                //dispatch(setField({tablelist:[],tableload:false,tabletotal:0}))
                httpUtil.showError(result,1)
            }
        }else{
            if(type==='admin'){
                result = await metaAdminManageService.getSystemChildrenByPage(payload,undefined,true)
            }else{
                result = await metaUserManageService.getSystemChildrenByPage(payload,undefined,true)
            }
            //const result:any= await metaManageService.getSystemChildrenByPage(payload,undefined,true)
            if(httpUtil.checkSuccess(result,1)){
                //dispatch(setField({tablelist:result.data.content,tableload:false,tabletotal:result.data.totalElements}))
            }else{
                //dispatch(setField({tablelist:[],tableload:false,tabletotal:0}))
                httpUtil.showError(result,1)
            }
        }
        console.log(result)
        if(httpUtil.checkSuccess(result,1)){
            setPayload({tablelist:result.data.content,tableload:false,tabletotal:result.data.totalElements})
        }else{
            setPayload({tablelist:[],tableload:false,tabletotal:0})
        }
    }

    const changePage=(pageNum:any,pageSize:any)=>{
        setState({pageable:{pageNum,pageSize}})
    }

    const changeOrder=(order:any)=>{
        setState({order})
    }

    const changeSelect=(keys:any,datas?:any)=>{
        setSelect({selectedRowKeys:keys,selectedRowDatas:datas})
    }

    return {...payload,...state,...select,changePage,changeOrder,changeSelect}

}