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 WFDynaEditViewEngine from '@engine/view/wfdyna-edit-view-engine';
import HRTransferApplyUIService from '@/uiservice/hrtransfer-apply/hrtransfer-apply-ui-service';

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

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

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

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

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


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

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

    /**
     * 容器模型
     *
     * @protected
     * @type {*}
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    protected containerModel: any = {
        view_toolbar: { name: 'toolbar', type: 'TOOLBAR' },
        view_form: { name: 'form', type: 'FORM' },
        wflinks: [],
    };

    /**
     * 工具栏模型
     *
     * @type {*}
     * @memberof HRTransferApplyUsr2WFDynaEditView
     */
    public toolBarModels: any = {
        tbitem3: { name: 'tbitem3', caption: '保存', 'isShowCaption': true, 'isShowIcon': true, tooltip: '保存', iconcls: 'fa fa-save', icon: '', disabled: false, type: 'DEUIACTION', visabled: true,noprivdisplaymode:2,dataaccaction: '', uiaction: { tag: 'Save', target: '', class: '' } },

        tbitem9_saveandstart: { name: 'tbitem9_saveandstart', caption: '开始流程', 'isShowCaption': true, 'isShowIcon': true, tooltip: '开始流程', disabled: false, type: 'DEUIACTION', visabled: true,noprivdisplaymode:2,dataaccaction: '', uiaction: { tag: 'SaveAndStart', target: '', class: '' } },

    };



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


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

    /**
     * 引擎初始化
     *
     * @public
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public engineInit(): void {
        this.engine.init({
            view: this,
            form: this.$refs.form,
            p2k: '0',
            keyPSDEField: 'hrtransferapply',
            majorPSDEField: 'hrtransferapplyname',
            isLoadDefault: true,
        });
    }

    /**
     * toolbar 部件 click 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public toolbar_click($event: any, $event2?: any): void {
        if (Object.is($event.tag, 'tbitem3')) {
            this.toolbar_tbitem3_click(null, '', $event2);
        }
        if (Object.is($event.tag, 'tbitem9_saveandstart')) {
            this.toolbar_tbitem9_saveandstart_click(null, '', $event2);
        }
    }

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

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

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

    /**
     * 逻辑事件
     *
     * @param {*} [params={}]
     * @param {*} [tag]
     * @param {*} [$event]
     * @memberof 
     */
    public toolbar_tbitem3_click(params: any = {}, tag?: any, $event?: any) {
        // 参数
        // 取数
        let datas: any[] = [];
        let xData: any = null;
        // _this 指向容器对象
        const _this: any = this;
        let paramJO:any = {};
        let contextJO:any = {};
        xData = this.$refs.form;
        if (xData.getDatas && xData.getDatas instanceof Function) {
            datas = [...xData.getDatas()];
        }
        if(params){
          datas = [params];
        }
        // 界面行为
        this.Save(datas, contextJO,paramJO,  $event, xData,this,"HRTransferApply");
    }

    /**
     * 逻辑事件
     *
     * @param {*} [params={}]
     * @param {*} [tag]
     * @param {*} [$event]
     * @memberof 
     */
    public toolbar_tbitem9_saveandstart_click(params: any = {}, tag?: any, $event?: any) {
        // 参数
        // 取数
        let datas: any[] = [];
        let xData: any = null;
        // _this 指向容器对象
        const _this: any = this;
        let paramJO:any = {};
        let contextJO:any = {};
        xData = this.$refs.form;
        if (xData.getDatas && xData.getDatas instanceof Function) {
            datas = [...xData.getDatas()];
        }
        if(params){
          datas = [params];
        }
        // 界面行为
        this.SaveAndStart(datas, contextJO,paramJO,  $event, xData,this,"HRTransferApply");
    }

    /**
     * 保存
     *
     * @param {any[]} args 当前数据
     * @param {any} contextJO 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public Save(args: any[],contextJO?:any, params?: any, $event?: any, xData?: any,actionContext?:any,srfParentDeName?:string) {
        // 界面行为容器对象 _this
        const _this: any = this;
        if (xData && xData.save instanceof Function) {
            xData.save().then((response: any) => {
                if (!response || response.status !== 200) {
                    return;
                }
                _this.$emit('viewdataschange', [{ ...response.data }]);
            });
        } else if (_this.save && _this.save instanceof Function) {
            _this.save();
        }
    }

    /**
     * 开始流程
     *
     * @param {any[]} args 当前数据
     * @param {any} contextJO 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public SaveAndStart(args: any[],contextJO?:any, params?: any, $event?: any, xData?: any,actionContext?:any,srfParentDeName?:string) {
        const _this: any = this;
        if (!xData || !(xData.wfstart instanceof Function)) {
            return;
        }
        let localdata:any = {processDefinitionKey:null};
        xData.wfstart(args,localdata).then((response: any) => {
            if (!response || response.status !== 200) {
                return;
            }
            const { data: _data } = response;
            if(window.parent){
                window.parent.postMessage({ ..._data },'*');
            }
            if (_this.viewdata) {
                _this.$emit('viewdataschange', [{ ..._data }]);
                _this.$emit('close');
            }else if (this.$tabPageExp) {
                this.$tabPageExp.onClose(this.$route.fullPath);
            }
        });
    }

    /**
     * 工具栏模型数据
     * 
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public linkModel:Array<any> = [];

    /**
     * 获取工具栏按钮
     * 
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public getWFLinkModel():Promise<any>{
        return new Promise((resolve:any, reject:any) =>{
            let datas: any[] = [];
            let xData: any = this.$refs.form;
            if (xData.getDatas && xData.getDatas instanceof Function) {
                datas = [...xData.getDatas()];
            }
            if(Object.keys(this.viewparams).length > 0){
                Object.assign(datas,{'taskDefinitionKey':this.viewparams.userTaskId});
            }
            this.appEntityService.GetWFLink(JSON.parse(JSON.stringify(this.context)),datas,true).then((response:any) =>{
                if (response && response.status === 200) {
                    this.linkModel = response.data;
                    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;
                }
            });
        });
    }

    /**
     * 动态工具栏点击
     * 
     * @memberof HRTransferApplyUsr2WFDynaEditViewBase
     */
    public dynamic_toolbar_click(linkItem:any, $event:any){
        let datas: any[] = [];
        let xData: any = this.$refs.form;
        if (xData.getDatas && xData.getDatas instanceof Function) {
            datas = [...xData.getDatas()];
        }
        xData.wfsubmit(datas,linkItem).then((response: any) => {
            if (!response || response.status !== 200) {
                return;
            }
            const { data: _data } = response;

            this.$emit('viewdataschange', [{ ..._data }]);
            this.closeView([{ ..._data }]);
            // if (this.viewdata) {
            //     this.$emit('viewdataschange', [{ ..._data }]);
            //     this.$emit('close');
            // } else if (this.$tabPageExp) {
            //     this.$tabPageExp.onClose(this.$route.fullPath);
            // }
        });
    }



}