<template>
    <split id="calendarexpviewcalendarexpbar" class="app-calendar-exp-bar " v-model="split" mode="horizontal" @on-move-end="onSplitChange">
        <div slot='left'>
           <div class="container-header">
           </div>
            <div class='calendar-exp-bar-content2'>
                <view_calendarexpbar_calendar 
    :viewState="viewState"  
    :viewparams="viewparams" 
    :context="context" 
    :showBusyIndicator="true"
    :isSelectFirstDefault="true"
    :newdata="newdata"
    :opendata="opendata"
    name="calendarexpbar_calendar"  
    ref='calendarexpbar_calendar' 
    @selectionchange="calendarexpbar_calendar_selectionchange($event)"  
    @load="calendarexpbar_calendar_load($event)"  
    @closeview="closeView($event)">
</view_calendarexpbar_calendar>
            </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)">
            </component>
        </div>
    </split>
</template>


<script lang='tsx'>
import { Vue, Component, Prop, Provide, Emit, Watch, Model,Inject } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ControlInterface } from '@/interface/control';
import { UIActionTool,Util } from '@/utils';
import NavDataService from '@/service/app/navdata-service';
import AppCenterService from "@service/app/app-center-service";
import EMWOService from '@/service/emwo/emwo-service';
import CalendarExpViewcalendarexpbarService from './calendar-exp-viewcalendarexpbar-calendarexpbar-service';



@Component({
    components: {
      
    }
})
export default class CalendarExpViewcalendarexpbarBase extends Vue implements ControlInterface {

    /**
     * 名称
     *
     * @type {string}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public name?: string;

    /**
     * 视图通讯对象
     *
     * @type {Subject<ViewState>}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public viewState!: Subject<ViewState>;

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public context: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public viewparams: any;

    /**
     * 视图状态事件
     *
     * @public
     * @type {(Subscription | undefined)}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public viewStateEvent: Subscription | undefined;

    /**
     * 获取部件类型
     *
     * @returns {string}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public getControlType(): string {
        return 'CALENDAREXPBAR'
    }



    /**
     * 计数器服务对象集合
     *
     * @type {Array<*>}
     * @memberof CalendarExpViewcalendarexpbarBase
     */    
    public counterServiceArray:Array<any> = [];

    /**
     * 建构部件服务对象
     *
     * @type {CalendarExpViewcalendarexpbarService}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public service: CalendarExpViewcalendarexpbarService = new CalendarExpViewcalendarexpbarService({ $store: this.$store });

    /**
     * 实体服务对象
     *
     * @type {EMWOService}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public appEntityService: EMWOService = new EMWOService({ $store: this.$store });

    /**
     * calendarexpbar_calendar 部件 selectionchange 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public calendarexpbar_calendar_selectionchange($event: any, $event2?: any) {
        this.calendarexpbar_selectionchange($event, 'calendarexpbar_calendar', $event2);
    }

    /**
     * calendarexpbar_calendar 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public calendarexpbar_calendar_load($event: any, $event2?: any) {
        this.calendarexpbar_load($event, 'calendarexpbar_calendar', $event2);
    }
    


    /**
     * 关闭视图
     *
     * @param {any} args
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public closeView(args: any): void {
        let _this: any = this;
        _this.$emit('closeview', [args]);
    }

    /**
     *  计数器刷新
     *
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    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();
                }
            })
        }
    }



    /**
     * 视图唯一标识
     *
     * @type {boolean}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public viewUID!:string;

    /**
     * 打开新建数据视图
     *
     * @type {any}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public newdata: any;
    /**
     * 打开编辑数据视图
     *
     * @type {any}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    @Prop() public opendata: any;

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

    /**
     * 呈现模式，可选值：horizontal或者vertical
     * 
     * @public
     * @type {(string)}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public showMode:string ="horizontal";

    /**
     * 控件宽度
     *
     * @type {number}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public ctrlWidth:number = 800;

    /**
     * 控件高度
     *
     * @type {number}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public ctrlHeight: number = 0;

    /**
     * 搜素值
     * 
     * @public
     * @type {(string)}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public searchText:string = "";

    /**
     * 分割宽度
     *
     * @type {number}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public split: number = 0.5;


    /**
     * 导航视图名称
     *
     * @type {string}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public navViewName: any = {
        EMWO_INNER: "emwo-innerwoview-edit-view",
        EMWO_OSC: "emwo-oscwoview-edit-view",
        EMWO_DP: "emwo-dpwoview-edit-view",
        EMWO_EN: "emwo-enwoview-edit-view"
    };

    /**
     * 导航参数
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public navParam: any = {
        EMWO_INNER: {
            navigateContext:null,
            navigateParams:null
        },
        EMWO_OSC: {
            navigateContext:null,
            navigateParams:null
        },
        EMWO_DP: {
            navigateContext:null,
            navigateParams:null
        },
        EMWO_EN: {
            navigateContext:null,
            navigateParams:null
        }
    };

    /**
     * 导航过滤项
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public navFilter: any = {
        EMWO_INNER: "",
        EMWO_OSC: "",
        EMWO_DP: "",
        EMWO_EN: ""
    };

    /**
     * 导航关系
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public navPSDer: any = {
        EMWO_INNER: "",
        EMWO_OSC: "",
        EMWO_DP: "",
        EMWO_EN: ""
    };

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


    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public getDatas(): any[] {
        return [];
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public getData(): any {
        return null;
    }

    /**
     * 选中数据
     *
     * @type {*}
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public selection: any = {};

    /**
     * split值变化事件
     *
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public onSplitChange() {
        if(this.$refs.calendarexpbar_calendar){
            const calendarContainer:any = this.$refs.calendarexpbar_calendar;
            if(calendarContainer.$refs.calendar){
                const appCalendar: any = calendarContainer.$refs.calendar;
                const api = appCalendar.getApi();
                api.updateSize();
            }
        }
        if(this.split){
          this.$store.commit("setViewSplit",{viewUID:this.viewUID,viewSplit:this.split});
        }
    }

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

    /**
    * 执行created后的逻辑
    *
    * @memberof CalendarExpViewcalendarexpbarBase
    */
    public afterCreated(){
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                this.viewState.next({ tag: 'calendarexpbar_calendar', action: action, data: data });
            });
        }  
    }
    
    /**
    * Vue声明周期(组件渲染完毕)
    *
    * @memberof CalendarExpViewcalendarexpbarBase
    */
    public mounted() {
        this.afterMounted();     
    }

    /**
    * 执行mounted后的逻辑
    *
    * @memberof CalendarExpViewcalendarexpbarBase
    */
    public afterMounted(){ 
        if(this.$store.getters.getViewSplit(this.viewUID)){
            this.split = this.$store.getters.getViewSplit(this.viewUID);
        }else{
            let containerWidth:number = (document.getElementById("calendarexpviewcalendarexpbar") as any).offsetWidth;
            let containerHeight:number = (document.getElementById("calendarexpviewcalendarexpbar") as any).offsetHeight;
            if(Object.is(this.showMode,'horizontal')){
                if(this.ctrlWidth){
                    this.split = this.ctrlWidth/containerWidth;
                }
            }else{
                if(this.ctrlHeight){
                    this.split = this.ctrlHeight/containerHeight;
                }
            }
            this.$store.commit("setViewSplit",{viewUID:this.viewUID,viewSplit:this.split}); 
        }  
    }
 
    /**
     * vue 生命周期
     *
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public destroyed() {
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public afterDestroy() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
    }
    
    /**
     * calendarexpbar的选中数据事件
     * 
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public calendarexpbar_selectionchange(args: any [], tag?: string, $event2?: any): void {
        let tempContext:any = {};
        let tempViewParam:any = {};
        if (args.length === 0) {
            return ;
        }
        const arg:any = args[0];
        if(this.context){
            Object.assign(tempContext,JSON.parse(JSON.stringify(this.context)));
        }
        switch(arg.itemType) {
            case "EMWO_INNER":
                Object.assign(tempContext,{ emwo_inner : arg.emwo_inner});
                Object.assign(tempContext,{srfparentdename:'EMWO_INNER',srfparentkey:arg['emwo_inner']});
                if(this.navFilter && this.navFilter['EMWO_INNER'] && !Object.is(this.navFilter['EMWO_INNER'],"")){
                    Object.assign(tempViewParam,{[this.navFilter['EMWO_INNER']]:arg['emwo_inner']});
                }
                if(this.navPSDer && this.navFilter['EMWO_INNER'] && !Object.is(this.navPSDer['EMWO_INNER'],"")){
                    Object.assign(tempViewParam,{[this.navPSDer['EMWO_INNER']]:arg['emwo_inner']});
                }
                if(this.navParam && this.navParam['EMWO_INNER'] && this.navParam['EMWO_INNER'].navigateContext && Object.keys(this.navParam['EMWO_INNER'].navigateContext).length >0){
                    let _context:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_INNER'].navigateContext);
                    Object.assign(tempContext,_context);
                }
                if(this.navParam && this.navParam['EMWO_INNER'] && this.navParam['EMWO_INNER'].navigateParams && Object.keys(this.navParam['EMWO_INNER'].navigateParams).length >0){
                    let _params:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_INNER'].navigateParams);
                    Object.assign(tempViewParam,_params);
                }
                break;
            case "EMWO_OSC":
                Object.assign(tempContext,{ emwo_osc : arg.emwo_osc});
                Object.assign(tempContext,{srfparentdename:'EMWO_OSC',srfparentkey:arg['emwo_osc']});
                if(this.navFilter && this.navFilter['EMWO_OSC'] && !Object.is(this.navFilter['EMWO_OSC'],"")){
                    Object.assign(tempViewParam,{[this.navFilter['EMWO_OSC']]:arg['emwo_osc']});
                }
                if(this.navPSDer && this.navFilter['EMWO_OSC'] && !Object.is(this.navPSDer['EMWO_OSC'],"")){
                    Object.assign(tempViewParam,{[this.navPSDer['EMWO_OSC']]:arg['emwo_osc']});
                }
                if(this.navParam && this.navParam['EMWO_OSC'] && this.navParam['EMWO_OSC'].navigateContext && Object.keys(this.navParam['EMWO_OSC'].navigateContext).length >0){
                    let _context:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_OSC'].navigateContext);
                    Object.assign(tempContext,_context);
                }
                if(this.navParam && this.navParam['EMWO_OSC'] && this.navParam['EMWO_OSC'].navigateParams && Object.keys(this.navParam['EMWO_OSC'].navigateParams).length >0){
                    let _params:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_OSC'].navigateParams);
                    Object.assign(tempViewParam,_params);
                }
                break;
            case "EMWO_DP":
                Object.assign(tempContext,{ emwo_dp : arg.emwo_dp});
                Object.assign(tempContext,{srfparentdename:'EMWO_DP',srfparentkey:arg['emwo_dp']});
                if(this.navFilter && this.navFilter['EMWO_DP'] && !Object.is(this.navFilter['EMWO_DP'],"")){
                    Object.assign(tempViewParam,{[this.navFilter['EMWO_DP']]:arg['emwo_dp']});
                }
                if(this.navPSDer && this.navFilter['EMWO_DP'] && !Object.is(this.navPSDer['EMWO_DP'],"")){
                    Object.assign(tempViewParam,{[this.navPSDer['EMWO_DP']]:arg['emwo_dp']});
                }
                if(this.navParam && this.navParam['EMWO_DP'] && this.navParam['EMWO_DP'].navigateContext && Object.keys(this.navParam['EMWO_DP'].navigateContext).length >0){
                    let _context:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_DP'].navigateContext);
                    Object.assign(tempContext,_context);
                }
                if(this.navParam && this.navParam['EMWO_DP'] && this.navParam['EMWO_DP'].navigateParams && Object.keys(this.navParam['EMWO_DP'].navigateParams).length >0){
                    let _params:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_DP'].navigateParams);
                    Object.assign(tempViewParam,_params);
                }
                break;
            case "EMWO_EN":
                Object.assign(tempContext,{ emwo_en : arg.emwo_en});
                Object.assign(tempContext,{srfparentdename:'EMWO_EN',srfparentkey:arg['emwo_en']});
                if(this.navFilter && this.navFilter['EMWO_EN'] && !Object.is(this.navFilter['EMWO_EN'],"")){
                    Object.assign(tempViewParam,{[this.navFilter['EMWO_EN']]:arg['emwo_en']});
                }
                if(this.navPSDer && this.navFilter['EMWO_EN'] && !Object.is(this.navPSDer['EMWO_EN'],"")){
                    Object.assign(tempViewParam,{[this.navPSDer['EMWO_EN']]:arg['emwo_en']});
                }
                if(this.navParam && this.navParam['EMWO_EN'] && this.navParam['EMWO_EN'].navigateContext && Object.keys(this.navParam['EMWO_EN'].navigateContext).length >0){
                    let _context:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_EN'].navigateContext);
                    Object.assign(tempContext,_context);
                }
                if(this.navParam && this.navParam['EMWO_EN'] && this.navParam['EMWO_EN'].navigateParams && Object.keys(this.navParam['EMWO_EN'].navigateParams).length >0){
                    let _params:any = this.$util.computedNavData(arg,tempContext,tempViewParam,this.navParam['EMWO_EN'].navigateParams);
                    Object.assign(tempViewParam,_params);
                }
                break;
        }
        this.selection = {};
        Object.assign(this.selection, { view: { viewname: this.navViewName[arg.itemType] }, context:tempContext,viewparam:tempViewParam });
        this.$emit('selectionchange',args);
        this.$forceUpdate();
    }

    /**
     * calendarexpbar的load完成事件
     * 
     * @memberof CalendarExpViewcalendarexpbarBase
     */
    public calendarexpbar_load(args:any, tag?: string, $event2?: any){
        this.$emit('load',args);
    }

    /**
    * 执行搜索
    *
    * @memberof CalendarExpViewcalendarexpbarBase
    */
    public onSearch($event:any) {
        let calendar:any = this.$refs.calendarexpbar_calendar;
        calendar.searchEvents({ query: this.searchText });
    }

}
</script>

<style lang='less'>
@import './calendar-exp-viewcalendarexpbar-calendarexpbar.less';
</style>