<template>
    <div class = "drtab">
        <tabs :animated='false' class='app-dr-tab' name='name' @on-click="tabPanelClick">
                  <tab-pane v-if="isShowSlot" :index='0' name='form' tab='name' class='' :disabled='items[0].disabled'
                label='业务协作'>
                <div class='main-data'>
                    <slot></slot>
                </div>
            </tab-pane>
            </tabs>
    </div>
    
</template>
<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch, Model } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ControlInterface } from '@/interface/control';
import { UIActionTool,Util } from '@/utils';
import BCService from '@/service/bc/bc-service';
import DefaultService from './default-drtab-service';



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

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

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

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

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

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

    /**
     * 获取部件类型
     *
     * @returns {string}
     * @memberof Default
     */
    protected getControlType(): string {
        return 'DRTAB'
    }



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

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

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


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

    /**
     *  计数器刷新
     *
     * @memberof Default
     */
    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 {string}
     * @memberof DefaultBase
     */
    @Prop({default:true}) protected isShowSlot?: boolean;

    /**
     *  应用实体参数名称
     *
     * @type string
     * @memberof DefaultBase
     */
    @Prop() protected parentName!: string;
    
    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof DefaultBase
     */
    public getDatas(): any[] {
        return this.items;
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof DefaultBase
     */
    public getData(): any {
        return this.selection;
    }

    /**
     * 数据选中项
     *
     * @type {*}
     * @memberof DefaultBase
     */
    protected selection: any = {};

    /**
     * 父数据
     *
     * @private
     * @type {*}
     * @memberof DefaultBase
     */
    private parentData: any = {};

    /**
     * 关系栏数据项
     *
     * @type {any[]}
     * @memberof DefaultBase
     */
    protected items: any[] = [
        {
            index: 0, 
            id: 'form',
            name: 'form', 
            text: '业务协作', 
            disabled: false, 
        },
    ];

    /**
     * 生命周期
     *
     * @memberof DefaultBase
     */
    protected created(): void {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof DefaultBase
     */    
    protected afterCreated(){
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                if (Object.is('state', action)) {
                    const state = !this.context.bc ? true : false;
                    this.setItemDisabled(state);
                }
            });
        }
        this.$nextTick(() => {
            this.$emit('selectionchange', [this.items[0]]);
        });
    }

    /**
     * vue 生命周期
     *
     * @memberof DefaultBase
     */
    protected destroyed() {
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof DefaultBase
     */
    protected afterDestroy() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
    }

    /**
     * 获取关系项
     *
     * @private
     * @param {*} [arg={}]
     * @returns {*}
     * @memberof DefaultBase
     */
    private getDRTabItem(arg: any = {}): any {
        let expmode = arg.nodetype.toUpperCase();
        if (!expmode) {
            expmode = '';
        }
        return undefined;
    }

    /**
     * 设置关系项状态
     *
     * @param {boolean} state
     * @memberof DefaultBase
     */
    protected setItemDisabled(state: boolean): void {
        this.items.forEach((item: any) => {
            if (Object.is(item.name, 'form')) {
                item.disabled = false;
            } else {
                item.disabled = state;
            }
        });
    }

    /**
     * 获取数据项
     *
     * @private
     * @param {string} id
     * @returns {*}
     * @memberof DefaultBase
     */
    private getItem(id: string): any {
        const arr: any[] = this.items.filter((_item: any) => Object.is(_item.id, id));
        if (arr) {
            return arr[0];
        }
        return null;
    }

    /**
     * 选中节点
     *
     * @param {*} $event
     * @memberof DefaultBase
     */
    protected tabPanelClick($event: any): void {
        const item = this.getItem($event);
        if (Object.is(item.id, this.selection.id)) {
            return;
        }
        this.$emit('selectionchange', [item]);

        const refview = this.getDRTabItem({ nodetype: item.id });
        this.selection = {};
        const _context: any = { ...JSON.parse(JSON.stringify(this.context)) };
        Object.assign(_context,{srfparentdename:this.parentName,srfparentkey:_context[this.parentName.toLowerCase()]});
        const _params: any = { ...JSON.parse(JSON.stringify(this.viewparams)) };
        if (refview && refview.parentdatajo) {
            Object.assign(_context, refview.parentdatajo);
            Object.assign(this.selection, { view: { viewname: refview.viewname }, data: _context, param: _params });
        }
        Object.assign(this.selection, item);
    }

}
</script>

<style lang='less'>
@import './default-drtab.less';
</style>