<template>
<div class="view-container wfaswfproxy-data-view">
    <app-studioaction viewTitle="加签流程代理数据视图" viewName="wfaswfproxydataview"></app-studioaction>
    <card class='view-card' :disHover="true" :padding="0" :bordered="false">
        <div class="content-container">
            <iframe :src="iframeUrl" height="100%" width="100%" seamless="seamless" frameborder="0" scrolling="auto"></iframe>
        </div>
    </card>
</div>
</template>

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




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

    /**
     * 实体服务对象
     *
     * @type {WFASService}
     * @memberof WFASWFProxyDataViewBase
     */
    protected appEntityService: WFASService = new WFASService;


    /**
     * 计数器服务对象集合
     *
     * @type {Array<*>}
     * @memberof WFASWFProxyDataViewBase
     */    
    protected counterServiceArray:Array<any> = [];
    
    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof WFASWFProxyDataViewBase
     */
    @Emit() 
    protected viewDatasChange(val: any):any {
        return val;
    }

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

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

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

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof WFASWFProxyDataViewBase
	 */
	protected viewtag: string = 'a2603cbec3565de6b793584af62a6c77';

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof WFASWFProxyDataViewBase
     */
    protected model: any = {
        srfTitle: '加签流程代理数据视图',
        srfCaption: 'wfas.views.wfproxydataview.caption',
        srfSubCaption: '',
        dataInfo: ''
    }

    /**
     * 视图参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof WFASWFProxyDataViewBase
     */
    @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 WFASWFProxyDataViewBase
     */
    @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 WFASWFProxyDataViewBase
     */
    protected containerModel: any = {
        wflinks: [],
    };

    /**
     *  计数器刷新
     *
     * @memberof WFASWFProxyDataViewBase
     */
    public counterRefresh(){
        const _this:any =this;
        if(_this.counterServiceArray && _this.counterServiceArray.length >0){
            _this.counterServiceArray.forEach((item:any) =>{
                if(item.refreshData && item.refreshData instanceof Function){
                    item.refreshData();
                }
            })
        }
    }

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



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

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

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

    /**
     * 解析视图参数
     *
     * @private
     * @memberof WFASWFProxyDataViewBase
     */
    private parseViewParam(): void {
        if (!this.viewDefaultUsage && this.viewdata && !Object.is(this.viewdata, '')) {
            Object.assign(this.context, JSON.parse(this.viewdata));
            if(this.context && this.context.srfparentdename){
                Object.assign(this.viewparams,{srfparentdename:this.context.srfparentdename});
            }
            if(this.context && this.context.srfparentkey){
                Object.assign(this.viewparams,{srfparentkey:this.context.srfparentkey});
            }
                    this.getIframeUrl();

            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.$route,this.context,this.viewparams);
        if(this.$store.getters.getAppData() && this.$store.getters.getAppData().context){
            Object.assign(this.context,this.$store.getters.getAppData().context);
        }
        //初始化视图唯一标识
        Object.assign(this.context,{srfsessionid:this.$util.createUUID()});
    }

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

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

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

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

    /**
     * 执行mounted后的逻辑
     * 
     * @memberof WFASWFProxyDataViewBase
     */
    protected afterMounted(){
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        
    }




    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof WFASWFProxyDataViewBase
     */
    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);
        }
    }

    /**
     * 销毁视图回调
     *
     * @memberof WFASWFProxyDataViewBase
     */
    protected destroyed(){
        this.afterDestroyed();
    }

    /**
     * 执行destroyed后的逻辑
     * 
     * @memberof WFASWFProxyDataViewBase
     */
    protected afterDestroyed(){
        if(this.viewDefaultUsage){
            let localStoreLength = Object.keys(localStorage);
            if(localStoreLength.length > 0){
                localStoreLength.forEach((item:string) =>{
                if(item.startsWith(this.context.srfsessionid)){
                    localStorage.removeItem(item);
                }
                })
            }
        }
    }
    
    /**
     * iframe路径
     *
     * @memberof @memberof WFASWFProxyDataViewBase
     */
    protected iframeUrl:string ="";

    /**
     * 获取iframe路径
     *
     * @public
     * @memberof WFASWFProxyDataViewBase
     */
    public getIframeUrl(){
        this.appEntityService.WFGetProxyData(this.context,this.viewparams,false).then((response:any) =>{
            if (response && response.status === 200) {
                const data = response.data;
                this.iframeUrl = `${data.proxyviewurl}`;   
            }
        }).catch((response:any) =>{
            if (response && response.status === 401) {
                return;
            }
            if (!response || !response.status || !response.data) {
                this.$Notice.error({ title: '错误', desc: '系统异常' });
                return;
            }
        })
    }


}
</script>

<style lang='less'>
@import './wfaswfproxy-data-view.less';
</style>