import { DataPanelEngine, EditViewEngine, LogUtil, ModelTool } from '@ibizstudio/runtime';
import { MainViewBase } from './mainview-base';
import { AppCenterService } from '../app-service';
/**
 * 编辑视图基类
 *
 * @export
 * @class EditViewBase
 * @extends {MainViewBase}
 * @implements {EditViewInterface}
 */
export class EditViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof EditViewBase
         */
        this.engine = new EditViewEngine();
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof AccountInfoBase
         */
        this.datapanel = new DataPanelEngine();
    }
    /**
     * 监听视图静态参数变化
     *
     * @memberof EditViewBase
     */
    onStaticPropsChange(newVal, oldVal) {
        if (newVal.panelState) {
            this.panelState = newVal.panelState;
        }
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
     * 视图初始化
     *
     * @memberof EditViewBase
     */
    viewInit() {
        super.viewInit();
        if (this.panelState) {
            this.panelStateStateEvent = this.panelState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.viewInstance.name)) {
                    return;
                }
                if (action == 'save') {
                    this.viewState.next({ tag: this.editFormInstance.name, action: 'save', data: data });
                }
                if (action == 'remove') {
                    this.viewState.next({ tag: this.editFormInstance.name, action: 'remove', data: data });
                }
            });
        }
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof EditViewBase
     */
    engineInit() {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        this.engine.init({
            view: this,
            parentContainer: this.$parent,
            form: this.$refs[this.editFormInstance.name].ctrl,
            p2k: '0',
            isLoadDefault: this.viewInstance.loadDefault,
            keyPSDEField: this.appDeCodeName.toLowerCase(),
            majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
        });
        if (this.dataPanelInstance) {
            this.datapanel.init({
                view: this,
                parentContainer: this.$parent,
                datapanel: this.$refs[(_a = this.dataPanelInstance) === null || _a === void 0 ? void 0 : _a.name].ctrl,
                p2k: '0',
                isLoadDefault: this.viewInstance.loadDefault,
                keyPSDEField: this.appDeCodeName.toLowerCase(),
                majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
            });
        }
    }
    /**
     * 初始化编辑视图实例
     *
     * @memberof EditViewBase
     */
    async viewModelInit() {
        await super.viewModelInit();
        this.editFormInstance = ModelTool.findPSControlByName('form', this.viewInstance.getPSControls());
        this.dataPanelInstance = ModelTool.findPSControlByName('datapanel', this.viewInstance.getPSControls());
    }
    /**
     * 渲染视图标题头信息表单部件
     *
     * @memberof EditViewBase
     */
    renderDataPanelInfo() {
        var _a;
        if (!this.dataPanelInstance) {
            return null;
        }
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.dataPanelInstance);
        return this.$createElement(targetCtrlName, { slot: 'datapanel', props: targetCtrlParam, ref: (_a = this.dataPanelInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof EditViewBase
     */
    renderMainContent() {
        var _a;
        if (!this.editFormInstance) {
            return null;
        }
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.editFormInstance);
        return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: (_a = this.editFormInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
    /**
     * 将抄送任务标记为已读
     *
     * @param data 业务数据
     * @memberof EditViewBase
     */
    readTask(data) {
        this.appEntityService
            .ReadTask(this.context, data)
            .then((response) => {
            if (!response || response.status !== 200) {
                LogUtil.warn(this.$t('app.editor.error'));
                return;
            }
            AppCenterService.notifyMessage({ name: this.appDeCodeName, action: 'appRefresh', data: data });
        })
            .catch((error) => {
            LogUtil.warn(this.$t('app.editor.error'));
        });
    }
    /**
     *  视图销毁
     *
     * @memberof EditViewBase
     */
    viewDestroyed() {
        super.viewDestroyed();
        if (this.panelStateStateEvent) {
            this.panelStateStateEvent.unsubscribe();
        }
    }
}
