import { Prop, Provide, Emit, Model } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
import { Watch, MainControlBase } from '@/studio-core';
import HREmployeeService from '@/service/hremployee/hremployee-service';
import TreeExpViewtreeexpbarService from './tree-exp-viewtreeexpbar-treeexpbar-service';
import HREmployeeUIService from '@/uiservice/hremployee/hremployee-ui-service';


/**
 * treeexpbar部件基类
 *
 * @export
 * @class MainControlBase
 * @extends {TreeExpViewtreeexpbarTreeExpBarBase}
 */
export class TreeExpViewtreeexpbarTreeExpBarBase extends MainControlBase {

    /**
     * 获取部件类型
     *
     * @protected
     * @type {string}
     * @memberof TreeExpViewtreeexpbarTreeExpBarBase
     */
    protected controlType: string = 'TREEEXPBAR';

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

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

    /**
     * 应用实体名称
     *
     * @protected
     * @type {string}
     * @memberof TreeExpViewtreeexpbarTreeExpBarBase
     */
    protected appDeName: string = 'hremployee';

    /**
     * 应用实体中文名称
     *
     * @protected
     * @type {string}
     * @memberof TreeExpViewtreeexpbarTreeExpBarBase
     */
    protected appDeLogicName: string = '员工';

    /**
     * treeexpbar_tree 部件 selectionchange 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TreeExpViewtreeexpbarTreeExpBarBase
     */
    public treeexpbar_tree_selectionchange($event: any, $event2?: any) {
        this.treeexpbar_selectionchange($event, 'treeexpbar_tree', $event2);
    }

    /**
     * treeexpbar_tree 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TreeExpViewtreeexpbarTreeExpBarBase
     */
    public treeexpbar_tree_load($event: any, $event2?: any) {
        this.treeexpbar_load($event, 'treeexpbar_tree', $event2);
    }

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

    /**
     * 打开编辑数据视图
     *
     * @type {any}
     * @memberof TreeExpViewtreeexpbarBase
     */
    @Prop() public opendata: any;
    /**
     * 视图唯一标识
     *
     * @type {boolean}
     * @memberof TreeExpViewtreeexpbarBase
     */
    @Prop() public viewUID!:string;

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

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

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

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

    /**
     * 过滤值
     *
     * @type {string}
     * @memberof TreeExpViewtreeexpbarBase
     */
    public srfnodefilter: string = '';

    /**
     * 刷新标识
     *
     * @public
     * @type {number}
     * @memberof TreeExpViewtreeexpbarBase
     */
    public counter:number = 0;

    /**
     * 是否加载默认关联视图
     *
     * @public
     * @type {boolean}
     * @memberof TreeExpViewtreeexpbarBase
     */
    public istLoadDefaultRefView: boolean = false;

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

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

    /**
     * 获取关系项视图
     *
     * @param {*} [arg={}]
     * @returns {*}
     * @memberof TreeExpViewtreeexpbarBase
     */
    public getExpItemView(arg: any = {}): any {
        let expmode = arg.nodetype.toUpperCase();
        if (!expmode) {
            expmode = '';
        }
        if (Object.is(expmode, 'TR09')) {
            return {  
                viewname: 'hrtechnical-title-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRTECHNICALTITLE_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrtechnicaltitle'
			};
        }
        if (Object.is(expmode, 'TR0')) {
            return {  
                viewname: 'hremployee-v-002', 
                parentdata: {},
                deKeyField:'hremployee'
			};
        }
        if (Object.is(expmode, 'TR10')) {
            return {  
                viewname: 'hrcontract-v-001', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRCONTRACT_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrcontract'
			};
        }
        if (Object.is(expmode, 'TR12')) {
            return {  
                viewname: 'hrreward-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRREWARD_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrreward'
			};
        }
        if (Object.is(expmode, 'TR07')) {
            return {  
                viewname: 'hrarchives-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRARCHIVES_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrarchives'
			};
        }
        if (Object.is(expmode, 'TR11')) {
            return {  
                viewname: 'hrcertificate-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRCERTIFICATE_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrcertificate'
			};
        }
        if (Object.is(expmode, 'TR06')) {
            return {  
                viewname: 'hrwork-record-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRWORKRECORD_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrworkrecord'
			};
        }
        if (Object.is(expmode, 'TR14')) {
            return {  
                viewname: 'hrresearch-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRRESEARCH_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrresearch'
			};
        }
        if (Object.is(expmode, 'TR05')) {
            return {  
                viewname: 'hrfamily-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRFAMILY_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrfamily'
			};
        }
        if (Object.is(expmode, 'TR13')) {
            return {  
                viewname: 'hrtraining-record-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRTRAININGRECORD_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrtrainingrecord'
			};
        }
        if (Object.is(expmode, 'TR04')) {
            return {  
                viewname: 'hrlanguage-ability-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRLANGUAGEABILITY_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrlanguageability'
			};
        }
        if (Object.is(expmode, 'TR03')) {
            return {  
                viewname: 'hreducation-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HREDUCATION_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hreducation'
			};
        }
        if (Object.is(expmode, 'TR01')) {
            return {  
                viewname: 'hrdistribution-grid-view', 
                parentdata: {"srfparentdefname":"HREMPLOYEEID","srfparentdename":"HREMPLOYEE","srfparentmode":"DER1N_HRDISTRIBUTION_HREMPLOYEE_HREMPLOYEEID_327A4D"},
                deKeyField:'hrdistribution'
			};
        }
        return null;
    }

    /**
     * 树导航选中
     *
     * @param {any []} args
     * @param {string} [tag]
     * @param {*} [$event2]
     * @returns {void}
     * @memberof TreeExpViewtreeexpbarBase
     */
    public treeexpbar_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.$forceUpdate();
    }

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

    /**
     * 执行搜索
     *
     * @memberof TreeExpViewtreeexpbarBase
     */
    public onSearch(): void {
        if (!this.viewState) {
            return;
        }
        this.istLoadDefaultRefView = false;
        this.viewState.next({ tag: 'treeexpbar_tree', action: 'filter', data: { srfnodefilter: this.srfnodefilter } });
    }

    /**
     * vue 声明周期
     *
     * @memberof @memberof TreeExpViewtreeexpbarBase
     */
    public created() {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof TreeExpViewtreeexpbarBase
     */    
    public afterCreated(){
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                this.istLoadDefaultRefView = false;
                this.viewState.next({ tag: 'treeexpbar_tree', action: action, data: data });
            });
        }
    }

    /**
    * Vue声明周期(组件渲染完毕)
    *
    * @memberof TreeExpViewtreeexpbarBase
    */
    public mounted() {
        this.afterMounted();     
    }

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


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

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

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

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

    /**
     * 视图数据加载完成
     *
     * @param {*} $event
     * @memberof TreeExpViewtreeexpbarBase
     */
    public onViewLoad($event: any): void {
        this.$emit('load', $event);
    }
}