import { DataPanelEngine, ModelTool, TabExpViewEngine, Util } from '@ibizstudio/runtime';
import { MainViewBase } from './mainview-base';
/**
 * 分页导航视图基类
 *
 * @export
 * @class TabExpViewBase
 * @extends {MainViewBase}
 * @implements {TabExpViewInterface}
 */
export class TabExpViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {TabExpViewEngine}
         * @memberof TabExpviewBase
         */
        this.engine = new TabExpViewEngine();
        /**
         * 数据面板引擎
         *
         * @public
         * @type {DataPanelEngine}
         * @memberof TabExpviewBase
         */
        this.dataPanelEngine = new DataPanelEngine();
    }
    /**
     * 加载模型
     *
     * @memberof TabExpviewBase
     */
    loadModel() {
        var _a;
        let _this = this;
        if (this.context[this.appDeCodeName.toLowerCase()]) {
            let tempContext = Util.deepCopy(this.context);
            if (tempContext && tempContext.srfsessionid) {
                tempContext.srfsessionkey = tempContext.srfsessionid;
                delete tempContext.srfsessionid;
            }
            (_a = this.appEntityService) === null || _a === void 0 ? void 0 : _a.getDataInfo(tempContext, {}, false).then((response) => {
                if (!response || response.status !== 200) {
                    return;
                }
                const { data: _data } = response;
                if (_data.srfopprivs) {
                    this.$store.commit('authresource/setSrfappdeData', { key: `${this.deName}-${_data[this.appDeKeyFieldName.toLowerCase()]}`, value: _data.srfopprivs });
                }
                this.engine.computeToolbarState(false, _data);
                this.viewState.next({ tag: 'tabexppanel', action: 'loadmodel', data: _data });
                if (_data[this.appDeMajorFieldName.toLowerCase()]) {
                    this.model.dataInfo = _data[this.appDeMajorFieldName.toLowerCase()];
                    if (_this.$tabPageExp) {
                        _this.$tabPageExp.setCurPageCaption({
                            caption: _this.model.srfCaption,
                            title: _this.model.srfCaption,
                            info: _this.model.dataInfo,
                            viewtag: this.viewtag
                        });
                    }
                    if (_this.$route) {
                        _this.$route.meta.info = _this.model.dataInfo;
                    }
                }
            });
        }
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof TabExpviewBase
     */
    engineInit() {
        var _a, _b;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let viewEngineOpts = ({
            view: this,
            parentContainer: this.$parent,
            p2k: '0',
            keyPSDEField: this.appDeCodeName.toLowerCase(),
            majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
            isLoadDefault: (_a = this.viewInstance) === null || _a === void 0 ? void 0 : _a.loadDefault,
        });
        this.engine.init(viewEngineOpts);
        if (this.dataPanelInstance) {
            let dataPanelEngineOpts = ({
                view: this,
                datapanel: this.$refs[(_b = this.dataPanelInstance) === null || _b === void 0 ? void 0 : _b.name].ctrl,
                keyPSDEField: this.appDeCodeName.toLowerCase(),
                majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
                isLoadDefault: true,
            });
            this.dataPanelEngine.init(dataPanelEngineOpts);
        }
    }
    /**
     * 初始化分页导航视图实例
     *
     * @memberof TabExpviewBase
     */
    async viewModelInit() {
        var _a;
        this.viewInstance = ((_a = this.staticProps) === null || _a === void 0 ? void 0 : _a.modeldata);
        await super.viewModelInit();
        this.tabExpPanelInstance = ModelTool.findPSControlByType("TABEXPPANEL", this.viewInstance.getPSControls());
        this.dataPanelInstance = ModelTool.findPSControlByName("datapanel", this.viewInstance.getPSControls());
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof TabExpviewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.tabExpPanelInstance);
        return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: (_a = this.tabExpPanelInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
}
