import { MPickUpViewBase } from '../../../view/mpickupview-base';
import { AppLayoutService } from '../../../app-service/common-service/app-layout-service';
import { Watch } from '../../../decorators';

/**
 * 应用数据多项选择视图基类
 *
 * @export
 * @class AppMPickUpViewBase
 * @extends {MPickUpViewBase}
 */
export class AppMPickUpViewBase extends MPickUpViewBase {
  /**
   * 视图参数变化
   *
   * @param {*} newVal
   * @param {*} oldVal
   * @memberof AppMPickUpViewBase
   */
  @Watch('viewparam', { immediate: true, deep: true })
  on_viewparams_change(newVal: any, oldVal: any) {
    if (newVal) {
      if (this.viewparams.selectedData) {
        this.selectedData = JSON.stringify(this.viewparams.selectedData);
        this.viewSelections = this.viewparams.selectedData;
      }
    }
  }

  /**
   * 初始化containerModel
   *
   * @memberof AppMPickUpViewBase
   */
  initContainerModel(opts: any) {
    super.initContainerModel(opts);
    const { modeldata } = opts;
    modeldata?.getPSControls().forEach((ctrl: any) => {
      this.containerModel[`view_${ctrl.name}`] = { name: `${ctrl.name}`, type: `${ctrl.controlType}` };
    });
  }

  /**
   * 视图渲染
   *
   * @memberof AppMPickUpViewBase
   */
  render(h: any) {
    if (!this.viewIsLoaded) {
      return null;
    }
    const targetViewLayoutComponent: any = AppLayoutService.getLayoutComponent(`${this.viewInstance?.viewType}-${this.viewInstance?.viewStyle}`);
    return h(
      targetViewLayoutComponent,
      {
        props: { viewInstance: this.viewInstance, model: this.model, modelService: this.modelService, viewparams: this.viewparams, context: this.context },
      },
      [this.renderTopMessage(), this.renderBodyMessage(), this.renderToolBar(), this.renderMainContent(), this.renderBottomMessage()],
    );
  }
}
