import ViewEngine from './view-engine';

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

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

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

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

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof MobEditView9Engine
     */
    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';
            this.setViewState2({ tag: tag, action: action, viewdata: this.viewdata });
        }
    }

    /**
     * 部件事件机制
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof MobEditView9Engine
     */
    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 MobEditView9Engine
     */
    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);
        }
    }

    /**
     * 表单加载完成
     *
     * @param {*} args
     * @memberof MobEditView9Engine
     */
    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 MobEditView9Engine
     */
    public onFormSave(args: any): void {
        if(this.view){
            this.view.viewdataschange.emit({action:'save',items:args});
        }
    }

    /**
     * 表单删除完成
     *
     * @param {*} args
     * @memberof MobEditView9Engine
     */
    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 MobEditView9Engine
     */
    public doSysUIAction(tag: string, actionmode?: string): void {
        // if (Object.is(tag, 'Help')) {
        //     this.doHelp();
        //     return;
        // }
        // if (Object.is(tag, 'SaveAndStart')) {
        //     this.doSaveAndStart();
        //     return;
        // }
        // if (Object.is(tag, 'SaveAndExit')) {
        //     this.doSaveAndExit();
        //     return;
        // }
        // if (Object.is(tag, 'SaveAndNew')) {
        //     this.doSaveAndNew();
        //     return;
        // }
        if (Object.is(tag, 'Save')) {
            this.doSave();
            return;
        }
        // if (Object.is(tag, 'Print')) {
        //     this.doPrint();
        //     return;
        // }
        // if (Object.is(tag, 'Copy')) {
        //     this.doCopy();
        //     return;
        // }
        // if (Object.is(tag, 'RemoveAndExit')) {
        //     this.doRemoveAndExit();
        //     return;
        // }
        // if (Object.is(tag, 'Refresh')) {
        //     this.doRefresh();
        //     return;
        // }
        // if (Object.is(tag, 'New')) {
        //     this.doNew();
        //     return;
        // }
        // if (Object.is(tag, 'FirstRecord')) {
        //     this.doMoveToRecord('first');
        //     return;
        // }
        // if (Object.is(tag, 'PrevRecord')) {
        //     this.doMoveToRecord('prev');
        //     return;
        // }
        // if (Object.is(tag, 'NextRecord')) {
        //     this.doMoveToRecord('next');
        //     return;
        // }
        // if (Object.is(tag, 'LastRecord')) {
        //     this.doMoveToRecord('last');
        //     return;
        // }
        // if (Object.is(tag, 'Exit') || Object.is(tag, 'Close')) {
        //     this.doExit();
        //     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 MobEditView9Engine
     */
    public saveData(arg: any = {}): void {
        if (this.getForm()) {
            const tag = this.getForm().name;
            this.setViewState2({ tag: tag, action: 'save', viewdata: arg });
        }
    }

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

}