import { MDViewBase } from './mdview-base';
import { ModelTool, PickupGridViewEngine } from '@ibizstudio/runtime';
/**
 * 表格选择视图基类
 *
 * @export
 * @class PickupGridViewBase
 * @extends {MDViewBase}
 * @implements {PickupGridViewInterface}
 */
export class PickupGridViewBase extends MDViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof PickupGridViewBase
         */
        this.engine = new PickupGridViewEngine();
        /**
         * 表格行数据默认激活模式
         * 0 不激活
         * 1 单击激活
         * 2 双击激活
         *
         * @type {(number | 0 | 1 | 2)}
         * @memberof PickupGridViewBase
         */
        this.gridRowActiveMode = 2;
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof PickupGridViewBase
     */
    engineInit(opts) {
        var _a, _b, _c, _d;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.engine && this.gridInstance) {
            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[this.gridInstance.name].ctrl,
            }, opts);
            if (((_b = this.searchFormInstance) === null || _b === void 0 ? void 0 : _b.name) && this.$refs[this.searchFormInstance.name]) {
                engineOpts.searchform = this.$refs[this.searchFormInstance.name].ctrl;
            }
            else if (((_c = this.quickSearchFormInstance) === null || _c === void 0 ? void 0 : _c.name) && this.$refs[this.quickSearchFormInstance.name]) {
                engineOpts.searchform = this.$refs[this.quickSearchFormInstance.name].ctrl;
            }
            if (((_d = this.searchBarInstance) === null || _d === void 0 ? void 0 : _d.name) && this.$refs[this.searchBarInstance.name]) {
                engineOpts.searchbar = this.$refs[this.searchBarInstance.name].ctrl;
            }
            this.engine.init(engineOpts);
        }
    }
    /**
     * 初始化分页导航视图实例
     *
     * @memberof PickupGridViewBase
     */
    async viewModelInit() {
        var _a, _b;
        this.viewInstance = (_a = this.staticProps) === null || _a === void 0 ? void 0 : _a.modeldata;
        await super.viewModelInit();
        this.gridRowActiveMode = (_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.gridRowActiveMode;
        this.gridInstance = ModelTool.findPSControlByType('GRID', this.viewInstance.getPSControls());
    }
    /**
     * 监听视图动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PickupGridViewBase
     */
    onDynamicPropsChange(newVal, oldVal) {
        super.onDynamicPropsChange(newVal, oldVal);
        if ((newVal === null || newVal === void 0 ? void 0 : newVal.selectedData) && newVal.selectedData != (oldVal === null || oldVal === void 0 ? void 0 : oldVal.selectedData)) {
            this.selectedData = newVal.selectedData;
        }
    }
    /**
     * 监听视图静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PickupGridViewBase
     */
    onStaticPropsChange(newVal, oldVal) {
        this.isSingleSelect = newVal.isSingleSelect;
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof PickupGridViewBase
     */
    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 });
    }
    /**
     * 渲染搜索表单
     *
     * @memberof PickupGridViewBase
     */
    renderSearchForm() {
        var _a, _b, _c, _d;
        if (!this.searchFormInstance) {
            return;
        }
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.searchFormInstance);
        Object.assign(targetCtrlParam.staticProps, { enableSaveFilter: false });
        if (((_a = this.viewInstance) === null || _a === void 0 ? void 0 : _a.viewStyle) == 'DEFAULT' && ((_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.enableQuickSearch)) {
            return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: (_c = this.searchFormInstance) === null || _c === void 0 ? void 0 : _c.name, on: targetCtrlEvent });
        }
        else {
            return this.$createElement(targetCtrlName, { slot: 'searchForm', props: targetCtrlParam, ref: (_d = this.searchFormInstance) === null || _d === void 0 ? void 0 : _d.name, on: targetCtrlEvent });
        }
    }
    /**
     * 计算目标部件所需参数
     *
     * @param {string} [controlType]
     * @returns
     * @memberof PickupGridViewBase
     */
    computeTargetCtrlData(controlInstance) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.dynamicProps, {
            selectedData: this.selectedData,
        });
        Object.assign(targetCtrlParam.staticProps, {
            gridRowActiveMode: 2,
            isSingleSelect: this.isSingleSelect,
        });
        return { targetCtrlName, targetCtrlParam, 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 ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof GridViewBase
     */
    onCtrlEvent(controlname, action, data) {
        if (action) {
            switch (action) {
                case 'search':
                    this.onSearch(data);
                    break;
                default:
                    super.onCtrlEvent(controlname, action, data);
                    break;
            }
        }
    }
}
