<template>
    <div id="app">
        <w-header></w-header>
        <transition :name="transitionName">
            <keep-alive>
                <router-view class="child-view"></router-view>
            </keep-alive>
        </transition>
        <w-spinner></w-spinner>
    </div>
</template>

<script>
    import WSpinner from "./components/WSpinner";
    import WHeader from "./components/WHeader";
    export default {
        components: {WHeader, WSpinner},
        data () {
            return {
                transitionName: null,
            }
        },
        mounted() {
            this.sessionStorage('/', 1);
            //
            let hash = window.location.hash;
            if (hash.indexOf("#") === 0) {
                hash = hash.substr(1);
                if (hash !== '/' && this.sessionStorage(hash) === 0) {
                    this.sessionStorage(hash, this.sessionStorage('::count') + 1);
                }
            }
            //
            setInterval(() => {
                this.searchEnter();
            }, 1000);
            //
            this.handleWebSocket();
            $A.setOnUserInfoListener("app", () => { this.handleWebSocket() });
        },
        watch: {
            '$route' (To, From) {
                if (this.transitionName === null) {
                    this.transitionName = 'app-slide-no';
                    return;
                }
                if (typeof To.name === 'undefined' || typeof From.name === 'undefined') {
                    return;
                }
                this.slideType(To, From)
            }
        },
        methods: {
            slideType(To, From) {
                let isBack = this.$router.isBack;
                this.$router.isBack = false;
                //
                let ToIndex = this.sessionStorage(To.path);
                let FromIndex = this.sessionStorage(From.path);
                if (ToIndex && ToIndex < FromIndex) {
                    isBack = true;      //后退
                    this.sessionStorage(true, ToIndex);
                }else{
                    isBack = false;     //前进
                    this.sessionStorage(To.path, this.sessionStorage('::count') + 1);
                }
                //
                if (To.meta.slide === false || From.meta.slide === false)
                {
                    //取消动画
                    this.transitionName = 'app-slide-no'
                }
                else if (To.meta.slide === 'up' || From.meta.slide === 'up' || To.meta.slide === 'down' || From.meta.slide === 'down')
                {
                    //上下动画
                    if (isBack) {
                        this.transitionName = 'app-slide-down'
                    } else {
                        this.transitionName = 'app-slide-up'
                    }
                }
                else
                {
                    //左右动画（默认）
                    if (isBack) {
                        this.transitionName = 'app-slide-right'
                    } else {
                        this.transitionName = 'app-slide-left'
                    }
                }
            },
            sessionStorage(path, num) {
                let conut = 0;
                let history = JSON.parse(window.sessionStorage['__history__'] || '{}');
                if (path === true) {
                    let items = {};
                    for(let i in history){
                        if (history.hasOwnProperty(i)) {
                            if (parseInt(history[i]) <= num) {
                                items[i] = history[i];
                                conut++;
                            }
                        }
                    }
                    history = items;
                    history['::count'] = Math.max(num, conut);
                    window.sessionStorage['__history__'] = JSON.stringify(history);
                    return history;
                }
                if (typeof num === 'undefined') {
                    return parseInt(history[path] || 0);
                }
                if (path === "/") num = 1;
                history[path] = num;
                for(let key in history){ if (history.hasOwnProperty(key) && key !== '::count') { conut++; } }
                history['::count'] = Math.max(num, conut);
                window.sessionStorage['__history__'] = JSON.stringify(history);
            },

            searchEnter() {
                let row = $A(".sreachBox");
                if (row.length === 0) {
                    return;
                }
                if (row.attr("data-enter-init") === "init") {
                    return;
                }
                row.attr("data-enter-init", "init");
                //
                let buttons = row.find("button[type='button']");
                let button = null;
                if (buttons.length === 0) {
                    return;
                }
                buttons.each((index, item) => {
                    if ($A(item).text().indexOf("搜索")) {
                        button = $A(item);
                    }
                });
                if (button === null) {
                    return;
                }
                row.find("input.ivu-input").keydown(function(e) {
                    if (e.keyCode == 13) {
                        if (!button.hasClass("ivu-btn-loading") ) {
                            button.click();
                        }
                    }
                });
            },

            handleWebSocket(force) {
                if ($A.getToken() === false) {
                    $A.WS.close();
                } else {
                    $A.WS.setOnMsgListener("app", (msgDetail) => {
                        if (msgDetail.sender == $A.getUserName()) {
                            return;
                        }
                        if (msgDetail.messageType != 'send') {
                            return;
                        }
                        let content = $A.jsonParse(msgDetail.content)
                        if (content.type == 'taskA') {
                            $A.triggerTaskInfoListener(content.act, content.taskDetail, false);
                        }
                    }).connection(force);
                }
            }
        }
    }
</script>

<style>
    body { overflow-x: hidden; }
</style>
<!--suppress CssUnusedSymbol -->
<style scoped>
    .child-view {
        position: absolute;
        width: 100%;
        min-height: 100%;
        background-color: #f1f2f7;
        transition: all .3s cubic-bezier(.55, 0, .1, 1);
    }
    .app-slide-no-leave-to {display: none;}
    /**
     * 左右模式
     */
    .app-slide-left-leave-active{z-index:1;transform:translate(0,0)}
    .app-slide-left-leave-to{z-index:1;transform:translate(0,0)}
    .app-slide-left-enter-active{opacity:0;z-index:2;transform:translate(30%,0)}
    .app-slide-left-enter-to{opacity:1;z-index:2;transform:translate(0,0)}
    .app-slide-right-leave-active{opacity:1;z-index:2;transform:translate(0,0)}
    .app-slide-right-leave-to{opacity:0;z-index:2;transform:translate(30%,0)}
    .app-slide-right-enter-active{z-index:1;transform:translate(0,0)}
    .app-slide-right-enter{z-index:1;transform:translate(0,0)}

    /**
     * 上下模式
     */
    .app-slide-up-leave-active{z-index:1;transform:translate(0,0)}
    .app-slide-up-leave-to{z-index:1;transform:translate(0,0)}
    .app-slide-up-enter-active{opacity:0;z-index:2;transform:translate(0,20%)}
    .app-slide-up-enter-to{opacity:1;z-index:2;transform:translate(0,0)}
    .app-slide-down-leave-active{opacity:1;z-index:2;transform:translate(0,0)}
    .app-slide-down-leave-to{opacity:0;z-index:2;transform:translate(0,20%)}
    .app-slide-down-enter-active{z-index:1;transform:translate(0,0)}
    .app-slide-down-enter{z-index:1;transform:translate(0,0)}
</style>
