<template>
  <app-layout viewName="emeqsparegridexpview" viewTitle="备件包表格导航视图" :isShowToolbar="false" :className="{ 'view-container': true, 'default-mode-view': true, 'degridexpview': true, 'emeqspare-grid-exp-view': true }" layoutMode="VIEW" :isShowUserInfo="isDefaultView()" :openMode="openMode" @close-view="closeView($event)">
    <template slot="headerLeft">
      <div class="view-header-left">

        <div class="view-caption" v-if="isDefaultView()">{{$t(model.srfCaption)}}</div>
        <modal-breadcrumb v-if="isModalView()"/>
      </div>
    </template>
    <template slot="content">
      <div class="view-content-wrapper">
                    <view_gridexpbar 
            :viewState="viewState"  
            :viewparams="viewparams" 
            :context="context" 
            :showBusyIndicator="true" 
            :viewUID="viewUID"
            :newdata="newdata"
            :opendata="opendata"
            name="gridexpbar"  
            ref='gridexpbar' 
            @selectionchange="gridexpbar_selectionchange($event)"  
            @activated="gridexpbar_activated($event)"  
            @load="gridexpbar_load($event)"  
            @closeview="closeView($event)">
        </view_gridexpbar>

      </div>
    </template>
  </app-layout>

</template>

<script lang='tsx'>
import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { UIActionTool, Util } from '@/utils';
import { VueLifeCycleProcessing, GridExpViewBase } from '@/crm-core';
import EMEQSpareService from '@/service/emeqspare/emeqspare-service';

import GridExpViewEngine from '@engine/view/grid-exp-view-engine';


/**
 * 备件包表格导航视图基类
 *
 * @export
 * @class EMEQSpareGridExpViewBase
 * @extends {GridExpViewBase}
 */
@Component({})
@VueLifeCycleProcessing
export default class EMEQSpareGridExpViewBase extends GridExpViewBase {

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


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

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof EMEQSpareGridExpViewBase
	 */
	public viewtag: string = 'a08baab304950ada849bf8d372da667d';

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof EMEQSpareGridExpViewBase
     */
    protected srfparentdata: any = {};

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

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

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

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof EMEQSpareGridExpViewBase
     */
    public containerModel: any = {
        view_gridexpbar: { name: 'gridexpbar', type: 'GRIDEXPBAR' },
    };

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



    /**
     * 视图引擎
     *
     * @public
     * @type {Engine}
     * @memberof EMEQSpareGridExpViewBase
     */
    public engine: GridExpViewEngine = new GridExpViewEngine();
	

    /**
     * 引擎初始化
     *
     * @public
     * @memberof EMEQSpareGridExpViewBase
     */
    public engineInit(): void {
        this.engine.init({
            view: this,
            gridexpbar: this.$refs.gridexpbar,
            keyPSDEField: 'emeqspare',
            majorPSDEField: 'emeqsparename',
            isLoadDefault: true,
        });
    }

    

    /**
     * gridexpbar 部件 selectionchange 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof EMEQSpareGridExpViewBase
     */
    public gridexpbar_selectionchange($event: any, $event2?: any) {
        this.engine.onCtrlEvent('gridexpbar', 'selectionchange', $event);
    }


    /**
     * gridexpbar 部件 activated 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof EMEQSpareGridExpViewBase
     */
    public gridexpbar_activated($event: any, $event2?: any) {
        this.engine.onCtrlEvent('gridexpbar', 'activated', $event);
    }


    /**
     * gridexpbar 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof EMEQSpareGridExpViewBase
     */
    public gridexpbar_load($event: any, $event2?: any) {
        this.engine.onCtrlEvent('gridexpbar', 'load', $event);
    }



    /**
     * 打开新建数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [fullargs]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof EMEQSpareGridExpView
     */
    public newdata(args: any[],fullargs?:any[], params?: any, $event?: any, xData?: any) {
        let localContext:any = null;
        let localViewParam:any =null;
        const data: any = {};
        if(args[0].srfsourcekey){
            data.srfsourcekey = args[0].srfsourcekey;
        }
        let tempContext = JSON.parse(JSON.stringify(this.context));
        delete tempContext.emeqspare;
        if(args.length >0){
            Object.assign(tempContext,args[0]);
        }
        const deResParameters: any[] = [];
        const parameters: any[] = [
            { pathName: 'emeqspares', parameterName: 'emeqspare' },
        ];
        const _this: any = this;
        const openPopupModal = (view: any, data: any) => {
            let container: Subject<any> = this.$appmodal.openModal(view, tempContext, data);
            container.subscribe((result: any) => {
                if (!result || !Object.is(result.ret, 'OK')) {
                    return;
                }
                if (!xData || !(xData.refresh instanceof Function)) {
                    return;
                }
                xData.refresh(result.datas);
            });
        }
        const view: any = {
            viewname: 'emeqspareedit-view-edit-mode', 
            height: 800, 
            width: 1600,  
            title: this.$t('entities.emeqspare.views.editview_editmode.title'),
        };
        openPopupModal(view, data);
    }


    /**
     * 打开编辑数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [fullargs]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof EMEQSpareGridExpView
     */
    public opendata(args: any[],fullargs?:any[],params?: any, $event?: any, xData?: any) {
        let localContext:any = null;
        let localViewParam:any =null;
        const data: any = {};
        let tempContext = JSON.parse(JSON.stringify(this.context));
        if(args.length >0){
            Object.assign(tempContext,args[0]);
        }
        const deResParameters: any[] = [];
        const parameters: any[] = [
            { pathName: 'emeqspares', parameterName: 'emeqspare' },
        ];
        const _this: any = this;
        const openPopupModal = (view: any, data: any) => {
            let container: Subject<any> = this.$appmodal.openModal(view, tempContext, data);
            container.subscribe((result: any) => {
                if (!result || !Object.is(result.ret, 'OK')) {
                    return;
                }
                if (!xData || !(xData.refresh instanceof Function)) {
                    return;
                }
                xData.refresh(result.datas);
            });
        }
        const view: any = {
            viewname: 'emeqspareedit-view-edit-mode', 
            height: 800, 
            width: 1600,  
            title: this.$t('entities.emeqspare.views.editview_editmode.title'),
        };
        openPopupModal(view, data);
    }




    /**
     * 销毁视图回调
     *
     * @memberof EMEQSpareGridExpViewBase
     */
    public destroyed(){
        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 {string}
     * @memberof EMEQSpareGridExpViewBase
     */
    public viewUID: string = 'eam-core-emeqspare-grid-exp-view';

}
</script>

<style lang='less'>
@import './emeqspare-grid-exp-view.less';
</style>