import ViewEngine from './view-engine';

/**
 * 数据选择视图引擎
 *
 * @export
 * @class MDViewEngine
 * @extends {ViewEngine}
 */
export default class MobMPickupViewEngine extends ViewEngine {

     /**
     * pickupviewpanel面板
     *
     * @type {*}
     * @memberof GridViewEngine
     */
    protected pickupviewpanel: any;

    /**
     * 打开数据
     *
     * @type {*}
     * @memberof GridViewEngine
     */
    protected openData?: (params: any) => void;

    /**
     * 新建数据
     *
     * @protected
     * @memberof GridViewEngine
     */
    protected newData?: (params: any) => void;

    /**
     * Creates an instance of GridViewEngine.
     * @memberof GridViewEngine
     */
    constructor() {
        super();
    }

    /**
     * 引擎初始化
     *
     * @param {*} [options={}]
     * @memberof GridViewEngine
     */
    public init(options: any = {}): void {
        this.pickupviewpanel = options.pickupviewpanel;
        this.openData = options.opendata;
        this.newData = options.newdata;
        super.init(options);
    }

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof MDViewEngine
     */
    public load(opts: any = {}): void {
        super.load(opts);
        const _srfparentdata = this.viewdata.srfparentdata ? { srfparentdata: this.viewdata.srfparentdata } : { srfparentdata: {} };
        if (this.getPickupviewpanel()) {
            const tag = this.getPickupviewpanel().name;
            this.setViewState2({ tag: tag, action: 'load', viewdata: _srfparentdata });
        }
    }

    /**
     * 部件事件
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof GridViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        super.onCtrlEvent(ctrlName, eventName, args);
        if (Object.is(ctrlName, 'pickupviewpanel')) {
            this.pickupviewpanelEvent(eventName, args);
        }
    }

    /**
     * pickupviewpanel事件
     *
     * @param {string} eventName
     * @param {*} [args={}]
     * @memberof MDViewEngine
     */
    public pickupviewpanelEvent(eventName: string, args: any = {}): void {
        if (Object.is(eventName, 'load')) {
            this.onPickupviewpanelLoad(args);
        }
        if (Object.is(eventName, 'dataschange')) {
            this.onickupviewpanelDatasChange(args);
        }
    }

    /**
     * 搜索表单加载完成
     *
     * @param {*} [args={}]
     * @memberof MDViewEngine
     */
    public onPickupviewpanelLoad(args: any = {}): void {
        if (this.view) {
            this.view.viewload.emit(args);
        }
    }

    public onickupviewpanelDatasChange(args: any = {}): void {
        if (this.view) {
            this.view.viewdataschange.emit(args);
            this.view.viewDatasChange(args);
        }
    }

    public doSysUIAction(tag: string, actionmode?: string): void {
        super.doSysUIAction(tag, actionmode);
    }

    public getPickupviewpanel(): any {
        return this.pickupviewpanel;
    }

}