<template>
<div class="app-logout-view app-logout-view">
    <img class="logout-bg" src="/assets/img/logout_bg.png"/>
    <div class="logout-container">
        尊敬的客户您好，您已成功退出系统，将在 <a>{{this.countdown}}</a> 秒后跳转至<a @click="gotoLoginView">登录页</a>！
    </div>
</div>
</template>

<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { UIActionTool } from '@/utils';
import { Subject } from 'rxjs';




@Component({
    components: {
    },
})
export default class AppLogoutViewBase extends Vue {

    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof AppLogoutViewBase
     */
    @Emit() 
    protected viewDatasChange(val: any):any {
        return val;
    }

    /**
     * 传入视图上下文
     *
     * @type {string}
     * @memberof AppLogoutViewBase
     */
    @Prop() protected viewdata!: string;

    /**
     * 传入视图参数
     *
     * @type {string}
     * @memberof AppLogoutViewBase
     */
    @Prop() protected viewparam!: string;

    /**
     * 视图默认使用
     *
     * @type {boolean}
     * @memberof AppLogoutViewBase
     */
    @Prop({ default: true }) protected viewDefaultUsage!: boolean;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof AppLogoutViewBase
	 */
	protected viewtag: string = '15920fe0e07590c82ff28b22395d9492';

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof AppLogoutViewBase
     */
    protected model: any = {
        srfTitle: '应用注销视图',
        srfCaption: 'app.views.applogoutview.caption',
        srfSubCaption: '',
        dataInfo: ''
    }

    /**
     * 视图参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppLogoutViewBase
     */
    @Watch('viewparam',{immediate: true, deep: true})
    onParamData(newVal: any, oldVal: any) {
        if(newVal){
            Object.assign(this.viewparams, JSON.parse(this.viewparam));
        } 
    }

    /**
     * 处理应用上下文变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppLogoutViewBase
     */
    @Watch('viewdata')
    onViewData(newVal: any, oldVal: any) {
        const _this: any = this;
        if (!Object.is(newVal, oldVal) && _this.engine) {
            _this.parseViewParam();
            _this.engine.load();
        }
        
    }

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof AppLogoutViewBase
     */
    protected containerModel: any = {
    };

    /**
     * 视图状态订阅对象
     *
     * @private
     * @type {Subject<{action: string, data: any}>}
     * @memberof AppLogoutViewBase
     */
    protected viewState: Subject<ViewState> = new Subject();




    /**
     * 引擎初始化
     *
     * @private
     * @memberof AppLogoutViewBase
     */
    private engineInit(): void {
    }

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof AppLogoutViewBase
     */
    protected context:any = {};

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppLogoutViewBase
     */
    protected viewparams:any = {};

    /**
     * 解析视图参数
     *
     * @private
     * @memberof AppLogoutViewBase
     */
    private parseViewParam(): void {
        if (!this.viewDefaultUsage && this.viewdata && !Object.is(this.viewdata, '')) {
            Object.assign(this.context, JSON.parse(this.viewdata));
            return;
        }
        const path = (this.$route.matched[this.$route.matched.length - 1]).path;
        const keys: Array<any> = [];
        const curReg = this.$pathToRegExp.pathToRegexp(path, keys);
        const matchArray = curReg.exec(this.$route.path);
        let tempValue: Object = {};
        keys.forEach((item: any, index: number) => {
            Object.defineProperty(tempValue, item.name, {
                enumerable: true,
                value: matchArray[index + 1]
            });
        });
        this.$viewTool.formatRouteParams(tempValue,this.context,this.viewparams);
    }

    /**
     * Vue声明周期
     *
     * @memberof AppLogoutViewBase
     */
    protected created() {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     * @memberof AppLogoutViewBase
     */    
    protected afterCreated(){
        const secondtag = this.$util.createUUID();
        this.$store.commit('viewaction/createdView', { viewtag: this.viewtag, secondtag: secondtag });
        this.viewtag = secondtag;
        this.parseViewParam();
        
    }

    /**
     * 销毁之前
     *
     * @memberof AppLogoutViewBase
     */
    protected beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
    }

    /**
     * Vue声明周期(组件初始化完毕)
     *
     * @memberof AppLogoutViewBase
     */
    protected mounted() {
        this.afterMounted();
    }

    /**
     * 执行mounted后的逻辑
     * 
     * @memberof AppLogoutViewBase
     */
    protected afterMounted(){
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        this.timeEvent = setInterval(() => {
    if(this.countdown === 0) {
        this.gotoLoginView();
    }
    this.countdown--;
}, 1000);

    }




    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof AppLogoutViewBase
     */
    protected closeView(args: any[]): void {
        let _view: any = this;
        if (_view.viewdata) {
            _view.$emit('viewdataschange', [args]);
            _view.$emit('close', [args]);
        } else if (_view.$tabPageExp) {
            _view.$tabPageExp.onClose(_view.$route.fullPath);
        }
    }

    /**
     * 倒计时
     *
     * @type {number}
     * @memberof LogoutView
     */
    public countdown: number = 5;

    /**
     * 定时标识
     *
     * @type {*}
     * @memberof LogoutView
     */
    public timeEvent: any;

    /**
     * 跳转登录页
     *
     * @memberof LogoutView
     */
    public gotoLoginView() {
        if(this.timeEvent) {
            clearInterval(this.timeEvent);
            this.$router.push({name: 'login'});
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof Default
     */
    public destroyed() {
        if (this.timeEvent) {
            clearInterval(this.timeEvent);
        }
    }

}
</script>

<style lang='less'>
@import './app-logout-view.less';
</style>