import { __decorate } from "tslib";
import { notNilEmpty } from 'qx-util';
import { Vue, Component, Emit, Prop } from 'vue-property-decorator';
import './studio-model-editor.less';
/**
 * 模型编辑器
 *
 * @export
 * @class StudioModelEditor
 * @extends {Vue}
 */
let StudioModelEditor = class StudioModelEditor extends Vue {
    constructor() {
        super(...arguments);
        /**
         * 数据是否变更
         *
         * @protected
         * @type {boolean}
         * @memberof StudioModelEditor
         */
        this.isDataChange = false;
        /**
         * 是否正在加载中
         *
         * @protected
         * @type {boolean}
         * @memberof StudioModelEditor
         */
        this.isLoading = false;
        /**
         * 加载中文本
         *
         * @protected
         * @type {string}
         * @memberof StudioModelEditor
         */
        this.loadingText = '加载中...';
        /**
         * 上下文
         *
         * @author chitanda
         * @date 2021-03-31 17:03:52
         * @protected
         * @type {IContext}
         */
        this.context = {};
        /**
         * 模型字符串，用于编辑
         *
         * @protected
         * @type {string}
         * @memberof StudioModelEditor
         */
        this.modelStr = '';
    }
    /**
     * 关闭界面
     *
     * @protected
     * @returns {*}
     * @memberof StudioModelEditor
     */
    close() { }
    /**
     * 表单保存
     *
     * @protected
     * @returns {*}
     * @memberof StudioModelEditor
     */
    saved() { }
    /**
     * 关闭模型编辑
     *
     * @protected
     * @memberof StudioModelEditor
     */
    closeView() {
        if (this.isClose) {
            return;
        }
        if (!this.isDataChange) {
            this.close();
        }
        else {
            this.$Modal.confirm({
                title: '确认退出?',
                content: '修改模型尚未保存，确认退出吗?',
                onOk: () => this.close(),
            });
        }
    }
    /**
     * 组件创建完毕
     *
     * @memberof StudioModelEditor
     */
    created() {
        document.addEventListener('keydown', (e) => {
            if (e.keyCode === 27) {
                this.closeView();
            }
        });
    }
    /**
     * 设置参数
     *
     * @author chitanda
     * @date 2021-03-31 17:03:10
     * @param {IContext} context
     * @param {OpenParams} params
     * @return {*}  {void}
     */
    setParam(context, params) {
        if (this.isClose) {
            return;
        }
        if (params && !Object.is(JSON.stringify(this.param), JSON.stringify(params))) {
            this.context = context;
            this.param = params;
            this.modelStr = '';
            this.loadModel();
        }
    }
    /**
     * 重新加载
     *
     * @protected
     * @memberof StudioModelEditor
     */
    reload() {
        if (this.isDataChange) {
            this.$Modal.confirm({
                title: '确认刷新?',
                content: '修改模型尚未保存，确认刷新吗?',
                onOk: () => this.loadModel(),
            });
        }
        else {
            this.loadModel();
        }
    }
    /**
     * 保存
     *
     * @protected
     * @param {boolean} [isCloseView]
     * @memberof StudioModelEditor
     */
    save(isCloseView) {
        this.saveModel(null, isCloseView);
    }
    /**
     * 加载模型数据
     *
     * @protected
     * @returns {Promise<void>}
     * @memberof StudioModelEditor
     */
    async loadModel() {
        var _a;
        this.showLoading('模型加载中...');
        try {
            const entityName = (_a = this.param) === null || _a === void 0 ? void 0 : _a.entityName;
            if (notNilEmpty(entityName)) {
                const s = await ___ibz___.gs.getService(entityName);
                const model = await s.getLocalModel(this.context, {});
                this.modelStr = JSON.stringify(model, null, 2);
            }
            else {
                this.$Message.error('未指定实体，无法加载模型!');
            }
        }
        catch (err) {
            this.$Message.error(err.message);
        }
        this.hiddenLoading();
    }
    /**
     * 保存模型
     *
     * @protected
     * @param {*} params 保存传递参数
     * @param {boolean} [isCloseView] 保存完成是否关闭当前视图
     * @returns {Promise<void>}
     * @memberof StudioModelEditor
     */
    async saveModel(params, isCloseView) {
        var _a;
        if (!this.modelFormat()) {
            return;
        }
        this.showLoading('模型保存中...');
        try {
            const entityName = (_a = this.param) === null || _a === void 0 ? void 0 : _a.entityName;
            if (notNilEmpty(entityName)) {
                const s = await ___ibz___.gs.getService(entityName);
                await s.setLocalModel(this.context, JSON.parse(this.modelStr));
                this.$emit('saved');
                if (isCloseView) {
                    this.close();
                }
            }
            else {
                this.$Message.error('未指定实体，无法加载模型!');
            }
        }
        catch (err) {
            this.$Message.error(err.message);
        }
        this.hiddenLoading();
    }
    /**
     * 模型变更
     *
     * @protected
     * @param {string} val
     * @memberof StudioModelEditor
     */
    modelChange(val) {
        if (!Object.is(this.modelStr, val)) {
            this.modelStr = val;
            this.isDataChange = true;
        }
    }
    /**
     * 格式化模型
     *
     * @protected
     * @memberof StudioModelEditor
     */
    modelFormat() {
        try {
            this.modelStr = JSON.stringify(JSON.parse(this.modelStr), null, 2);
            return true;
        }
        catch (error) {
            this.$Message.error('格式有误，请检查!');
        }
        return false;
    }
    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof StudioModelEditor
     */
    render() {
        return (<div class='studio-model-editor' v-loading={this.isLoading} element-loading-text={this.loadingText} element-loading-spinner='el-icon-loading' element-loading-background='rgba(0, 0, 0, 0.8)'>
                <div class='studio-model-editor-header'>
                    <div class='title'>模型编辑</div>
                    <div class='actions'>
                        <i-button size='small' title='刷新数据' on-click={() => this.reload()}>
                            刷新
                        </i-button>
                        <i-button size='small' title='格式化JSON数据' on-click={() => this.modelFormat()}>
                            格式化
                        </i-button>
                        <i-button size='small' title='保存数据并关闭界面' on-click={() => this.save(true)}>
                            保存并关闭
                        </i-button>
                        <i-button size='small' class='close' type='text' ghost icon='md-close' on-click={() => this.closeView()}/>
                    </div>
                </div>
                <div class='studio-model-editor-content'>
                    <app-code-editor key='code-content-ftl' code={this.modelStr} on-change={(val) => this.modelChange(val)} codetype='json' height='100%'></app-code-editor>
                </div>
            </div>);
    }
    /**
     * 显示加载动画
     *
     * @protected
     * @param {string} [loadingText]
     * @memberof StudioModelEditor
     */
    showLoading(loadingText) {
        if (loadingText) {
            this.loadingText = loadingText;
        }
        this.isLoading = true;
    }
    /**
     * 隐藏加载动画
     *
     * @protected
     * @memberof StudioModelEditor
     */
    hiddenLoading() {
        this.isLoading = false;
    }
};
__decorate([
    Prop({ default: false })
], StudioModelEditor.prototype, "isClose", void 0);
__decorate([
    Emit('close')
], StudioModelEditor.prototype, "close", null);
__decorate([
    Emit('saved')
], StudioModelEditor.prototype, "saved", null);
StudioModelEditor = __decorate([
    Component({})
], StudioModelEditor);
export { StudioModelEditor };
