export default (app) => {
    let div = null

    app.directive('tip', (el, binding, vnode, prevVnode) => {
        el.onmouseover = (event) => {
            console.log(event.currentTarget.getBoundingClientRect())
            div = document.createElement("div")
            div.style.position = 'fixed'
            div.style.width = 'max-content'
            div.style.top = event.x + 'px'
            div.style.left = event.y + 'px'
            div.style.backgroundColor = 'rgb(116,116,116)'
            div.innerHTML = binding.value
            div.style.fontSize = '12px'
            div.style.color = '#fff'
            div.style.padding = '5px'
            div.style.zIndex = '999'
            div.style.borderRadius = '3px'
            div.style.boxShadow = '0px 0px 12px rgba(0, 0, 0, 0.12)'
            document.body.appendChild(div)
            console.log(div)
            edgeMonitoringJudgment(div)
        }

        el.onmouseout = () => {
            document.body.removeChild(div)
        }

    })

    app.beforeUnmount = () => {
        document.body.removeChild(div)
    }


}

function edgeMonitoringJudgment(el) {
    let w, h;
    w = window.innerWidth;
    h = window.innerHeight;
    // 监测右边超出
    if (el.offsetLeft + el.offsetWidth >= w) {
        console.log('超出', w - el.offsetLeft)
        el.style.left = el.offsetLeft - (el.offsetWidth - (w - el.offsetLeft)) + 'px'
        console.log(el)
    }

}


