import { GridViewEngine, ModelTool } from '@ibizstudio/runtime';
import { MDViewBase } from './mdview-base';
/**
 * 表格视图基类
 *
 * @export
 * @class GridViewBase
 * @extends {MDViewBase}
 * @implements {GirdViewInterface}
 */
export class GridViewBase extends MDViewBase {
    constructor() {
        super(...arguments);
        /**
         * 表格行数据默认激活模式
         * 0 不激活
         * 1 单击激活
         * 2 双击激活
         *
         * @protected
         * @type {(0 | 1 | 2)}
         * @memberof GridViewBase
         */
        this.gridRowActiveMode = 0;
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof GridViewBase
         */
        this.engine = new GridViewEngine();
    }
    /**
     * 引擎初始化
     *
     * @param {*} [opts={}] 引擎参数
     * @memberof GridViewBase
     */
    engineInit(opts = {}) {
        var _a, _b, _c, _d, _e;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.engine && this.gridInstance && this.$refs[this.gridInstance.name]) {
            let engineOpts = Object.assign({
                view: this,
                parentContainer: this.$parent,
                p2k: '0',
                isLoadDefault: (_a = this.viewInstance) === null || _a === void 0 ? void 0 : _a.loadDefault,
                keyPSDEField: this.appDeCodeName.toLowerCase(),
                majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
                opendata: (args, fullargs, params, $event, xData) => {
                    this.opendata(args, fullargs, params, $event, xData);
                },
                newdata: (args, fullargs, params, $event, xData) => {
                    this.newdata(args, fullargs, params, $event, xData);
                },
                grid: this.$refs[(_b = this.gridInstance) === null || _b === void 0 ? void 0 : _b.name].ctrl,
            }, opts);
            if (((_c = this.searchFormInstance) === null || _c === void 0 ? void 0 : _c.name) && this.$refs[this.searchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.searchFormInstance.name].ctrl);
            }
            else if (((_d = this.quickSearchFormInstance) === null || _d === void 0 ? void 0 : _d.name) && this.$refs[this.quickSearchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.quickSearchFormInstance.name].ctrl);
            }
            if (((_e = this.searchBarInstance) === null || _e === void 0 ? void 0 : _e.name) && this.$refs[this.searchBarInstance.name]) {
                engineOpts.searchbar = (this.$refs[this.searchBarInstance.name].ctrl);
            }
            this.engine.init(engineOpts);
        }
    }
    /**
     * 初始化表格视图实例
     *
     * @memberof GridViewBase
     */
    async viewModelInit() {
        var _a, _b;
        this.viewInstance = ((_a = this.staticProps) === null || _a === void 0 ? void 0 : _a.modeldata);
        await super.viewModelInit();
        this.gridInstance = ModelTool.findPSControlByType("GRID", this.viewInstance.getPSControls());
        this.gridRowActiveMode = (_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.gridRowActiveMode;
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof GridViewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.gridInstance);
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: (_a = this.gridInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
    /**
     * 快速搜索
     *
     * @param {*} $event 事件源对象
     * @memberof GridViewBase
     */
    onSearch($event) {
        var _a, _b;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        const refs = this.$refs;
        if (refs[(_a = this.gridInstance) === null || _a === void 0 ? void 0 : _a.name] && refs[this.gridInstance.name].ctrl) {
            refs[(_b = this.gridInstance) === null || _b === void 0 ? void 0 : _b.name].ctrl.load(this.context, true);
        }
    }
    /**
     * 计算目标部件所需参数
     *
     * @param {any} [controlInstance] 部件模型实例
     * @returns
     * @memberof GridViewBase
     */
    computeTargetCtrlData(controlInstance) {
        var _a, _b;
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.staticProps, {
            isOpenEdit: (_a = this.viewInstance) === null || _a === void 0 ? void 0 : _a.rowEditDefault,
            gridRowActiveMode: (_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.gridRowActiveMode,
        });
        return { targetCtrlName: targetCtrlName, targetCtrlParam: targetCtrlParam, targetCtrlEvent: targetCtrlEvent };
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof GridViewBase
     */
    onCtrlEvent(controlname, action, data) {
        if (action) {
            switch (action) {
                case "save":
                    this.$emit("view-event", { action: "drdatasaved", data: data });
                    break;
                case "search":
                    this.onSearch(data);
                    break;
                default:
                    super.onCtrlEvent(controlname, action, data);
                    break;
            }
        }
    }
}
