import { ModelTool, TabSearchViewEngine, Util } from '@ibizstudio/runtime';
import { MDViewBase } from './mdview-base';
/**
 * 实体分页搜索视图基类
 *
 * @export
 * @class TabSearchViewBase
 * @extends {MDViewBase}
 * @implements {TabSearchViewInterface}
 */
export class TabSearchViewBase extends MDViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof TabSearchViewBase
         */
        this.engine = new TabSearchViewEngine();
        /**
         * 当前激活分页视图面板
         *
         * @memberof TabSearchViewBase
         */
        this.activiedTabViewPanel = {};
        /**
         * 分页标题绘制状态
         *
         * @memberof TabSearchViewBase
         */
        this.tabsHeaderRenderState = false;
    }
    /**
     * 加载模型
     *
     * @memberof TabSearchViewBase
     */
    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.$t(_this.model.srfCaption),
                            title: _this.$t(_this.model.srfCaption),
                            info: _this.model.dataInfo,
                            viewtag: this.viewtag
                        });
                    }
                    if (_this.$route) {
                        _this.$route.meta.info = _this.model.dataInfo;
                    }
                }
            });
        }
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof TabSearchViewBase
     */
    engineInit() {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let engineOpts = ({
            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(engineOpts);
    }
    /**
     * 初始化分页导航视图实例
     *
     * @memberof TabSearchViewBase
     */
    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());
        const tabViewPanels = this.tabExpPanelInstance.getPSControls() || [];
        if (tabViewPanels.length > 0) {
            this.activiedTabViewPanel = tabViewPanels[0].name;
        }
    }
    /**
     * 分页面板点击
     *
     * @memberof TabSearchViewBase
     */
    tabPanelClick(event) {
        var _a;
        if (event) {
            this.viewState.next({ tag: (_a = this.tabExpPanelInstance) === null || _a === void 0 ? void 0 : _a.name, action: 'changeActivedTab', data: event });
        }
    }
    /**
     * 初始化分页导航面板标题
     *
     * @memberof TabSearchViewBase
     */
    initTabExpHeader(data) {
        const getActivedTabViewPanel = () => {
            const tabViewPanels = this.tabExpPanelInstance.getPSControls() || [];
            if (tabViewPanels.length > 0) {
                return tabViewPanels[0].name;
            }
            return null;
        };
        this.activiedTabViewPanel = (data === null || data === void 0 ? void 0 : data.activiedTabViewPanel) || getActivedTabViewPanel();
        this.authResourceObject = data === null || data === void 0 ? void 0 : data.authResourceObject;
        this.tabsHeaderRenderState = this.viewInstance.viewStyle == 'DEFAULT' ? true : false;
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof TabSearchViewBase
     */
    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 });
    }
    /**
     * 绘制分页面板项
     *
     * @memberof TabSearchViewBase
     */
    renderTabPaneContent(tabViewPanel, index) {
        var _a, _b, _c;
        const appCounterRef = tabViewPanel.getPSAppCounterRef();
        let viewPanelCount = undefined;
        if (appCounterRef && tabViewPanel.counterId) {
            const targetCounterService = (_a = Util.findElementByField(this.counterServiceArray, 'id', appCounterRef.id)) === null || _a === void 0 ? void 0 : _a.service;
            viewPanelCount = (_b = targetCounterService === null || targetCounterService === void 0 ? void 0 : targetCounterService.counterData) === null || _b === void 0 ? void 0 : _b[tabViewPanel.counterId.toLowerCase()];
        }
        const tabsName = `${this.appDeCodeName}_${this.viewInstance.codeName}_tabexpheader`;
        let disabled = this.authResourceObject && ((_c = this.authResourceObject[tabViewPanel.name]) === null || _c === void 0 ? void 0 : _c.disabled);
        const IPSSysImage = tabViewPanel.getPSSysImage();
        return (<tab-pane lazy={true} name={tabViewPanel.name} tab={tabsName} disabled={disabled} label={(h) => {
                return h('div', [
                    IPSSysImage ? IPSSysImage.imagePath ?
                        h('img', {
                            src: IPSSysImage.imagePath,
                            style: {
                                'margin-right': '2px'
                            }
                        }) :
                        h('i', {
                            class: IPSSysImage.cssClass,
                            style: {
                                'margin-right': '2px'
                            }
                        }) : '',
                    h('span', tabViewPanel.caption),
                    h('Badge', {
                        props: {
                            count: viewPanelCount,
                            type: 'primary'
                        }
                    })
                ]);
            }}>
            </tab-pane>);
    }
    /**
     * 绘制分页面板标题栏
     *
     * @memberof TabSearchViewBase
     */
    renderTabsHeader() {
        var _a;
        if (!this.tabsHeaderRenderState) {
            return;
        }
        const IPSDETabViewPanel = (_a = this.tabExpPanelInstance) === null || _a === void 0 ? void 0 : _a.getPSControls();
        const tabsName = `${this.appDeCodeName}_${this.viewInstance.codeName}_tabexpheader`;
        return (<div class={{ 'tabviewpanel-header': true }} slot="tabsHeader">
                <tabs value={this.activiedTabViewPanel} animated={false} class='tabexppanel' name={tabsName} on-on-click={($event) => this.tabPanelClick($event)}>
                    {IPSDETabViewPanel === null || IPSDETabViewPanel === void 0 ? void 0 : IPSDETabViewPanel.map((tabViewPanel, index) => {
                var _a;
                return this.authResourceObject && ((_a = this.authResourceObject[tabViewPanel.name]) === null || _a === void 0 ? void 0 : _a.visabled) ?
                    this.renderTabPaneContent(tabViewPanel, index) : null;
            })}
                </tabs>
            </div>);
    }
    /**
     * 部件事件处理
     *
     * @memberof TabSearchViewBase
     */
    onCtrlEvent(controlName, action, data) {
        super.onCtrlEvent(controlName, action, data);
        if (action == 'tabexppanelIsMounted') {
            this.initTabExpHeader(data);
        }
        if (action == 'viewPanelIsChange') {
            this.activiedTabViewPanel = data;
        }
    }
}
