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 HRTransferApplyWFDynaEditViewBase
 * @extends {ViewBase}
 */
export class HRTransferApplyWFDynaEditViewBase extends ViewBase {
    /**
     * 视图对应应用实体名称
     *
     * @protected
     * @type {string}
     * @memberof HRTransferApplyWFDynaEditViewBase
     */
    protected appDeName: string = 'hrtransferapply';

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

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

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

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


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

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

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


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


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

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

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

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

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


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

    /**
     * 获取工具栏按钮
     * 
     * @memberof HRTransferApplyWFDynaEditViewBase
     */
    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 HRTransferApplyWFDynaEditViewBase
     */
    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);
            // }
        });
    }



}