
import Trigger from './trigger'
import {getOffset,getDefaultTarget} from '@main/utils'
import {filterEmpty} from '@main/utils/props'
import PopMenu from './popMenu'
const SubMenu = {
    inject:["showAllFuncBox","hideAllFuncBox"],
    props:{
        eventKey:{
            type:String
        },
        mode:{
            type:String
        },
        level:{
            type:Number
        },
        parent: {
            type: Object,
            default: null
        },

        isAllFunc:{
            type:Boolean,
        },
        placement:{
            type:String,
            default:'bottom'
        }
    },
    data(){
        return{
            subStyle:{
                left:0,
                top:0,
                display:'none'
            },
            isActive:false,
        }        
    },
    mounted(){ 
        let me=this; 
        let subMenuDom = me.$refs['subMenus'].$el.firstElementChild;
        let h = document.documentElement.offsetHeight;
        let header = document.querySelector(".pl-header");
        let navbar = document.querySelector(".pl-navbar");
        let headH = (header ? header.offsetHeight : 0) + (navbar ? navbar.offsetHeight : 0);
        if(!subMenuDom.dataWidth){
            subMenuDom.dataWidth=subMenuDom.offsetWidth;
            (me.level!=0 || me.placement=="right") &&(subMenuDom.style.width = '0');
        }
        if(!subMenuDom.dataHeight){
            subMenuDom.dataHeight=(subMenuDom.offsetHeight>h-headH?h-headH:subMenuDom.offsetHeight)+1;
            me.level==0 && me.placement.includes("bottom") &&(subMenuDom.style.height = '0');
        }
    },
    methods:{
        setPotion(){
            let me=this;
            let rect = getOffset(me.$refs['domMenuTitle'],getDefaultTarget()); 
            let subStyle = {
                left: (me.level==0&&me.placement.includes("bottom"))?rect.left:rect.left+rect.width,
                top: (me.level==0&&me.placement.includes("bottom"))?rect.top+rect.height-1:rect.top,
                width:rect.width,
                height:rect.height,
            }
            if(me.level==0&&me.isAllFunc){
                subStyle.left+=10;
            }
            
            return subStyle;
        },
        show(){
            let me=this; 
            if(me.level!=0){
                me.parent.show&&me.parent.show();
            }
            if(me.isAllFunc){
                me.showAllFuncBox();//当在全部功能中展现菜单，不收起全部功能下拉框
            }
            let p = me.setPotion(),     
                h = document.documentElement.offsetHeight,
                w = document.documentElement.offsetWidth,            
                subMenuDom = me.$refs['subMenus'].$el.firstElementChild,
                header = document.querySelector(".pl-header"),
                navbar = document.querySelector(".pl-navbar"),
                headH = (header ? header.offsetHeight : 0) + (navbar ? navbar.offsetHeight : 0);
            //subMenuDom.style.display = 'block'; 
            let sh = subMenuDom.offsetHeight,
                sw = subMenuDom.offsetWidth;
            sh = sh>(h-headH) ? (h-headH) : sh;
            if(subMenuDom.dataWidth){
                sw=subMenuDom.dataWidth;
            }
            if(subMenuDom.dataHeight){
                sh=subMenuDom.dataHeight;
            }
            if(p.left+sw>w){
                subMenuDom.style.left="auto";
                subMenuDom.style.right=-(me.level>0?p.left-p.width:w)+'px';
            }else{
                subMenuDom.style.right="auto";
                subMenuDom.style.left=(me.level>0?p.left-10:p.left)+'px';
            }
            subMenuDom.style.top = ((sh+p.top)>h?(h-sh):p.top)+'px';
            subMenuDom.style.opacity = '1'; 
            subMenuDom.style.height = sh+'px'; 
            subMenuDom.style.width = sw+'px'; 
            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;
            if(me.level==0 && me.placement.includes("bottom")){
                subMenuDom.style.height = '0';
            }else{
                subMenuDom.style.width = '0';
            }
            // 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 me=this;
            let {...props} = me.$props;
            const {subStyle,isActive} = me;
            let popMenuProps={
                props:{
                    ...props,
                    level:props.level+1,
                    children:children,
                    parent:me
                },
                style:{
                    // ...subStyle,                
                },
                class:{
                    ['pl-menus-sub']:1,
                    ['pl-menus-pop']:1,
                },
                on:{
                    mouseenter:me.handleSubMouseEnter,
                    mouseleave:me.handleSubMouseLeave
                }
            }
            if(me.isAllFunc || me.placement!="bottom"){
                popMenuProps.class['allFuncSubMenu']=1;
            }
            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;