import { __decorate } from "tslib";
import { getControl, getPSUIActionByModelObject } from '@ibizstudio-ex/runtime';
import { Util } from '@ibizstudio-ex/runtime';
import { Vue, Prop } from 'vue-property-decorator';
import './design-view-base2.less';
/**
 * 设计视图基础
 *
 * @author chitanda
 * @date 2021-04-14 11:04:33
 * @export
 * @class DesignViewBase
 * @extends {Vue}
 */
export class DesignViewBase2 extends Vue {
    constructor() {
        super(...arguments);
        /**
         * 显示素材区
         *
         * @type {boolean}
         * @memberof DesignViewBase
         */
        this.showMaterial = true;
    }
    /**
     * Vue生命周期，实例创建完成
     *
     * @memberof FormDesign
     */
    created() {
        var _a;
        this.fillCtx();
        this.toolBarModel = getControl(this.viewModel, 'toolbar');
        this.propertyView = (_a = this.viewModel.findPSAppViewRef('PROPERTYVIEW')) === null || _a === void 0 ? void 0 : _a.getRefPSAppView();
    }
    /**
     * 填充设计上下文
     *
     * @memberof FormDesign
     */
    fillCtx() {
        if (this.context) {
            Object.assign(this.ctx.context, this.context);
            delete this.ctx.context.srfsessionid;
        }
    }
    async save() { }
    /**
     * 工具栏点击
     *
     * @memberof FormDesign
     */
    async handleItemClick(data, $event) {
        const targetViewLogic = this.viewModel.findPSAppViewLogic(`toolbar_${data.tag}_click`);
        const action = targetViewLogic.getPSAppViewUIAction();
        const uIAction = await getPSUIActionByModelObject(action);
        const tag = uIAction.uIActionTag.toLowerCase();
        if (Object.is(tag, 'save')) {
            await this.saveAction();
        }
        else if ((tag === null || tag === void 0 ? void 0 : tag.indexOf('modeledit')) > -1) {
            if (await this.saveAction()) {
                await this.viewModel.fill(true);
                const entity = this.viewModel.getPSAppDataEntity();
                this.context.viewpath = 'PSSYSAPPS/FormDesign/PSAPPDEVIEWS/PSSystemModelEdit.json';
                this.context.srfdename = entity === null || entity === void 0 ? void 0 : entity.codeName.toLowerCase();
                const view = {
                    viewname: 'app-view-shell',
                    title: this.viewModel.caption,
                    placement: 'DRAWER_TOP',
                };
                let container = this.$appdrawer.openDrawer(view, Util.getViewProps(this.context, data));
                container.subscribe(async (result) => {
                    if (!result || !Object.is(result.ret, 'OK')) {
                        return;
                    }
                    await this.ctx.ctrl.reload();
                    this.ctx.evt.emit('reload');
                });
            }
        }
        else {
            // AppViewLogicService.getInstance().executeViewLogic(
            //     `${data.tag}_click`,
            //     null,
            //     this.ctx.context,
            //     {},
            //     viewlogicData.getPSAppViewLogics!,
            // );
        }
    }
    async saveAction() {
        const loading = this.$loading({
            lock: true,
            text: '保存中...',
            background: '#ffffffe6',
        });
        try {
            const res = await this.save();
            if (res.status === 200) {
                this.$success({
                    content: '保存成功',
                });
                loading.close();
                return true;
            }
            else {
                this.$throw(res);
            }
        }
        catch (error) {
            this.$throw(error);
        }
        loading.close();
        return false;
    }
    renderToolBar() {
        var _a;
        if (!this.toolBarModel) {
            return null;
        }
        return (<design-view-toolbar slot='toolbar' mode={((_a = this.viewModel) === null || _a === void 0 ? void 0 : _a.viewStyle) || 'DEFAULT'} toolbarModels={this.toolBarModel.getPSDEToolbarItems()} on-item-click={(data, $event) => {
                this.handleItemClick(data, $event);
            }}></design-view-toolbar>);
    }
    /**
     * 绘制素材区
     *
     * @author chitanda
     * @date 2021-04-14 11:04:26
     * @return {*}  {*}
     */
    renderMaterial() { }
    /**
     * 绘制结构树
     *
     * @author chitanda
     * @date 2021-04-14 11:04:31
     * @return {*}  {*}
     */
    renderStructureTree() {
        return <ibz-design-structure-tree ctx={this.ctx}/>;
    }
    /**
     * 绘制内容区
     *
     * @author chitanda
     * @date 2021-04-14 11:04:37
     * @return {*}  {*}
     */
    renderContent() {
        return <ibz-design-default-content ctx={this.ctx} childComponent='ibz-design-page'/>;
    }
    renderHome() { }
    render(h) {
        var _a;
        return (<div class='design-view2'>
                <div class='design-view-header'>
                    <div class='title'>{this.ctx.mainInfo || ((_a = this.viewModel) === null || _a === void 0 ? void 0 : _a.title)}</div>
                    <div class='home'>{this.renderHome()}</div>
                    {this.renderToolBar()}
                </div>
                <div class='design-view-content'>
                    <design-container showMaterial={this.showMaterial}>
                        <template slot='material'>{this.renderMaterial()}</template>
                        <template slot='structureTree'>{this.renderStructureTree()}</template>
                        <template slot='content'>{this.renderContent()}</template>
                        <template slot='attributes'>
                            {this.propertyView
                ? h('app-view-shell', {
                    props: {
                        staticProps: {
                            designCtx: this.ctx,
                            viewDefaultUsage: false,
                        },
                        dynamicProps: {
                            viewparam: {},
                            viewdata: JSON.stringify(Object.assign(Object.assign({}, this.ctx.context), { viewpath: this.propertyView.modelFilePath })),
                        },
                    },
                })
                : null}
                        </template>
                    </design-container>
                </div>
            </div>);
    }
}
__decorate([
    Prop()
], DesignViewBase2.prototype, "context", void 0);
__decorate([
    Prop()
], DesignViewBase2.prototype, "params", void 0);
__decorate([
    Prop()
], DesignViewBase2.prototype, "viewModel", void 0);
__decorate([
    Prop()
], DesignViewBase2.prototype, "ctx", void 0);
