<template>
<#if ctrl.render??>
    ${ctrl.render.code}
<#else>
    <split id="${ctrl.getCodeName()?lower_case}" class='app-tree-exp-bar' v-model="split" mode="horizontal" @on-move-end="onSplitChange">
        <div slot='left'>
<#if ctrl.isShowTitleBar()>
            <div class='tree-exp-bar-header'>
                <div class="tree-exp-bar-title"><#if ctrl.getTitle() == ''>{{ $t('app.treeExpBar.title') }}<#else>${ctrl.getTitle()}</#if></div>
                <#if ctrl.getPSControls()??>
                <#assign controls = ctrl.getPSControls()/>
                <#list controls as singleControl>
                <#if singleControl.getControlType() == "TOOLBAR">
                ${P.getCtrlCode(singleControl, 'CONTROL.html').code}
                </#if>
                </#list>
                </#if>
            </div>
</#if>
<#if ctrl.isEnableSearch()>
            <div class='search-content'>
                <i-input :search="true"
                    @on-change="($event) => { this.srfnodefilter = $event.target.value; }"
                    @on-search="onSearch">
                </i-input>
            </div>
</#if>
            <div class='<#if ctrl.isShowTitleBar()>tree-exp-content<#else>tree-exp-content2</#if>'>
                <#if ctrl.getPSDETree()??><#t>
                <#assign tree = ctrl.getPSDETree()/>
                <@ibizindent blank=12>
                ${P.getCtrlCode(tree, 'CONTROL.html').code}
                </@ibizindent>
                </#if>
            </div>
        </div>
        <div slot='right'>
            <component 
              v-if="selection.view && !Object.is(this.selection.view.viewname, '')" 
              :is="selection.view.viewname"
              class="viewcontainer2"
              :viewDefaultUsage="false"
              :viewdata="JSON.stringify(selection.context)"
              :viewparam="JSON.stringify(selection.viewparam)"
              @viewdataschange="onViewDatasChange"
              @viewdatasactivated="viewDatasActivated"
              @viewload="onViewLoad">
            </component>
        </div>
    </split>
</#if>
</template>
<#ibizinclude>
../@MACRO/CONTROL/CONTROL_HEADER-BASE.vue.ftl
</#ibizinclude>

<#if view.getPSAppViewLogics?? && view.getPSAppViewLogics()??>
  <#list view.getPSAppViewLogics() as logic>
    <#if logic.getPFLogicCodeType() == 'APP_NEWDATA'>
    /**
     * 打开新建数据视图
     *
     * @type {any}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    @Prop() public newdata: any;
    </#if>
    <#if logic.getPFLogicCodeType() == 'APP_OPENDATA'>
    /**
     * 打开编辑数据视图
     *
     * @type {any}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    @Prop() public opendata: any;
    </#if>
  </#list>
</#if>

    /**
     * 视图唯一标识
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    @Prop() public viewUID!:string;

    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public getDatas(): any[] {
        return [];
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public getData(): any {
        return {};
    }

    /**
     * 选中数据
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public selection: any = {};

    /**
     * 控件宽度
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public ctrlWidth:number = <#if ctrl.getWidth()??>${ctrl.getWidth()?c}<#else>0</#if>;

    /**
     * 过滤值
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public srfnodefilter: string = '';

    /**
     * 刷新标识
     *
     * @public
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public counter:number = 0;

    /**
     * 是否加载默认关联视图
     *
     * @public
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public istLoadDefaultRefView: boolean = false;

    /**
     * 分割宽度
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public split: number = 0.2;

    /**
     * split值变化事件
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public onSplitChange() {
        if(this.split){
          this.$store.commit("setViewSplit",{viewUID:this.viewUID,viewSplit:this.split});
        }
    }

    /**
     * 获取关系项视图
     *
     * @param {*} [arg={}]
     * @returns {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public getExpItemView(arg: any = {}): any {
        let expmode = arg.nodetype.toUpperCase();
        if (!expmode) {
            expmode = '';
        }
        <#list ctrl.getPSAppViewRefs() as item>
        <#if (item.getName()?index_of("EXPITEM:")==0)>
        <#assign refview = item.getRefPSAppView()>
        if (Object.is(expmode, '${item.getName()?substring(8)?upper_case}')) {
            return {  
                viewname: '${srffilepath2(refview.codeName)}', 
                parentdata: <#if item.getParentDataJO()??>${item.getParentDataJO()},<#else>{},</#if>
          <#if refview.getPSAppDataEntity?? && refview.getPSAppDataEntity()??>
                deKeyField:'${refview.getPSAppDataEntity().codeName?lower_case}'
          </#if>      
			};
        }
        </#if>
        </#list>
        return null;
    }

    /**
     * 树导航选中
     *
     * @param {any []} args
     * @param {string} [tag]
     * @param {*} [$event2]
     * @returns {void}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public ${ctrl.name}_selectionchange(args: any [], tag?: string, $event2?: any): void {
        if (args.length === 0) {
            return ;
        }
        const arg:any = args[0];
        if (!arg.id) {
            return;
        }
        const nodetype = arg.id.split(';')[0];
        const refview = this.getExpItemView({ nodetype: nodetype });
        if (!refview) {
            return;
        }
        let tempViewparam:any = {};
        let tempContext:any ={};
        if(arg && arg.navfilter){
            this.counter += 1;
            Object.defineProperty(tempViewparam, arg.navfilter, {
                value : arg.srfkey,
                writable : true,
                enumerable : true,
                configurable : true
            })
            Object.assign(tempContext,{srfcounter:this.counter});
        }
        Object.assign(tempContext,JSON.parse(JSON.stringify(this.context)));
        if(arg.srfappctx){
            Object.assign(tempContext,JSON.parse(JSON.stringify(arg.srfappctx)));
        }
        // 计算导航上下文
        if(arg && arg.navigateContext && Object.keys(arg.navigateContext).length >0){
            let tempData:any = arg.curData?JSON.parse(JSON.stringify(arg.curData)):{};
            Object.assign(tempData,arg);
            let _context = this.$util.computedNavData(tempData,tempContext,tempViewparam,arg.navigateContext);
            Object.assign(tempContext,_context);
        }
        if(arg.srfparentdename){
            Object.assign(tempContext,{srfparentdename:arg.srfparentdename});
        }
        if(arg.srfparentkey){
            Object.assign(tempContext,{srfparentkey:arg.srfparentkey});
        }
        // 计算导航参数
        if(arg && arg.navigateParams && Object.keys(arg.navigateParams).length >0){
            let tempData:any = arg.curData?JSON.parse(JSON.stringify(arg.curData)):{};
            Object.assign(tempData,arg);
            let _params = this.$util.computedNavData(tempData,tempContext,tempViewparam,arg.navigateParams);
            Object.assign(tempViewparam,_params);
            this.counter += 1;
            Object.assign(tempContext,{srfcounter:this.counter});
        }
        this.selection = {};
        Object.assign(this.selection, { view: { viewname: refview.viewname } });
        Object.assign(this.selection,{'viewparam':tempViewparam,'context':tempContext});
        this.$emit('selectionchange',args);
        this.$forceUpdate();
    }

    /**
     * 树加载完成
     *
     * @param {any[]} args
     * @param {string} [tag]
     * @param {*} [$event2]
     * @returns {void}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public ${ctrl.name}_load(args: any[], tag?: string, $event2?: any): void {
        this.$emit('load',args);
    }

    /**
     * 执行搜索
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public onSearch(): void {
        if (!this.viewState) {
            return;
        }
        <#if ctrl.getPSDETree()??>
        <#assign tree = ctrl.getPSDETree()/>
        this.istLoadDefaultRefView = false;
        this.viewState.next({ tag: '${tree.name}', action: 'filter', data: { srfnodefilter: this.srfnodefilter } });
        </#if>
    }

    /**
     * vue 声明周期
     *
     * @memberof @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public created() {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof ${srfclassname('${ctrl.codeName}')}Base
     */    
    public afterCreated(){
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                <#if ctrl.getPSDETree()??>
                <#assign tree = ctrl.getPSDETree()/>
                this.istLoadDefaultRefView = false;
                this.viewState.next({ tag: '${tree.name}', action: action, data: data });
                </#if>
            });
        }
    }

    /**
    * Vue声明周期(组件渲染完毕)
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public mounted() {
        this.afterMounted();     
    }

    /**
    * 执行mounted后的逻辑
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public afterMounted(){ 
        if(this.$store.getters.getViewSplit(this.viewUID)){
            this.split = this.$store.getters.getViewSplit(this.viewUID);
        }else{
            let containerWidth:number = (document.getElementById("${ctrl.getCodeName()?lower_case}") as any).offsetWidth;
            if(this.ctrlWidth){
                    this.split = this.ctrlWidth/containerWidth;
            }
            this.$store.commit("setViewSplit",{viewUID:this.viewUID,viewSplit:this.split}); 
        }  
    }


    /**
     * vue 生命周期
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public destroyed() {
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public afterDestroy() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
        <#if destroyed_block??>
        ${destroyed_block}
        </#if>
    }

    /**
     * 视图数据变化
     *
     * @param {*} $event
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public onViewDatasChange($event: any): void {
        this.$emit('selectionchange', $event);
    }

    /**
     * 视图数据被激活
     *
     * @param {*} $event
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public viewDatasActivated($event: any): void {
        this.$emit('activated', $event);
    }

    /**
     * 视图数据加载完成
     *
     * @param {*} $event
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public onViewLoad($event: any): void {
        this.$emit('load', $event);
    }
    
    <#if ctrl.getPSControls()??>
    <#assign controls = ctrl.getPSControls()/>
    <#list controls as singleControl>
    <#if singleControl.getControlType() == "TOOLBAR">

    ${P.getCtrlCode(singleControl, 'CONTROL.vue').code}
    </#if>
    </#list>
    </#if>
<#ibizinclude>
../@MACRO/CONTROL/CONTROL_BOTTOM-BASE.vue.ftl
</#ibizinclude>

<#ibizinclude>
../@MACRO/CONTROL/CONTROL-BASE.style.ftl
</#ibizinclude>