import { ModelTool, PortalViewEngine, Util } from '@ibizstudio/runtime';
import { GlobalService } from '@ibizstudio/runtime';
import { MainViewBase } from './mainview-base';
/**
 * 实体数据看板视图基类
 *
 * @export
 * @class DashboardViewBase
 * @extends {MainViewBase}
 * @implements {DashboardViewInterface}
 */
export class DashboardViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof DashboardViewBase
         */
        this.engine = new PortalViewEngine();
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof DashboardViewBase
     */
    engineInit() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        this.engine.init({
            view: this,
            dashboard: this.$refs[this.dashboardInstance.name].ctrl,
            p2k: '0',
            keyPSDEField: this.appDeCodeName.toLowerCase(),
            majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
            isLoadDefault: this.viewInstance.loadDefault,
        });
    }
    /**
     * 初始化列表视图实例
     *
     * @memberof DashboardViewBase
     */
    async viewModelInit() {
        await super.viewModelInit();
        this.dashboardInstance = ModelTool.findPSControlByName('dashboard', this.viewInstance.getPSControls());
        if (!(this.Environment && this.Environment.isPreviewMode)) {
            this.appEntityService = await new GlobalService().getService(this.appDeCodeName, this.context);
        }
    }
    /**
     *  视图挂载
     *
     * @memberof ViewBase
     */
    viewMounted() {
        this.loadModel();
        this.engineInit();
    }
    /**
     * 加载数据
     *
     * @memberof DashboardViewBase
     */
    loadModel() {
        var _a, _b;
        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;
            }
            (_b = (_a = this.appEntityService) === null || _a === void 0 ? void 0 : _a.getDataInfo) === null || _b === void 0 ? void 0 : _b.call(_a, 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.viewState.next({ tag: 'all-portlet', 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;
                    }
                }
            });
        }
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof DashboardViewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.dashboardInstance);
        return this.$createElement(targetCtrlName, {
            props: targetCtrlParam,
            ref: (_a = this.dashboardInstance) === null || _a === void 0 ? void 0 : _a.name,
            on: targetCtrlEvent,
        });
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof DashboardViewBase
     */
    onCtrlEvent(controlname, action, data) {
        if (action == 'refreshAll') {
            this.loadModel();
        }
        super.onCtrlEvent(controlname, action, data);
    }
}
