<template>
<div class="view-container deindexpickupdataview ibizhardware-index-pickup-data-view">
    <app-studioaction :viewTitle="$t(model.srfCaption)" viewName="ibizhardwareindexpickupdataview"></app-studioaction>
    <card class='view-card view-no-caption  view-no-toolbar' :disHover="true" :padding="0" :bordered="false">
        <div class="content-container">
                    <view_dataview 
                :viewState="viewState"  
                :viewparams="viewparams" 
                :context="context" 
                :isSingleSelect="isSingleSelect" 
                createAction="Create"
                removeAction="Remove"
                updateAction="Update"
                fetchAction="FetchIndexDER"
                :showBusyIndicator="true" 
                @selectionchange = "dataviewDataSeletionchange"
                name="dataview"  
                ref='dataview' 
                @closeview="closeView($event)">
            </view_dataview>
        </div>
    </card>
</div>
</template>

<script lang='tsx'>
import { Vue, Component, Prop, Provide, Emit, Watch,Inject } from 'vue-property-decorator';
import { UIActionTool,Util } from '@/utils';
import NavDataService from '@/service/app/navdata-service';
import { Subject,Subscription } from 'rxjs';
import IBIZHardwareService from '@/service/ibizhardware/ibizhardware-service';
import IBIZHardwareAuthService from '@/authservice/ibizhardware/ibizhardware-auth-service';


import IBIZHardwareUIService from '@/uiservice/ibizhardware/ibizhardware-ui-service';


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

    /**
     * 实体服务对象
     *
     * @type {IBIZHardwareService}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public appEntityService: IBIZHardwareService = new IBIZHardwareService;

    /**
     * 实体UI服务对象
     *
     * @type IBIZHardwareUIService
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public appUIService: IBIZHardwareUIService = new IBIZHardwareUIService();
    
    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    @Emit() 
    public viewDatasChange(val: any):any {
        return val;
    }

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

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

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

    /**
     * 视图默认使用
     *
     * @type {string}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    @Inject({from:'navModel',default: 'tab'})
    public navModel!:string;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof IBIZHardwareIndexPickupDataViewBase
	 */
	public viewtag: string = '0173689599376f748203557242d223ed';

    /**
	 * 视图类型
	 *
	 * @type {string}
	 * @memberof IBIZHardwareIndexPickupDataViewBase
	 */
	public viewType: string = 'DEINDEXPICKUPDATAVIEW';

	/**
	 * 自定义视图导航上下文集合
	 *
	 * @type {*}
	 * @memberof IBIZHardwareIndexPickupDataViewBase
	 */
    public customViewNavContexts:any ={
    };

	/**
	 * 自定义视图导航参数集合
	 *
	 * @type {*}
	 * @memberof IBIZHardwareIndexPickupDataViewBase
	 */
    public customViewParams:any ={
    };

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public model: any = {
        srfCaption: 'entities.ibizhardware.views.indexpickupdataview.caption',
        srfTitle: 'entities.ibizhardware.views.indexpickupdataview.title',
        srfSubTitle: 'entities.ibizhardware.views.indexpickupdataview.subtitle',
        dataInfo: ''
    }

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

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

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public containerModel: any = {
        view_dataview: { name: 'dataview', type: 'DATAVIEW' },
    };

    /**
     * 视图操作参数
     *
     * @type {*}
     * @readonly
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    get viewCtx() {
        return {
            appGlobal: this.$store.getters.getAppGlobal(),
            viewGlobal: {},
            viewNavData: {},
            viewNavContext: this.context,
            viewNavParam: this.viewparams,
            messageBox: this.$msgbox,
            view: this,
        }
    }

    /**
     * 视图刷新
     *
     * @param {*} args
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public refresh(args?: any): void {
        const refs: any = this.$refs;
        if (refs && refs.dataview) {
            refs.dataview.refresh();
        }
    }
    /**
     *  计数器刷新
     *
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    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();
                }
            })
        }
    }

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



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

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

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

    /**
     * 解析视图参数
     *
     * @public
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public 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) => {
            if(matchArray[index + 1]){
                Object.defineProperty(tempValue, item.name, {
                    enumerable: true,
                    value: decodeURIComponent(matchArray[index + 1])
                });
            }
        });
        this.$viewTool.formatRouteParams(tempValue,this.$route,this.context,this.viewparams);
        //初始化视图唯一标识
        Object.assign(this.context,{srfsessionid:this.$util.createUUID()});
    }

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

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

    /**
     * 销毁之前
     *
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
        let _this: any = this
        if (_this.serviceStateEvent) {
            _this.serviceStateEvent.unsubscribe();
        }
        if (_this.portletStateEvent) {
            _this.portletStateEvent.unsubscribe();
        }
        if (_this.engine) {
            _this.engine.destroy();
        }
    }

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

    /**
     * 执行mounted后的逻辑
     * 
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public afterMounted(){

        this.viewState.next({tag: 'dataview', action: 'load', data: {}});
        
    }




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

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

    /**
     * 执行destroyed后的逻辑
     * 
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public 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);
                }
                })
            }
        }
    }

    /**
     * 是否单选
     *
     * @type {boolean}
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public isSingleSelect: boolean = true;

    /**
     * 数据选中抛值
     *
     * @memberof IBIZHardwareIndexPickupDataViewBase
     */
    public dataviewDataSeletionchange(args:any){
        this.$emit('viewdataschange', [...args]);
    }



}
</script>