<template>
    <div v-if="layout == 'level'" class="pl-page" :class="isFullscreen?'showTab':''">
        <div :style="fullScreenTabHandleStyle" @mouseenter="showTab"></div>
        <Header v-show="!isFullscreen" :hasTitle="hasTitle" :logoWidth="logoWidth" ref="Header" style="box-shadow: 0 5px 7px rgba(221,221,221,0.5);z-index:10"></Header>
        <!-- <Navbar></Navbar> -->
        <Sidebar ref="Sidebar" :hasTitle="hasTitle" :sideWidth="sideWidth" :totop="totop" style="z-index: 11"></Sidebar>
        <Main ref="Main" @onFullScreen="handleFullScreen" :style="{left:toleft, top:totop+'px'}"></Main>
    </div>
    <div v-else-if="layout=='concise'" class="pl-page" :class="isFullscreen?'showTab':''">
        <div :style="fullScreenTabHandleStyle" @mouseenter="showTab"></div>
        <!-- <Header ref="Header" :logoWidth="logoWidth" v-show="!isFullscreen"></Header> -->
        <Sidebar ref="Sidebar" v-show="false"></Sidebar>
        <ConciseSidebar ref="ConciseSidebar" style="width:36px;" :class="isFullscreen?'showFuncMenu':''"></ConciseSidebar>
        <Main ref="Main" @onFullScreen="handleFullScreen" style="left:36px;top:0"></Main>
    </div>
    <div v-else class="pl-page" :class="isFullscreen?'showTab':''">
        <div :style="fullScreenTabHandleStyle" @mouseenter="showTab"></div>
        <Header ref="Header" :logoWidth="logoWidth" v-show="!isFullscreen"></Header>
        <Navbar ref="Navbar" v-show="!isFullscreen"></Navbar>
        <Sidebar ref="Sidebar" v-show="hasQuickNav&&!isFullscreen"></Sidebar>
        <Main ref="Main" @onFullScreen="handleFullScreen" :style="{left:hasQuickNav?71:0}"></Main>
    </div>
</template>
<script>
import {baseURL,codeProjectName,requestUrl} from '@main/config'
import {getMenus} from '@main/api/menu'
import {checkUrl} from '@main/utils/index'
import {Header,Navbar,Sidebar,Main} from "./components"
import ConciseSidebar from "./components/conciseSidebar"
import screenfull from 'screenfull'
// import { setTimeout } from 'timers';
export default {    
    components:{
        Header,Navbar,Sidebar,Main,ConciseSidebar
    },
    provide(){
        let me=this;
        return {
            addTab(item){
                // url,title,uid,callback,nearest                
                // this.addTab(item);//inject 提供的方法只能 传一个参数
                me.addTab(item.ssoUrl||item.resUrl,item.name,item.resId,item.callback,item.nearest,item.openType);
                // me.addTab(url,title,uid,callback,nearest);
            },
            showTab(){
                // console.log('layout index vue : showTab')
                me.showTab();
            },
            hideTab(){
                // console.log('layout index vue : hideTab')
                me.hideTab();
            }
        }
    },
    data(){
        return {
            hasTitle: true,
            toleft: window.__sysConfig.isMenuWordwrap?(window.__sysConfig.leftMenuWidth?(window.__sysConfig.leftMenuWidth+"px"):'141px'):'71px',
            sideWidth: window.__sysConfig.isMenuWordwrap?(window.__sysConfig.leftMenuWidth?(window.__sysConfig.leftMenuWidth+"px"):'140px'):'70px',
            logoWidth:'140px',
            totop: 50,
            isFullscreen: false,
            top: '',
            left: '',
            mainTop: '',
            navTop: '',
            timeout: null,
            sysConfig: this.$store.state.app.config,
        }
    },
    computed:{
        hasQuickNav(){
            return this.$store.getters.navs.length;
        },
        layout(){
            return window.localStorage.getItem('layout');
        },
        isTitle(){
            return this.hasTitle;
        },
        fullScreenTabHandleStyle(){
            let baseStyle={
                position: "absolute",
                left: 0,
                top: 0,
                "z-index": 10000,
            };
            if(this.layout=="level"){
                Object.assign(baseStyle,{
                    height: "100%",
                    width: "1px",
                });
            }else{
                Object.assign(baseStyle,{
                    height: "1px",
                    width: "100%",
                });
            }
            return baseStyle;
        }
    },
    methods:{
        addTab(url,title,uid,callback,nearest,openType){
            console.log('layout addTab :',baseURL,codeProjectName,url);
            let __url=url;
            if(!checkUrl(url)){
                //根据setting配置是否带e5
                __url=(window._global?location.origin:window.HIVUI_SETTING.serverUrl)+url;
            }
            if(openType==2){//弹窗
                this.$refs.Main.menuOpenList.push({
                    title:title,
                    url:__url,
                    uid:uid,
                });
            }else if(openType==3){//浏览器标签打开
                window.open(__url,"_blank");
            }else{//主界面框架标签打开
                window.Sys.addTab(__url,title,uid,callback,nearest);
            }
        },
        hideTitle(name){
            this.hasTitle = name;
            this.toleft = name?'71px':'49px';
            this.sideWidth = name?'70px':'48px'
        },
        /**
         * 是否全屏并按键ESC键的方法
         */
        checkFull() {
            var isFull = document.fullscreenEnabled || window.fullScreen || document.webkitIsFullScreen || document.msFullscreenEnabled
            // to fix : false || undefined == undefined
            if (isFull === undefined) {
                isFull = false
            }
            return isFull
        },
         handleFullScreen(){
            screenfull.toggle()
        },
        onchange(){
            // console.log(this.isFullscreen)
            if(this.isFullscreen==true){
                // this.$refs.Main.isFullscreen = true
                this.$refs.Sidebar.$el.style.top=0
                this.$refs.Sidebar.$el.style.left=0
                this.$refs.Main.$el.style.top=0
                this.$refs.Main.$el.style.left=0
                
                // console.log('222',this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style)
                this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.display='none'
                this.$refs.Sidebar.$el.style.display = 'none'
                this.$refs.Main.$refs['cmp-tabs'].resize()
                if(this.layout === 'concise'){
                    this.$refs.ConciseSidebar.$el.style.display = 'none'
                }
            }else{
                // console.log(222)
                // console.log('top',this.top)
                this.$refs['Sidebar'].$el.style.top=this.mainTop+this.navTop+'px'
                if(!this.sysConfig.isSinglePage){
                    this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.display='block';
                }
                if(this.layout != 'level'&&this.hasQuickNav&&!this.isFullscreen){
                    this.$refs.Sidebar.$el.style.display = 'block';
                }
                if(this.layout === 'level'){
                    this.$refs['Main'].$el.style.top=this.navTop+'px'
                    this.$refs['Main'].$el.style.left=this.left
                }else if(this.layout === 'concise'){
                    this.$refs.Main.$el.style.left=36+'px'
                    this.$refs.ConciseSidebar.$el.style.display = 'flex'
                }else{
                    this.$refs['Main'].$el.style.top=this.mainTop+this.navTop+'px'
                    this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.top=this.mainTop+this.navTop+'px'
                    this.$refs['Main'].$el.style.left=this.left
                    this.$refs['Navbar'].$el.style.top=this.navTop+'px'
                    this.$refs.Navbar.$el.style.display='block'
                }
                this.$refs.Main.$refs['cmp-tabs'].resize()
            }
        },
        showTab(){
            if(this.isFullscreen){
                
                clearTimeout(this.timeout);
                this.timeout = setTimeout(()=>{
                    if(!this.sysConfig.isSinglePage){
                        this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.display='block'
                        this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.top=this.mainTop+'px'
                    }
                    // this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.top='50px'
                    // console.log(this.$refs.Main.$el.style.top)
                    
                    if(this.layout === 'level'){
                        this.$refs.Sidebar.$el.style.display = 'block'
                        if(this.sysConfig.isSinglePage){
                            this.$refs.Sidebar.$el.style.top = '0px'
                        }else{
                            this.$refs.Sidebar.$el.style.top = '39px'
                        }
                    }else if(this.layout === 'concise'){
                        this.$refs.ConciseSidebar.$el.style.display = 'flex'
                        // this.$refs.Navbar.$el.style.display='none';
                        // this.$refs.Navbar.$el.style.top=0
                        // this.$refs.Navbar.$el.style.display='block'
                    }else{
                        // this.$refs.Sidebar.$el.style.display = 'block'
                        this.$refs.Navbar.$el.style.display='none';
                        this.$refs.Navbar.$el.style.top=0
                        this.$refs.Navbar.$el.style.display='block'
                    }
                    // this.$refs.Main.$refs['cmp-tabs'].resize()

                },0);
            }
        },
        hideTab(){
            if(this.isFullscreen){  
                clearTimeout(this.timeout);
                this.timeout = setTimeout(()=>{
                    this.$refs.Main.$el.style.top=0
                    this.$refs.Main.$refs['cmp-tabs'].$refs['domTabHead'].style.display='none'
                    if(this.layout === 'level'){
                        this.$refs.Sidebar.$el.style.display = 'none'
                    }else if(this.layout === 'concise'){
                        this.$refs.ConciseSidebar.$el.style.display='none'
                    }else{
                        this.$refs.Navbar.$el.style.display='none'
                    }
                    // this.$refs.Main.$refs['cmp-tabs'].resize()
                },0);
                
            }
        },
        KeyDown(event) {
            if (event.keyCode === 122) {
                event.returnValue = false;
                this.handleFullScreen();
            }
        }
    },
    created(){
        //单页模式强制左右布局
        if(this.sysConfig.isSinglePage){
            window.localStorage.setItem('layout','level');
        }else if(this.sysConfig.layout&&!window.localStorage.getItem('layout')){
            if(this.sysConfig.layout=="top"){
                window.localStorage.setItem('layout','vertical');
            }else if(this.sysConfig.layout=="left"){
                window.localStorage.setItem('layout','level');
            }else if(this.sysConfig.layout=="concise"){
                window.localStorage.setItem('layout','concise');
            }
        }
    },
    mounted(){
        if(this.layout !== 'concise'){
            this.navTop = this.$refs.Header.$el.offsetHeight
        }
        this.left = this.$refs.Main.$el.style.left
        console.log('left', this.isFullscreen)
        if(this.layout === 'level'){
            this.totop = this.$refs.Header.$el.offsetHeight || 50
        }else if(this.layout !== 'concise'){
            this.mainTop = this.$refs.Navbar.$el.offsetHeight
            // console.log('mainTop', this.mainTop)
        }
        this.onchange();
        window.onresize = () => {
            // 全屏下监控是否按键了ESC
            if (!this.checkFull()) {
            // 全屏下按键esc后要执行的动作
                this.isFullscreen = false
            }
            this.isFullscreen=screenfull.isFullscreen
            this.onchange()
        },
        window.addEventListener("keydown", this.KeyDown, true);//监听按键事件
    }
}
</script>
<style lang="less" scoped>
    .menuOpenDialog{
        ::v-deep .el-dialog__body{
            padding-top:0;
        }
    }
</style>