import { Component } from 'vue-property-decorator';
import { VueLifeCycleProcessing, EditorBase } from '@ibizstudio-ex/template-vue';
import { ModelTool } from '@ibizstudio-ex/runtime';
import { IPSAppDEField } from '@ibizstudio-ex/runtime';

/**
 * 自定义样式选择器插件类
 *
 * @export
 * @class CustomStyleSelector
 * @extends {EditorBase}
 */
@Component({})
@VueLifeCycleProcessing()
export class CustomStyleSelector extends EditorBase {
    /**
     * 编辑器初始化
     *
     * @memberof CustomStyleSelector
     */
    public async initEditor() {
        await super.initEditor();
        let unitName = this.parentItem?.unitName;
        let appDeField: IPSAppDEField = this.parentItem?.getPSAppDEField();
        this.customProps.type = ModelTool.isNumberField(appDeField) ? 'number' : 'text';
        this.customProps.unit = unitName;
        this.customProps.precision = ModelTool.getPrecision(this.editorInstance, appDeField);
    }

    /**
     * 编辑器change事件
     *
     * @param {*} value
     * @memberof CustomStyleSelector
     */
    public handleChange($event: any) {
        this.editorChange({ name: this.editorInstance.name, value: $event });
    }

    /**
     * 编辑器enter事件
     *
     * @param {*} value
     * @memberof CustomStyleSelector
     */
    public handleEnter($event: any) {
        this.$emit('enter', arguments);
    }

    /**
     * 绘制默认内容
     *
     * @returns {*}
     * @memberof CustomStyleSelector
     */
    public renderTextbox() {
        return this.$createElement('multi-number-editor', {
            props: {
                name: this.editorInstance.name,
                itemValue: this.value,
                disabled: this.disabled,
                ...this.customProps,
            },
            on: {
                change: this.handleChange,
                enter: this.handleEnter,
            },
            style: this.customStyle,
        });
    }

    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof CustomStyleSelector
     */
    public render(): any {
        if (!this.editorIsLoaded) {
            return null;
        }

        return this.renderTextbox();
    }
}
