<script>
import DisableScroll from './DisableScroll';
import { RouterLive, callFn } from 'nimble-lib';
export default {
    extends: DisableScroll,
    props: {
        /**
         * 路由选项
         */
        routerOption: {
            type: Object
        }
    },
    data() {
        return {
            pageList: [] // 页面列表
        };
    },
    created() {
        let _that = this;
        let _routerOption = _that.routerOption || {};
        if (!_that._routerLive && !_routerOption.notRouter && _routerOption.queryKey) {
            _that._routerLive = new RouterLive(_routerOption); // 路由
            _that.onPopstate = function(evt) {
                _that._onPopstate(evt);
            };
            window.addEventListener('popstate', _that.onPopstate); // history路由的时候
        }
    },
    beforeDestroy() {
        let _that = this;
        if (_that.onPopstate) {
            _that._goBackPage();
            window.removeEventListener('popstate', _that.onPopstate);
        // window.removeEventListener('hashchange', _that.popstate.bind(_that)); // hash路由的时候
        }
    },
    methods: {
        /**
         * 跳转页面
         * @param {Object} params url参数
         * @param {String} url url参数
         *
         * @return {String}
         */
        _toPage(params, url) {
            let _that = this;
            let _routerLive = _that._routerLive;
            if (!_routerLive) {
                return;
            }
            let _pageType = params && params.pageName;
            let _pageList = _that.pageList;
            if (_pageType && _that._outPage !== _pageType &&
                _pageList[_pageList.length - 1] !== _pageType) {
                _pageList.push(_pageType);
            } else {
                _that._outPage = null;
            }
            _that._pageList = _that._pageList || [];
            return _routerLive && _routerLive.toPage(params, url);
        },
        /**
         * 回退页面
         *
         * @param {Object} params url参数
         * @return {Promise}
         * @memberof CarBase
         */
        _goBackPage(params) {
            let _routerLive = this._routerLive;
            return _routerLive && _routerLive.goBackPage(params);
        },
        /**
         * url变化的事件
         * @param {Event} evt 事件对象
         */
        _onPopstate(evt) {
            let _that = this;
            let hookIs = _that.$refs.hook_is;
            let $options = hookIs && hookIs.$options;
            if (_that._routerLive) {
                let pageList = _that.pageList;
                let oldPage;
                let nowPage;
                if (pageList && pageList.length > 0) {
                    oldPage = pageList.pop();
                    nowPage = pageList[pageList.length - 1];
                    _that._outPage = nowPage; // 标记后退
                }
                let upPage = pageList.length >= 2 && pageList[pageList.length - 2];
                $options && callFn($options.popupPopstate, [{
                    Event: evt,
                    oldPage,
                    nowPage,
                    upPage
                }], hookIs);
            }
        }
    }
};
</script>
