import { Subject } from 'rxjs';
import { ViewBase } from '@/studio-core';
import HRTransferApplyService from '@/service/hrtransfer-apply/hrtransfer-apply-service';
import HRTransferApplyAuthService from '@/authservice/hrtransfer-apply/hrtransfer-apply-auth-service';
import WFDynaExpGridViewEngine from '@engine/view/wfdyna-exp-grid-view-engine';
import HRTransferApplyUIService from '@/uiservice/hrtransfer-apply/hrtransfer-apply-ui-service';

/**
 * 调动申请工作流导航表格视图视图基类
 *
 * @export
 * @class HRTransferApplyWFDynaExpGridViewBase
 * @extends {ViewBase}
 */
export class HRTransferApplyWFDynaExpGridViewBase extends ViewBase {
    /**
     * 视图对应应用实体名称
     *
     * @protected
     * @type {string}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected appDeName: string = 'hrtransferapply';

    /**
     * 应用实体主键
     *
     * @protected
     * @type {string}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected appDeKey: string = 'hrtransferapplyid';

    /**
     * 应用实体主信息
     *
     * @protected
     * @type {string}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected appDeMajor: string = 'hrtransferapplyname';

    /**
     * 实体服务对象
     *
     * @type {HRTransferApplyService}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected appEntityService: HRTransferApplyService = new HRTransferApplyService;

    /**
     * 实体权限服务对象
     *
     * @type HRTransferApplyUIService
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public appUIService: HRTransferApplyUIService = new HRTransferApplyUIService(this.$store);


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

    /**
     * 视图模型数据
     *
     * @protected
     * @type {*}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected model: any = {
        srfCaption: 'entities.hrtransferapply.views.wfdynaexpgridview.caption',
        srfTitle: 'entities.hrtransferapply.views.wfdynaexpgridview.title',
        srfSubTitle: 'entities.hrtransferapply.views.wfdynaexpgridview.subtitle',
        dataInfo: ''
    }

    /**
     * 容器模型
     *
     * @protected
     * @type {*}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    protected containerModel: any = {
        view_toolbar: { name: 'toolbar', type: 'TOOLBAR' },
        view_grid: { name: 'grid', type: 'GRID' },
        view_searchform: { name: 'searchform', type: 'SEARCHFORM' },
        wflinks: [],
    };

    /**
     * 工具栏模型
     *
     * @type {*}
     * @memberof HRTransferApplyWFDynaExpGridView
     */
    public toolBarModels: any = {
    };



	/**
     * 视图唯一标识
     *
     * @protected
     * @type {string}
     * @memberof ViewBase
     */
	protected viewtag: string = '10c50502a4eebb3c2f6373326db96fc5';


    /**
     * 视图引擎
     *
     * @public
     * @type {Engine}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public engine: WFDynaExpGridViewEngine = new WFDynaExpGridViewEngine();

    /**
     * 引擎初始化
     *
     * @public
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public engineInit(): void {
        this.engine.init({
            view: this,
            opendata: (args: any[],fullargs?:any[],params?: any, $event?: any, xData?: any) => {
                this.opendata(args,fullargs, params, $event, xData);
            },
            newdata: (args: any[],fullargs?:any[],params?: any, $event?: any, xData?: any) => {
                this.newdata(args,fullargs, params, $event, xData);
            },
            grid: this.$refs.grid,
            searchform: this.$refs.searchform,
            keyPSDEField: 'hrtransferapply',
            majorPSDEField: 'hrtransferapplyname',
            isLoadDefault: true,
        });
    }

    /**
     * grid 部件 selectionchange 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public grid_selectionchange($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('grid', 'selectionchange', $event);
    }

    /**
     * grid 部件 beforeload 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public grid_beforeload($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('grid', 'beforeload', $event);
    }

    /**
     * grid 部件 rowdblclick 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public grid_rowdblclick($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('grid', 'rowdblclick', $event);
    }

    /**
     * grid 部件 remove 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public grid_remove($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('grid', 'remove', $event);
    }

    /**
     * grid 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public grid_load($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('grid', 'load', $event);
    }

    /**
     * searchform 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public searchform_save($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('searchform', 'save', $event);
    }

    /**
     * searchform 部件 search 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public searchform_search($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('searchform', 'search', $event);
    }

    /**
     * searchform 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public searchform_load($event: any, $event2?: any): void {
        this.engine.onCtrlEvent('searchform', 'load', $event);
    }

    /**
     * 打开新建数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [fullargs]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof HRTransferApplyWFDynaExpGridView
     */
    public newdata(args: any[],fullargs?:any[], params?: any, $event?: any, xData?: any) {
        let localContext:any = null;
        let localViewParam:any =null;
        const data: any = {};
        if(args[0].srfsourcekey){
            data.srfsourcekey = args[0].srfsourcekey;
        }
        let tempContext = JSON.parse(JSON.stringify(this.context));
        delete tempContext.hrtransferapply;
        if(args.length >0){
            Object.assign(tempContext,args[0]);
        }
        let deResParameters: any[] = [];
        if(tempContext.hrorganization && true){
            deResParameters = [
            { pathName: 'hrorganizations', parameterName: 'hrorganization' },
            ]
        }
        const parameters: any[] = [
            { pathName: 'hrtransferapplies', parameterName: 'hrtransferapply' },
            { pathName: 'editview', parameterName: 'editview' },
        ];
        const _this: any = this;
        const openIndexViewTab = (data: any) => {
            const _data: any = { w: (new Date().getTime()) };
            Object.assign(_data, data);
            const routePath = this.$viewTool.buildUpRoutePath(this.$route, tempContext, deResParameters, parameters, args, _data);
            this.$router.push(routePath);
        }
        openIndexViewTab(data);
    }


    /**
     * 打开编辑数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [fullargs]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof HRTransferApplyWFDynaExpGridView
     */
    public opendata(args: any[],fullargs?:any[],params?: any, $event?: any, xData?: any) {
        const localContext: any = null;
        const localViewParam: any =null;
        const data: any = {};
        let tempContext = JSON.parse(JSON.stringify(this.context));
        if(args.length >0){
            Object.assign(tempContext,args[0]);
        }
        let deResParameters: any[] = [];
        if(tempContext.hrorganization && true){
            deResParameters = [
            { pathName: 'hrorganizations', parameterName: 'hrorganization' },
            ]
        }
        const parameters: any[] = [
            { pathName: 'hrtransferapplies', parameterName: 'hrtransferapply' },
            { pathName: 'wfdynaeditview', parameterName: 'wfdynaeditview' },
        ];
        const _this: any = this;
        const openIndexViewTab = (data: any) => {
            const routePath = this.$viewTool.buildUpRoutePath(this.$route, tempContext, deResParameters, parameters, args, data);
            this.$router.push(routePath+'?userTaskId='+this.viewparams.userTaskId);
        }
        openIndexViewTab(data);
    }



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

    /**
     * 树导航栏数据
     *
     * @type {any}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public wfStepModel: Array<any> = [];

    /**
     * 是否展开搜索表单
     *
     * @type {any}
     * @memberof  HRTransferApplyWFDynaExpGridViewBase
     */
    public isExpandSearchForm:boolean = true;

    /**
     * 是否单选
     *
     * @type {any}
     * @memberof  HRTransferApplyWFDynaExpGridViewBase
     */
    public isSingleSelect:boolean = true;

    /**
     * 左侧树的默认配置
     *
     * @type {any}
     * @memberof  HRTransferApplyWFDynaExpGridViewBase
     */
    public defaultProps:any ={
         children: 'children',
         label: 'userTaskName'
    };

    /**
     * 左侧树当前选中节点
     *
     * @type {any}
     * @memberof  HRTransferApplyWFDynaExpGridViewBase
     */
    public curSelectedNode:any;

    /**
     * 获取树导航栏数据
     *
     * @memberof  HRTransferApplyWFDynaExpGridViewBase
     */
    public getWFStepModel():Promise<any>{
        return new Promise((resolve:any, reject:any) =>{
            this.appEntityService.WFGetWFStep().then((response:any) =>{
                if (response && response.status === 200) {
                    this.wfStepModel = response.data;
                    if(this.wfStepModel && this.wfStepModel.length > 0){
                        this.curSelectedNode = this.wfStepModel[0];
                    }
                    Object.assign(this.viewparams,{'userTaskId':this.curSelectedNode['userTaskId'],'processDefinitionKey':this.curSelectedNode['processDefinitionKey']});
                    this.setTreeNodeHighLight(this.curSelectedNode);
                    resolve(response.data);
                }
                }).catch((response: any) => {
                    if (response && response.status) {
                        this.$Notice.error({ title: '错误', desc: response.message });
                        return;
                    }
                    if (!response || !response.status || !response.data) {
                        this.$Notice.error({ title: '错误', desc: '系统异常' });
                        return;
                    }
                });
        })
    }

    /**
     * 左侧树选中节点
     *
     * @type {any}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public handleNodeClick(data:any) {
        this.curSelectedNode = data;
        this.setTreeNodeHighLight(this.curSelectedNode);
        Object.assign(this.viewparams,{'userTaskId':data['userTaskId'],'processDefinitionKey':data['processDefinitionKey']});
        (this.$refs.searchform as any).onSearch();
    }

    /**
     * 设置选中高亮
     *
     * @param {*} data
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    public setTreeNodeHighLight(data: any): void {
        this.$nextTick(() =>{
            const tree: any = this.$refs.tree;
            tree.setCurrentKey(data.userTaskId);
        })
    }

    /**
     * 刷新数据
     *
     * @readonly
     * @type {(number | null)}
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    get refreshdata(): number | null {
        return this.$store.getters['viewaction/getRefreshData'](this.viewtag);
    }

    /**
     * 监控数据变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @returns
     * @memberof HRTransferApplyWFDynaExpGridViewBase
     */
    @Watch('refreshdata')
    onRefreshData(newVal: any, oldVal: any) {
        if (newVal === null || newVal === undefined) {
            return;
        }
        if (newVal === 0) {
            return;
        }
        const grid: any = this.$refs.grid;
        if (grid) {
            grid.load({});
        }
    }


}