import { Util, ViewTool } from '@ibizstudio/runtime';
import { MainControlBase } from './main-control-base';
/**
 * 分页导航面板部件基类
 *
 * @export
 * @class TabExpPanelBase
 * @extends {MainControlBase}
 */
export class TabExpPanelBase extends MainControlBase {
    constructor() {
        super(...arguments);
        /**
         * 是否初始化
         *
         * @returns {any}
         * @memberof TabExpPanelBase
         */
        this.isInit = {};
        /**
         * 行为参数
         *
         * @public
         * @type {*}
         * @memberof TabExpPanelBase
         */
        this.action = '';
        /**
         * 当前激活数据
         *
         * @public
         * @type {*}
         * @memberof TabExpPanelBase
         */
        this.activeData = {};
        /**
         * 分页面板权限标识存储对象
         *
         * @public
         * @type {*}
         * @memberof TabExpPanelBase
         */
        this.authResourceObject = {};
        /**
         * 被激活的分页面板
         *
         * @type {string}
         * @memberof TabExpPanelBase
         */
        this.activiedTabViewPanel = '';
    }
    /**
     * 部件模型数据初始化实例
     *
     * @param {*} [args]
     * @memberof TabExpPanelBase
     */
    async ctrlModelInit(args) {
        await super.ctrlModelInit();
        const allControls = this.controlInstance.getPSControls();
        if (allControls.length > 0) {
            this.activiedTabViewPanel = allControls[0].name;
        }
    }
    /**
     * 执行created后的逻辑
     *
     *  @memberof TabExpPanelBase
     */
    ctrlInit() {
        //设置分页导航srfparentdename和srfparentkey
        super.ctrlInit();
        if (this.context[this.appDeCodeName.toLowerCase()]) {
            Object.assign(this.context, { srfparentdename: this.appDeCodeName.toLowerCase(), srfparentdemapname: this.deName, srfparentkey: this.context[this.appDeCodeName.toLowerCase()] });
        }
    }
    viewStateAction(tag, action, data) {
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
        if (Object.is(action, 'changeActivedTab')) {
            this.tabPanelClick(data);
        }
        if (Object.is(action, 'loadmodel')) {
            this.activeData = data;
            this.computedAuthPanel(data);
        }
        else {
            this.action = action;
            this.viewState.next({ tag: this.activiedTabViewPanel, action: action, data: data });
            this.$forceUpdate();
        }
    }
    /**
     * 部件挂载
     *
     *  @memberof TabExpPanelBase
     */
    ctrlMounted() {
        var _a, _b;
        super.ctrlMounted();
        if (this.viewparams && (this.viewparams.srftabactivate || this.viewparams.srfnavtag)) {
            const activate = ((_a = this.viewparams.srftabactivate) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || ((_b = this.viewparams.srfnavtag) === null || _b === void 0 ? void 0 : _b.toLowerCase());
            if (activate && this.isInit[activate] !== undefined) {
                for (const key in this.isInit) {
                    if (this.isInit.hasOwnProperty(key)) {
                        this.isInit[key] = false;
                    }
                }
                this.$nextTick(() => {
                    this.tabPanelClick(activate);
                });
            }
        }
        this.ctrlEvent({ controlname: this.controlInstance.name, action: 'tabexppanelIsMounted', data: {
                activiedTabViewPanel: this.activiedTabViewPanel,
                authResourceObject: this.authResourceObject
            } });
    }
    /**
     * 分页视图面板数据变更
     *
     * @memberof TabExpPanelBase
     */
    tabViewPanelDatasChange() {
        this.counterRefresh();
    }
    /**
     * 分页面板选中
     *
     * @param {*} $event 选中分页
     * @returns
     * @memberof TabExpPanelBase
     */
    tabPanelClick($event) {
        if (!$event || Object.is(this.activiedTabViewPanel, $event)) {
            return;
        }
        this.isInit = [];
        this.isInit[$event] = true;
        if (!this.viewState) {
            return;
        }
        this.activiedTabViewPanel = $event;
        this.ctrlEvent({ controlname: this.controlInstance.name, action: 'viewPanelIsChange', data: this.activiedTabViewPanel });
        this.viewState.next({ tag: this.activiedTabViewPanel, action: this.action, data: this.activeData });
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof TabExpPanelBase
     */
    onCtrlEvent(controlname, action, data) {
        if (Object.is(controlname, 'tabviewpanel') && action == 'viewpanelDatasChange') {
            this.tabViewPanelDatasChange();
        }
        else {
            super.onCtrlEvent(controlname, action, data);
        }
    }
    /**
     * 初始化解析分页导航面板绘制参数
     *
     * @memberof TabExpPanelBase
     */
    initRenderOptions() {
        super.initRenderOptions();
        const allControls = this.controlInstance.getPSControls();
        this.authResourceObject = [];
        allControls.forEach((item, index) => {
            this.authResourceObject[item.name] = {
                resourcetag: "",
                visabled: true,
                disabled: false
            };
            this.isInit[item.name] = index === 0 ? true : false;
        });
    }
    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof TabExpPanelBase
     */
    getDatas() {
        return [];
    }
    /**
     * 获取单项数据
     *
     * @returns {*}
     * @memberof TabExpPanelBase
     */
    getData() {
        return null;
    }
    /**
     * 计算分页面板权限
     *
     * @memberof TabExpPanelBase
     */
    computedAuthPanel(data) {
        const _this = this;
        if (!data || Object.keys(data).length === 0) {
            return;
        }
        if (this.authResourceObject && Object.keys(this.authResourceObject).length > 0) {
            Object.keys(this.authResourceObject).forEach((key) => {
                if (this.authResourceObject[key] && this.authResourceObject[key]['dataaccaction']) {
                    let tempUIAction = Util.deepCopy(this.authResourceObject[key]);
                    ViewTool.calcActionItemAuthState(data, [tempUIAction], _this.appUIService);
                    this.authResourceObject[key].visabled = this.computedPanelWithResource(key, tempUIAction.visabled);
                    this.authResourceObject[key].disabled = this.computedPanelWithResource(key, tempUIAction.disabled);
                }
            });
            const keys = Object.keys(this.authResourceObject);
            for (let i = 0; i < keys.length; i++) {
                if (this.authResourceObject[keys[i]].visabled) {
                    // this.tabPanelClick(keys[i]);
                    return;
                }
            }
        }
    }
    /**
     * 合入统一资源权限
     *
     * @memberof TabExpPanelBase
     */
    computedPanelWithResource(name, mainState) {
        if (!this.$store.getters['authresource/getEnablePermissionValid'])
            return mainState === false ? false : true;
        if (!this.authResourceObject[name])
            return mainState === false ? false : true;
        const resourceAuth = this.appAuthService.getResourcePermission(this.authResourceObject[name]['resourcetag']);
        return !resourceAuth ? false : mainState ? true : false;
    }
}
