import { ModelTool, WFActionViewEngine } from '@ibizstudio/runtime';
import { MainViewBase } from './mainview-base';
/**
 * 工作流操作视图基类
 *
 * @export
 * @class WFActionViewBase
 * @extends {MainViewBase}
 * @implements {WFActionViewInterface}
 */
export class WFActionViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @memberof WFActionViewBase
         */
        this.engine = new WFActionViewEngine();
    }
    /**
     * 初始化编辑视图实例
     *
     * @memberof WFActionViewBase
     */
    async viewModelInit() {
        await super.viewModelInit();
        this.formInstance = ModelTool.findPSControlByName('form', this.viewInstance.getPSControls());
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof WFActionViewBase
     */
    engineInit() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        this.engine.init({
            view: this,
            parentContainer: this.$parent,
            form: this.$refs[this.formInstance.name].ctrl,
            p2k: '0',
            isLoadDefault: true,
            keyPSDEField: this.appDeCodeName.toLowerCase(),
            majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
        });
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof WFActionViewBase
     */
    renderMainContent() {
        if (!this.formInstance) {
            return;
        }
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.formInstance);
        return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: "form", on: targetCtrlEvent });
    }
    /**
     * 确定
     *
     * @memberof WFActionViewBase
     */
    onClickOk() {
        var _a;
        const form = (_a = this.$refs[this.formInstance.name]) === null || _a === void 0 ? void 0 : _a.ctrl;
        if (!form) {
            return;
        }
        form.wfsubmit([Object.assign({}, form.data)]).then((response) => {
            if (!response || response.status !== 200) {
                return;
            }
            this.$store.commit('viewaction/setViewDataChange', { viewtag: this.viewtag, viewdatachange: false });
            this.$emit('view-event', { viewName: this.viewInstance.name, action: 'viewdataschange', data: [Object.assign({}, response.data)] });
            this.$emit('view-event', { viewName: this.viewInstance.name, action: 'close', data: null });
        });
    }
    /**
     * 取消
     *
     * @memberof WFActionViewBase
     */
    onClickCancel() {
        this.$emit('view-event', { viewName: this.viewInstance.name, action: 'close', data: null });
    }
}
