import React from "react"
import {Tooltip,Typography,notification} from "antd"
import { heightLight } from "./heightLignt"

const {Text} = Typography

export const pageUtil={
    getBodyHeight:()=>{
        if(window.bodyheight){
            return window.bodyheight
        }else{
            return document.body.clientHeight
        }
    },
    showToolTip:(text:any,action?:Function,body?:any)=>{
        try {
            let name = text
            let title = text
            try {
                text = name.replace(/(\r\n|\n|\r)/gm, "↵")
                if(title.includes("\n")){
                    title=<pre style={{fontFamily:"宋体"}}>{title}</pre>
                }
            } catch (error) {
                
            }
            if(action){
                const bodys = body?body:(text||'')
                return(
                    <Tooltip title={title||''}>
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <a onClick={()=>{action()}}>
                                {bodys}
                            </a>
                        </div>
                    </Tooltip>
                )
            }else{
                const bodys = body?body:<span>{text||''}</span>
                return(
                    <Tooltip title={title||''}>
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <span>{bodys}</span>
                        </div>
                    </Tooltip>
                )
            }
        } catch (error) {
            console.log(error)
            return text
        }
    },
    showToolTipWithImg:(text:any)=>{
        try {
            return(
                <Tooltip title={<div style={{width:500,maxHeight:300,overflow:'auto'}} dangerouslySetInnerHTML={{__html: text}} ></div>}>
                    <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                        {text}
                    </div>
                </Tooltip>
            )
        } catch (error) {
            console.log(error)
            return text
        }
    },
    showToolTipWithHighLight:(text:any,action?:Function,body?:any)=>{
        let success = true
        try {
            let name = text
            try {
                text = name.replace(/(\r\n|\n|\r)/gm, "↵")
                text = heightLight.heightLightString(text)
            } catch (error) {
                success =false
            }
            if(action){
                const bodys = body?body:(text||'')
                return(
                    <Tooltip title={text||''}>
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <a onClick={()=>{action()}}>
                                {bodys}
                            </a>
                        </div>
                    </Tooltip>
                )
            }else{
                const bodys = body?body:<span>{text||''}</span>
                return(
                    <Tooltip title={text||''}>
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <span>{bodys}</span>
                        </div>
                    </Tooltip>
                )
            }
        } catch (error) {
            console.log(error)
            return text
        }
    },
    showChildrenToolTip:(text:any,action?:Function,body?:any)=>{
        try {
            let name = text
            try {
                text = name.replace(/(\r\n|\n|\r)/gm, "↵")
            } catch (error) {
                
            }
            if(action){
                const bodys = body?body:(text||'')
                return(
                    <Tooltip title={bodys} color="#fff" overlayStyle={{maxWidth:600}}>
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <a onClick={()=>{action()}}>
                                {text}
                            </a>
                        </div>
                    </Tooltip>
                )
            }else{
                const bodys = body?body:<span>{text||''}</span>
                return(
                    <Tooltip title={bodys} color="#fff">
                        <div style={{width:'100%',overflow: 'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}}>
                            <span>{text}</span>
                        </div>
                    </Tooltip>
                )
            }
        } catch (error) {
            console.log(error)
            return text
        }
    },
    checkMenuAdmit:(menuinfo:any,menu:any,menuadmit:any)=>{
        console.log('begin')
        let tempmenu = menuinfo.split('/')
        let queryurl = null
        if(tempmenu.length>2){
            queryurl = `/view/${tempmenu[tempmenu.length-1]}`
        }else{
            notification['info']({
                message:'提示',
                description:`路由${menuinfo}格式异常`,
                duration:5
            })
           return false
        }
        let menumessage = (menu.mapurl[queryurl])
        if(menumessage&&menumessage.href!=='/view/index'){
            let type = menumessage.id.split("-")
            let key = ""
            if (type[0] !== "item") {
                key = "sub-" + type[0]
            } 
            if(key){
                try {
                    let parent = menu.parentIdMap[key]||''
                    let totalbit = menuadmit[parent.name];
                    if(!parent.hidden){
                        //(menumessage.bit&totalbit)===menumessage.bit
                        if(totalbit.includes(menumessage.name)){
                            return true
                        }else{
                            notification['info']({
                                message:'提示',
                                description:`暂无访问菜单【${menumessage.name}】的权限`,
                                duration:5
                            })
                            return false
                        }
                    }else{
                        notification['info']({
                            message:'提示',
                            description:`暂无访问菜单【${menumessage.name}】的权限`,
                            duration:5
                        })
                        return false
                    }
                } catch (error) {
                    notification['info']({
                        message:'提示',
                        description:`暂无访问路由${menuinfo}的权限`,
                        duration:5
                    })
                   return false
                }
            }else{
                notification['info']({
                    message:'提示',
                    description:`暂无访问路由${menuinfo}的权限`,
                    duration:5
                })
               return false
            }
        }else{
            return false
        }
    },
    changeLevel:(text:any)=>{
        if(text===1||text==='1'){
            return '一级'
        }else if(text===2||text==='2'){
            return '二级'
        }else if(text===3||text==='3'){
            return '三级'
        }else if(text===4||text==='4'){
            return '四级'
        }else{
            return text
        }
    }
}