import { PickupViewEngine, ModelTool, throttle } from '@ibizstudio/runtime';
import { MainViewBase } from './mainview-base';
/**
 * 数据选择视图基类
 *
 * @export
 * @class PickupViewBase
 * @extends {MainViewBase}
 * @implements {PickupViewInterface}
 */
export class PickupViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof PickupViewBase
         */
        this.engine = new PickupViewEngine();
        /**
         * 选中数据的字符串
         *
         * @type {string}
         * @memberof PickupViewBase
         */
        this.selectedData = "";
        /**
         * 视图选中数据
         *
         * @type {any[]}
         * @memberof PickupViewBase
         */
        this.viewSelections = [];
        /**
         * 是否显示按钮
         *
         * @type {boolean}
         * @memberof PickupViewBase
         */
        this.isShowButton = true;
    }
    /**
     * 监听部件动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PanelControlBase
     */
    onDynamicPropsChange(newVal, oldVal) {
        var _a;
        super.onDynamicPropsChange(newVal, oldVal);
        if ((_a = this.viewparams) === null || _a === void 0 ? void 0 : _a.selectedData) {
            this.selectedData = JSON.stringify(this.viewparams.selectedData);
        }
    }
    /**
     * 监听部件静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PickupViewBase
     */
    onStaticPropsChange(newVal, oldVal) {
        this.isShowButton = (newVal === null || newVal === void 0 ? void 0 : newVal.isShowButton) !== false;
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
     * 引擎初始化
     *
     * @public
     * @memberof PickupViewBase
     */
    engineInit() {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        this.engine.init({
            view: this,
            parentContainer: this.$parent,
            pickupViewPanel: this.$refs[(_a = this.pickUpViewPanelInstance) === null || _a === void 0 ? void 0 : _a.name].ctrl,
            keyPSDEField: this.appDeCodeName.toLowerCase(),
            majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
        });
    }
    /**
     * 确定
     *
     * @memberof PickupViewBase
     */
    onClickOk() {
        this.$emit('view-event', { viewName: this.viewInstance.name, action: 'viewdataschange', data: this.viewSelections });
        this.$emit('view-event', { viewName: this.viewInstance.name, action: 'close', data: null });
    }
    /**
     * 取消
     *
     * @memberof PickupViewBase
     */
    onClickCancel() {
        this.$emit('view-event', { viewName: this.viewInstance.name, action: 'viewdataschange', data: null });
        this.$emit('view-event', { viewName: this.viewInstance.name, action: 'close', data: null });
    }
    /**
     *  视图挂载
     *
     * @memberof PickupViewBase
     */
    async viewMounted() {
        var _a;
        super.viewMounted();
        if ((_a = this.viewparams) === null || _a === void 0 ? void 0 : _a.selectedData) {
            this.engine.onCtrlEvent('pickupviewpanel', 'selectionchange', this.viewparams.selectedData);
        }
    }
    /**
     * 初始化数据选择视图实例
     *
     * @memberof PickupViewBase
     */
    async viewModelInit() {
        var _a;
        this.viewInstance = ((_a = this.staticProps) === null || _a === void 0 ? void 0 : _a.modeldata);
        await super.viewModelInit();
        this.pickUpViewPanelInstance = ModelTool.findPSControlByType("PICKUPVIEWPANEL", this.viewInstance.getPSControls());
    }
    /**
     * 计算目标部件所需参数
     *
     * @param {string} [controlType]
     * @returns
     * @memberof PickupViewBase
     */
    computeTargetCtrlData(controlInstance) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.dynamicProps, {
            selectedData: this.selectedData,
        });
        Object.assign(targetCtrlParam.staticProps, {
            isSingleSelect: true,
            isShowButton: this.isShowButton,
        });
        return { targetCtrlName, targetCtrlParam, targetCtrlEvent };
    }
    /**
     * 渲染选择视图面板
     *
     * @memberof PickupViewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.pickUpViewPanelInstance);
        return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: (_a = this.pickUpViewPanelInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
    /**
     * 渲染选择视图按钮
     *
     * @memberof PickupViewBase
     */
    renderPickButton() {
        var _a, _b, _c, _d;
        if (this.isShowButton) {
            return (<card dis-hover={true} bordered={false} class="footer">
                    <row style={{ "textAlign": 'right' }}>
                        <i-button type="primary" disabled={this.viewSelections.length > 0 ? false : true} on-click={(...params) => throttle(this.onClickOk, params, this)}>{(_b = (_a = this.containerModel) === null || _a === void 0 ? void 0 : _a.view_okbtn) === null || _b === void 0 ? void 0 : _b.text}</i-button>
                            &nbsp;&nbsp;
                        <i-button on-click={(...params) => throttle(this.onClickCancel, params, this)}>{(_d = (_c = this.containerModel) === null || _c === void 0 ? void 0 : _c.view_cancelbtn) === null || _d === void 0 ? void 0 : _d.text}</i-button>
                    </row>
                </card>);
        }
    }
}
