import ViewEngine from './view-engine';

/**
 * 编辑视图引擎
 *
 * @export
 * @class MobWFActionViewEngine
 * @extends {ViewEngine}
 */
export default class MobWFActionViewEngine extends ViewEngine {

    /**
     * 表单部件
     *
     * @protected
     * @type {*}
     * @memberof MobWFActionViewEngine
     */
    protected form: any;

    /**
     * 父健为当前健
     *
     * @protected
     * @type {string}
     * @memberof MobWFActionViewEngine
     */
    protected p2k: string = '';

    /**
     * 初始化编辑视图引擎
     *
     * @param {*} [options={}]
     * @memberof MobWFActionViewEngine
     */
    public init(options: any = {}): void {
        this.form = options.form;
        this.p2k = options.p2k;
        super.init(options);
    }

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof MobWFActionViewEngine
     */
    public load(opts: any = {}): void {
        super.load(opts);
        if (this.getForm()) {
            const tag = this.getForm().name;
            if (Object.is(this.p2k, '1') && this.viewdata.srfparentdata && !Object.is(this.viewdata.srfparentdata.srfparentkey, '')) {
                Object.assign(this.viewdata, { srfkey: this.viewdata.srfparentdata.srfparentkey });
            }
            const action: string = this.viewdata.srfkey && !Object.is(this.viewdata.srfkey, '') ? 'load' : 'loaddraft';
            // const action: string ='loaddraft';
            this.setViewState2({ tag: tag, action: action, viewdata: this.viewdata });
        }
    }

    /**
     * 部件事件机制
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        super.onCtrlEvent(ctrlName, eventName, args);
        if (Object.is(ctrlName, 'form')) {
            this.formEvent(eventName, args);
        }
    }

    /**
     * 表单事件
     *
     * @param {string} eventName
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public formEvent(eventName: string, args: any): void {
        if (Object.is(eventName, 'load')) {
            this.onFormLoad(args);
        }
        if (Object.is(eventName, 'save')) {
            this.onFormSave(args);
        }
        if (Object.is(eventName, 'remove')) {
            this.onFormRemove(args);
        }
        if (Object.is(eventName, 'wfstart')) {
            this.onFormWFStart(args);
        }
        if (Object.is(eventName, 'wfsubmit')) {
            this.onFormWFSubmit(args);
        }
    }

    /**
     * 表单加载完成
     *
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onFormLoad(args: any): void {
        if (Object.is(args.srfuf, '1')) {
            this.view.datainfo = args.srfmajortext;
            this.view.viewdataschange.emit({action:'load',items:args});
        }
    }

    /**
     * 表单工作流开始流程完成
     *
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onFormWFStart(args: any): void {
        if (this.view) {
            this.view.viewdataschange.emit({action:'wfstart',items:args});
        }
    }

    /**
     * 表单工作流提交完成
     *
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onFormWFSubmit(args: any): void {
        if (this.view) {
            this.view.viewdataschange.emit({action:'wfsubmit',items:args});
            this.view.$activeData = args;
            this.view.backView();
            this.view.$navCtrl.back();
        }
    }

    /**
     * 表单保存完成
     *
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onFormSave(args: any): void {
        if(this.view){
            this.view.viewdataschange.emit({action:'save',items:args});
            if(this.viewdata){
                this.submitData(this.viewdata);
            }else{
                this.submitData({});
            }
        }
    }

    /**
     * 表单删除完成
     *
     * @param {*} args
     * @memberof MobWFActionViewEngine
     */
    public onFormRemove(args: any): void {
        this.view.viewrefreshTag =true;
        this.view.viewdataschange.emit({action:'remove',items:args});
    }

    /**
     * 处理实体界面行为
     *
     * @param {string} tag
     * @param {string} [actionmode]
     * @returns {void}
     * @memberof MobWFActionViewEngine
     */
    public doSysUIAction(tag: string, actionmode?: string): void {
        if (Object.is(tag, 'Save')) {
            this.doSave();
            return;
        }
        super.doSysUIAction(tag, actionmode);
    }

    /**
     * 编辑界面_保存操作
     * 
     * @memberof IBizEditViewController
     */
    public doSave(): void {
        // this.afterformsaveaction = '';
        if(this.viewdata){
            this.saveData(this.viewdata);
        }else{
            this.saveData({});
        }
    }

    /**
     * 保存视图数据
     *
     * @param {*} [arg={}]
     * @memberof MobWFActionViewEngine
     */
    public saveData(arg: any = {}): void {
        if (this.getForm()) {
            const tag = this.getForm().name;
            this.setViewState2({ tag: tag, action: 'save', viewdata: arg });
        }
    }

     /**
     * 提交工作流
     *
     * @param {*} [arg={}]
     * @memberof MobWFActionViewEngine
     */
    public submitData(arg: any = {}): void {
        if (this.getForm()) {
            const tag = this.getForm().name;
            this.setViewState2({ tag: tag, action: 'submit', viewdata: arg });
        }
    }

    /**
     * 获取表单对象
     *
     * @returns {*}
     * @memberof MobWFActionViewEngine
     */
    public getForm(): any {
        return this.form;
    }

}