<template>
<#if ctrl.render??>
    ${ctrl.render.code}
<#else>
    <div id="${ctrl.getCodeName()?lower_case}" class="app-grid-exp-bar<#if ctrl.getPSSysCss()??><#assign singleCss = ctrl.getPSSysCss()> ${singleCss.getCssName()}</#if>">
    <#if ctrl.isShowTitleBar()>
        <div class='grid-exp-bar-header'>
            <div class="grid-exp-bar-title">
                <icon type='ios-home-outline'/><#if ctrl.getTitle() == ''>{{ $t('app.gridBar.title') }}<#else>${ctrl.getTitle()}</#if>
            </div>
        </div>
    </#if>
        <div class="exp-bar-action-container">
    <#if view.isEnableQuickGroup()>
        <#if view.getPSSysCounterRef?? && view.getPSSysCounterRef()?? && view.getPSSysCounterRef().getPSAppCounter()??>
            <#assign appCounter = view.getPSSysCounterRef().getPSAppCounter() />
        </#if>
            <div class="quick-group-container">
                <app-quick-group :items="quickGroupModel" <#if appCounter??>:counterService="${appCounter.getCodeName()?lower_case}counterservice"</#if> @valuechange="quickGroupValueChange"></app-quick-group>
            </div>
    </#if>
    <#if ctrl.isEnableSearch()>
            <div class='search-container'>
                <i-input
                    v-model="searchText"
                    :search="true"
                    :placeholder="placeholder"
                    @on-search="onSearch">
                </i-input>
            </div>
    </#if>
    <#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>
            <div class='grid-exp-bar-content'>
    <#if ctrl.getPSControls()??>
        <#assign controls = ctrl.getPSControls()/>
        <#list controls as singleControl>
            <#if singleControl.getControlType() == "GRID">
                ${P.getCtrlCode(singleControl, 'CONTROL.html').code}
            </#if>
        </#list>
    </#if>
            </div>
    </div>
</#if>
</template>

<#assign import_block>
import CodeListService from "@/codelist/codelist-service";
<#if view.getPSSysCounterRef?? && view.getPSSysCounterRef()?? && view.getPSSysCounterRef().getPSAppCounter()??>
<#assign appCounter = view.getPSSysCounterRef().getPSAppCounter() />
import  ${srfclassname('${appCounter.getCodeName()}')}CounterService  from '@/counter/${srffilepath2(appCounter.getCodeName())}/${srffilepath2(appCounter.getCodeName())}-counter';
</#if>
</#assign>

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

<#ibizinclude>
../@MACRO/FUNC/MACRO.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 {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    @Prop() public viewUID!:string;

    /**
     * 是否单选
     * 
     * @public
     * @type {(boolean)}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public isSingleSelect:boolean = true;

    <#if view.getPSSysCounterRef?? && view.getPSSysCounterRef?? && view.getPSSysCounterRef()?? && view.getPSSysCounterRef().getPSAppCounter()??>
    <#assign appCounter = view.getPSSysCounterRef().getPSAppCounter() />
    /**
     * ${srfclassname('${appCounter.getCodeName()}')}CounterService计数器服务对象
     *
     * @type {${srfclassname('${appCounter.getCodeName()}')}CounterService}
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public ${appCounter.getCodeName()?lower_case}counterservice: ${srfclassname('${appCounter.getCodeName()}')}CounterService = new ${srfclassname('${appCounter.getCodeName()}')}CounterService({ context:this.context,viewparams:this.viewparams });
    </#if>

    /**
     * 可搜索字段名称
     * 
     * 
     * @type {(string)}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public placeholder = "<@getQuickSearchPlaceholder ctrl />";

    /**
     * 搜素值
     * 
     * @public
     * @type {(string)}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public searchText: string = "";

<#if ctrl.getXDataPSControl()??>
<#assign xDataControl = ctrl.getXDataPSControl() />

    /**
     * 导航视图名称
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public navViewName: string = "<#if xDataControl.getNavPSAppView()??><#assign navPSAppView = xDataControl.getNavPSAppView() />${srffilepath2(navPSAppView.getCodeName())}</#if>";

    /**
     * 导航过滤项
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public navFilter: string = "<#if xDataControl.getNavFilter()??>${xDataControl.getNavFilter()}</#if>";

    /**
     * 导航关系
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public navPSDer: string = "<#if xDataControl.getNavPSDER()??>n_${xDataControl.getNavPSDER().getPSPickupDEField().getCodeName()?lower_case}_eq</#if>";
    
    /**
     * 导航上下文参数
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public navigateContext:any =<#if xDataControl.getPSNavigateContexts?? && xDataControl.getPSNavigateContexts()??><@getNavigateContext xDataControl /><#else>null</#if>;

    /**
     * 导航视图参数
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public navigateParams:any = <#if xDataControl.getPSNavigateParams?? && xDataControl.getPSNavigateParams()??><@getNavigateParams xDataControl /><#else>null</#if>; 
</#if>
<#if view.isEnableQuickGroup()>
    /**
     * 代码表服务对象
     *
     * @type {CodeListService}
     * @memberof ${srfclassname('${view.name}')}Base
     */  
    public codeListService:CodeListService = new CodeListService();
    
    /**
     * 快速分组数据对象
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public quickGroupData:any;

    /**
     * 快速分组是否有抛值
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public isEmitQuickGroupValue:boolean = false;

    /**
     * 快速分组模型
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public quickGroupModel:Array<any> = [];

    /**
     * 加载快速分组模型
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public loadQuickGroupModel(){
        <#if view.getQuickGroupPSCodeList()??>
        <#assign codelist=view.getQuickGroupPSCodeList() />
        let quickGroupCodeList:any = {tag:'${codelist.codeName}',codelistType:'${codelist.getCodeListType()}'};
        if(quickGroupCodeList.tag && Object.is(quickGroupCodeList.codelistType,"STATIC")){
            const codelist = this.$store.getters.getCodeList(quickGroupCodeList.tag);
            if (codelist) {
                this.quickGroupModel = [...this.handleDynamicData(JSON.parse(JSON.stringify(codelist.items)))];
            } else {
                <#noparse>console.log(`----${quickGroupCodeList.tag}----代码表不存在`);</#noparse>
            }
        }else if(quickGroupCodeList.tag && Object.is(quickGroupCodeList.codelistType,"DYNAMIC")){
            this.codeListService.getItems(quickGroupCodeList.tag,{},{}).then((res:any) => {
                this.quickGroupModel = res;
            }).catch((error:any) => {
                <#noparse>console.log(`----${quickGroupCodeList.tag}----代码表不存在`);</#noparse>
            });
        }
        </#if>
    }

    /**
     * 处理快速分组模型动态数据部分(%xxx%)
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public handleDynamicData(inputArray:Array<any>){
        if(inputArray.length >0){
            inputArray.forEach((item:any) =>{
               if(item.data && Object.keys(item.data).length >0){
                   Object.keys(item.data).forEach((name:any) =>{
                        let value: any = item.data[name];
                        if (value && typeof(value)=='string' && value.startsWith('%') && value.endsWith('%')) {
                            const key = (value.substring(1, value.length - 1)).toLowerCase();
                            if (this.context[key]) {
                                value = this.context[key];
                            } else if(this.viewparams[key]){
                                value = this.viewparams[key];
                            }
                        }
                        item.data[name] = value;
                   })
               }
            })
        }
        return inputArray;
    }

    /**
     * 快速分组值变化
     *
     * @memberof ${srfclassname('${view.name}')}Base
     */
    public quickGroupValueChange($event:any){
        if($event && $event.data){
            if(this.quickGroupData) {
                for(let key in this.quickGroupData) {
                    delete this.viewparams[key];
                }
            }
            this.quickGroupData = $event.data;
            Object.assign(this.viewparams, $event.data);
        }else{
            if(this.quickGroupData) {
                for(let key in this.quickGroupData) {
                    delete this.viewparams[key];
                }
            }
        }
        if(this.isEmitQuickGroupValue){
            this.onSearch($event);
        }
        this.isEmitQuickGroupValue = true;
    }
</#if>

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof MOBILEENTITY3Canlen
     */
    @Prop({ default: true }) public showBusyIndicator!: boolean;

    <#if ctrl.getPSControls()??>
    <#assign controls = ctrl.getPSControls()/>
    <#list controls as singleControl>
    <#if singleControl.getControlType() == "TOOLBAR">

    ${P.getCtrlCode(singleControl, 'CONTROL.vue').code}
    </#if>
    </#list>
    </#if>

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

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

    /**
    * Vue声明周期(组件初始化完毕)
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public created() {
        this.afterCreated();     
    }

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

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

    /**
    * 执行mounted后的逻辑
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public afterMounted() { }

    /**
     * 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>
    }

    /**
    * 执行搜索
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public onSearch($event:any) {
        <#if ctrl.getXDataPSControl()??>
        <#assign xDataControl = ctrl.getXDataPSControl()/>
        let grid:any = this.$refs.${xDataControl.getName()};
        grid.load({ query: this.searchText });
        </#if>
        
    }

    /**
    * 刷新
    *
    * @memberof ${srfclassname('${ctrl.codeName}')}Base
    */
    public refresh(args?: any): void {
    <#if ctrl.getXDataPSControl?? && ctrl.getXDataPSControl()??>
    <#assign dataControl = ctrl.getXDataPSControl() />
        const refs: any = this.$refs;
        if (refs && refs.${dataControl.name}) {
            refs.${dataControl.name}.refresh();
        }
    </#if>
    }

    /**
     * ${ctrl.name}的选中数据事件
     * 
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public ${ctrl.name}_selectionchange(args: any [], tag?: string, $event2?: any): void {
        const tempContext: any = {};
        const tempViewParam: any = {};
        if (args.length === 0) {
            this.calcToolbarItemState(true);
            return ;
        }
        const arg: any = args[0];
        if (this.context) {
            Object.assign(tempContext, JSON.parse(JSON.stringify(this.context)));
        }
<#if ctrl.getXDataPSControl()??>
    <#assign xDataControl = ctrl.getXDataPSControl()/>
    <#if xDataControl.getPSAppDataEntity()??>
    <#assign curDataEntity = xDataControl.getPSAppDataEntity()/>
        Object.assign(tempContext, { '${curDataEntity.getCodeName()?lower_case}': arg['${curDataEntity.getCodeName()?lower_case}']});
        Object.assign(tempContext, { srfparentdename: '${curDataEntity.getCodeName()}', srfparentkey:arg['${curDataEntity.getCodeName()?lower_case}']});
        if (this.navFilter && !Object.is(this.navFilter, "")) {
            Object.assign(tempViewParam, { [this.navFilter]: arg['${curDataEntity.getCodeName()?lower_case}']});
        }
        if (this.navPSDer && !Object.is(this.navPSDer,"")) {
            Object.assign(tempViewParam, { [this.navPSDer]: arg['${curDataEntity.getCodeName()?lower_case}']});
        }
    </#if>
</#if>
        if (this.navigateContext && Object.keys(this.navigateContext).length > 0) {
            let _context:any = this.$util.computedNavData(arg, tempContext, tempViewParam, this.navigateContext);
            Object.assign(tempContext, _context);
        }
        if (this.navigateParams && Object.keys(this.navigateParams).length > 0) {
            let _params:any = this.$util.computedNavData(arg, tempContext, tempViewParam, this.navigateParams);
            Object.assign(tempViewParam, _params);
        }
        const navItem: any = {
            navView: this.navViewName,
            data: args,
            srfnavdata: {
                context: tempContext,
                viewparams: tempViewParam
            }
        }
        this.calcToolbarItemState(false);
        this.$emit('selectionchange', navItem);
    }

    /**
     * ${ctrl.name}的load完成事件
     * 
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public ${ctrl.name}_load(args:any, tag?: string, $event2?: any){
        this.calcToolbarItemState(true);
        this.$emit('load',args);
    }
    
    /**
     * 设置导航区工具栏禁用状态
     *
     * @param {boolean} state
     * @return {*} 
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public calcToolbarItemState(state: boolean) {
        let _this: any = this;
        const models:any = _this.${ctrl.getCodeName()?lower_case}_toolbarModels;
        if (models) {
            for (const key in models) {
                if (!models.hasOwnProperty(key)) {
                    return;
                }
                const _item = models[key];
                if (_item.uiaction && (Object.is(_item.uiaction.target, 'SINGLEKEY') || Object.is(_item.uiaction.target, 'MULTIKEY'))) {
                    _item.disabled = state;
                }
                _item.visabled = true;
                if (_item.noprivdisplaymode && _item.noprivdisplaymode === 6) {
                    _item.visabled = false;
                }
            }
            this.calcNavigationToolbarState();
        }
    }   

    /**
     * 计算导航工具栏权限状态
     * 
     * @memberof ${srfclassname('${ctrl.codeName}')}Base
     */
    public calcNavigationToolbarState(){
        let _this: any = this;
        // 界面行为
        <#assign ModelsName>${ctrl.getCodeName()?lower_case}_toolbarModels</#assign>
        if(_this.${ModelsName}){
        <#if ctrl.getPSAppDataEntity?? && ctrl.getPSAppDataEntity()??>
        <#assign curAppEntity = ctrl.getPSAppDataEntity() />
            const curUIService:${srfclassname('${curAppEntity.getCodeName()}')}UIService  = new ${srfclassname('${curAppEntity.getCodeName()}')}UIService();
            ViewTool.calcActionItemAuthState({},_this.${ModelsName},curUIService);
        </#if>
        }
    }
<#ibizinclude>
../@MACRO/CONTROL/CONTROL_BOTTOM-BASE.vue.ftl
</#ibizinclude>

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