
import Trigger from './trigger'
import {getOffset,getDefaultTarget} from '@main/utils'
import {filterEmpty} from '@main/utils/props'
import PopMenu from './popMenu'
const SubMenu = {
    props:{
        eventKey:{
            type:String
        },
        mode:{
            type:String
        },
        level:{
            type:Number
        },
        parent: {
            type: Object,
            default: null
        }
    },
    data(){
        return{
            subStyle:{
                left:0,
                top:0,
                display:'none'
            },
            isActive:false,
        }        
    },
    methods:{
        setPotion(){
            let me=this;
            let rect = getOffset(me.$refs['domMenuTitle'],getDefaultTarget()); 
            let subStyle = {
                left: me.level==0?rect.left:rect.left+rect.width,
                top: me.level==0?rect.top+rect.height-1:rect.top,
                width:rect.width
            }
            return subStyle;
        },
        show(){
            let me=this; 
            if(me.level!=0){
                me.parent.show&&me.parent.show();
            }
            let p = me.setPotion(),     
                h = document.documentElement.offsetHeight,
                w = document.documentElement.offsetWidth,            
                subMenuDom = me.$refs['subMenus'].$el.firstElementChild,
                headH=document.querySelector(".pl-header").offsetHeight+document.querySelector(".pl-navbar").offsetHeight;
            subMenuDom.style.display = 'block'; 
            let sh = subMenuDom.offsetHeight,
                sw = subMenuDom.offsetWidth;
            sh = sh>(h-headH) ? (h-headH) : sh;
            if(me.level>0){
                subMenuDom.style.left = (p.left+sw>w?p.left-p.width-sw:p.left-10)+'px';
            }else{
                subMenuDom.style.left = (p.left+sw>w?w-sw:p.left)+'px';
            }
            subMenuDom.style.top = ((sh+p.top)>h?(h-sh):p.top)+'px';
            subMenuDom.style.height = sh+'px'; 
            subMenuDom.style.display = 'block'; 
            let zIndex = window.getComputedStyle(this.$el.parentNode,null)['z-index']*1;
            subMenuDom.style.zIndex=zIndex+1;

            // me.$set(me,'subStyle',{display:'block',...p}); 
            // me.subStyle.display = 'block'; 
            me.isActive =true;
        },
        hide(){
            let me=this;   
            let subMenuDom = me.$refs['subMenus'].$el.firstElementChild;
            subMenuDom.style.display = 'none'; 
            // me.subStyle.display = 'none';
            me.isActive = false;
        },
        hideParent(){ 
            let me=this;     
            if(me.level!=0){
                me.parent.hide&&me.parent.hideParent();
            }
            this.hide();
        },
        handleTitleMouseEnter(evt){ 
            this.show();
        },
        handleTitleMouseLeave(evt){
            this.hide();
        },
        handleSubMouseEnter(){
            this.show();
            this.$emit('mouseenter');
        },
        handleSubMouseLeave(){      
            this.hideParent();
            this.$emit('mouseleave');
        },
        renderChildren(children){
            let {...props} = this.$props;
            const {subStyle,isActive} = this;
            let popMenuProps={
                props:{
                    ...props,
                    level:props.level+1,
                    children:children,
                    parent:this
                },
                style:{
                    // ...subStyle,                
                },
                class:{
                    ['pl-menus-sub']:1,
                    ['pl-menus-pop']:1,
                },
                on:{
                    mouseenter:this.handleSubMouseEnter,
                    mouseleave:this.handleSubMouseLeave
                }
            }
            return (
                <PopMenu ref="domMenuSub" {...popMenuProps}></PopMenu>
            )
        }
    },
    render(h) {
        const props = this.$props;
        const {subStyle,isActive} = this;
        const isInlineMode = props.mode === 'inline';        
        const children = this.renderChildren(filterEmpty(this.$slots.default));
        const nodeProps ={
            class:{
                ['pl-menus-item']:1,
                ['pl-menus-itemSeld']:isActive
            }
        }
        const titleProps={
            attrs:{
                title:this.$slots.title[0].text
            },
            on:{
                mouseenter:this.handleTitleMouseEnter,
                mouseleave:this.handleTitleMouseLeave
            }
        }
        /*                     
            {isInlineMode && children}
            {!isInlineMode && (
            <Trigger>
                {children}
            </Trigger>)}
        */
       /**
        * 菜单太多的问题
        * 子菜单独立渲染 添加滚动条
        */

        return (
            <li {...nodeProps} >
                <div ref="domMenuTitle" class="title" {...titleProps}>
                    {props.level>0&&(<i class="iconfont icon-arrow-right trigger"></i>)}
                    <span class='txt'>{this.$slots.title}</span>
                </div>
                <Trigger ref="subMenus">{children}</Trigger>
                {/* {props.level==0&&(
                    <Trigger>
                        {children}
                    </Trigger>)}
                {
                    props.level>0&&children
                } */}
            </li>
        );
    }
}
export default SubMenu;