import { __decorate } from "tslib";
import { Component } from 'vue-property-decorator';
import { VueLifeCycleProcessing, AppFormBase } from '@ibizstudio-ex/template-vue';
import './memo-form.less';
/**
 * 备忘录表单插件类
 *
 * @export
 * @class MemoForm
 * @extends {Vue}
 */
let MemoForm = class MemoForm extends AppFormBase {
    constructor() {
        super(...arguments);
        this.collapsed = false;
    }
    created() {
        const collapsed = localStorage.getItem('mome-form-collapsed');
        if (collapsed) {
            this.collapsed = JSON.parse(collapsed);
        }
    }
    activated() {
        const collapsed = localStorage.getItem('mome-form-collapsed');
        if (collapsed) {
            this.collapsed = JSON.parse(collapsed);
        }
    }
    collapsedChange() {
        this.collapsed = !this.collapsed;
        localStorage.setItem('mome-form-collapsed', JSON.stringify(this.collapsed));
    }
    renderMemo() {
        if (!this.memo) {
            return null;
        }
        return <div class={{ 'form-memo-area': true }}>
            <div class={{ 'collapse-btn': true }} on-click={() => this.collapsedChange()}>
                <i class='el-icon-caret-bottom'></i>
            </div>
            {this.memo}
        </div>;
    }
    /**
     * @description 自动保存
     * @param {*} [opt={}]
     * @return {*}  {void}
     * @memberof MemoForm
     */
    autoSave(opt = {}) {
        if (!this.formValidateStatus()) {
            return;
        }
        const arg = Object.assign({}, opt);
        const data = this.getData();
        Object.assign(arg, data);
        Object.assign(arg, { srfmajortext: data[this.majorMessageItemName] });
        const action = Object.is(data.srfuf, '1') ? this.updateAction : this.createAction;
        if (!action) {
            this.$throw(`${this.controlInstance.codeName}` + this.$t('app.formpage.notconfig.actionname'), 'autoSave');
            return;
        }
        let tempContext = JSON.parse(JSON.stringify(this.context));
        if (this.context.srfcallbacktype && this.context.srfcallbackid) {
            Object.assign(arg, { srfcallbacktype: this.context.srfcallbacktype, srfcallbackid: this.context.srfcallbackid });
        }
        const post = this.service.add(action, tempContext, arg, this.showBusyIndicator);
        post.then((response) => {
            if (!response.status || response.status !== 200) {
                this.$throw(response, 'autoSave');
                return;
            }
            const data = response.data;
            this.onFormLoad(data, 'autoSave');
            this.ctrlEvent({
                controlname: this.controlInstance.name,
                action: 'save',
                data: data,
            });
            this.$nextTick(() => {
                this.formState.next({ type: 'save', action: 'autoSave', data: data });
            });
        })
            .catch((response) => {
            this.onControlResponse('autoSave', response);
            if (response && response.status && response.data) {
                this.$throw(response, 'autoSave', { dangerouslyUseHTMLString: true });
            }
            else {
                this.$throw(this.$t('app.commonwords.sysexception'), 'autoSave');
            }
        });
    }
    /**
     * 根据detailType绘制对应detail
     *
     * @param {*} modelJson
     * @param {number} index
     * @memberof AppFormBase
     */
    renderByDetailType(modelJson, index) {
        var _a;
        if (modelJson.getPSSysPFPlugin()) {
            const pluginInstance = this.PluginFactory.getPluginInstance('CONTROLITEM', ((_a = modelJson.getPSSysPFPlugin()) === null || _a === void 0 ? void 0 : _a.pluginCode) || '');
            if (pluginInstance) {
                return pluginInstance.renderCtrlItem(this.$createElement, modelJson, this, null);
            }
        }
        else {
            switch (modelJson.detailType) {
                case 'FORMPAGE':
                    return this.renderFormPage(modelJson, index);
                case 'GROUPPANEL':
                    return this.renderGroupPanel(modelJson, index);
                case 'TABPAGE':
                    return this.renderTabPage(modelJson, index);
                case 'TABPANEL':
                    return this.renderTabPanel(modelJson, index);
                case 'FORMITEM':
                    const editor = modelJson.getPSEditor();
                    if (editor) {
                        if (editor.editorStyle === 'noteEditor' && editor.editorType.toLowerCase() === 'textarea') {
                            this.memo = this.renderFormItem(modelJson, index);
                            return null;
                        }
                    }
                    return this.renderFormItem(modelJson, index);
                case 'BUTTON':
                    return this.renderButton(modelJson, index);
                case 'DRUIPART':
                    return this.renderDruipart(modelJson, index);
                case 'RAWITEM':
                    return this.renderRawitem(modelJson, index);
                case 'IFRAME':
                    return this.renderIframe(modelJson, index);
                case 'FORMPART':
                    return this.renderFormPart(modelJson, index);
            }
        }
    }
    /**
     * 绘制内容
     *
     * @returns
     * @memberof AppFormBase
     */
    render() {
        var _a, _b, _c;
        if (!this.controlIsLoaded) {
            return null;
        }
        const { controlClassNames } = this.renderOptions;
        const formId = ((_b = (_a = this.controlInstance.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()) +
            ((_c = this.controlInstance.codeName) === null || _c === void 0 ? void 0 : _c.toLowerCase());
        const controlStyle = {
            width: this.controlInstance.width ? this.controlInstance.width + 'px' : '',
            height: this.controlInstance.height ? this.controlInstance.height + 'px' : ''
        };
        const viewType = this.controlInstance.getParentPSModelObject().viewType;
        return (<i-form props={{ model: this.data }} class={Object.assign(Object.assign({}, controlClassNames), { 'app-form': true, 'memo-form': true, 'memo-mode': this.memo, 'closed': this.collapsed })} ref={this.controlInstance.name} id={formId} style={controlStyle} on-on-validate={this.formItemValidate.bind(this)}>
                <input style='display:none;'/>
                {this.showFormNavBar && this.groupAnchorDatas.length > 0 ? <app-anchor viewType={viewType} anchorDatas={this.groupAnchorDatas}></app-anchor> : null}
                <row>{this.renderFormContent()}</row>
                {this.renderMemo()}
            </i-form>);
    }
};
MemoForm = __decorate([
    Component({}),
    VueLifeCycleProcessing()
], MemoForm);
export { MemoForm };
