import { ControlVOBase, EditFormService } from '@core';
import { {{pascalCase ctrl.psAppDataEntity.codeName}}Service } from '@api/{{spinalCase ctrl.psAppDataEntity.codeName}}/{{spinalCase ctrl.psAppDataEntity.codeName}}-service';

/**
 * 部件展示数据对象
 * @export
 * @class ControlVO
 */
export class ControlVO extends ControlVOBase {
  /**
   * 用后台数据对象创建部件数据对象
   * @param data 后台数据
   */
  constructor(data: any){
    super(data);
    // 记录没有映射的属性
    {{#if ctrl.psDEFormItems}}
    this.$ownKeys = 
  {{~#each ctrl.psDEFormItems as | formItem | ~}}
    {{#if @first}}[{{/if~}}
    '{{lowerCase formItem.id}}'{{#unless @last}},{{/unless}}
    {{~#if @last}}];{{/if~}}
  {{/each}}
  {{/if}}
  
  }

  // 表单里映射了属性的字段
  {{#each ctrl.psDEFormItems as | formItem | }}
    {{!-- TODO: 表单formItem的name拿不到 --}}
    {{#neq formItem.psAppDEField null }}
  get {{lowerCase formItem.id}}() {
    return this.$DO.{{lowerCase formItem.psAppDEField.codeName}};
  }
  set {{lowerCase formItem.id}}(value: any) {
    this.$DO.{{lowerCase formItem.psAppDEField.codeName}} = value;
  }

    {{/neq}}
  {{/each}}

  // 表单里没有映射实体属性的字段(srfuf除外)
  {{#each ctrl.psDEFormItems as | formItem | }}
    {{#if (and (eq formItem.psAppDEField null) (neq formItem.id "srfuf" )) }}
  {{lowerCase formItem.id}}: any;
    {{/if}}
  {{/each}}
}

// 部件配置对象
export const ctrlState = () => {
  return {
    autoSearch: {{ctrl.enableAutoSearch}},
    controlCodeName: '{{ctrl.codeName}}',
    controlName: '{{ctrl.name}}',
    controlService: new EditFormService<ControlVO>(ControlVO, new {{pascalCase ctrl.psAppDataEntity.codeName}}Service() ),
    data: new ControlVO({}),
    showPopover: false,
    selectHistoryItem: {},
    historyItems: [],
    detailsModel: {
    {{#if ctrl.psDEFormPages}}
      {{#each ctrl.psDEFormPages as | FormPage | }}
        {{> @macro/front-end/widgets/form-detail/include-form.hbs type="FORMDETAILSMODEL" item=FormPage}}
      {{/each}}
    {{/if}}
    },
    rules: {
      {{#each ctrl.psDEFormEditItemVRs as | ruleItem |}}
      {{#neq ruleItem 2}}
        {{#if ruleItem.valueRuleType 'DEFVALUERULE'}}
        {{psDEFormEditItemName}}: [
        {{#each ruleItem.psDEFVRGroupCondition as | condition |}}
        {trigger: ['change', 'blur'],
        validator: (_rule: RuleObject, value: string) => verifyRules(_rule,value,{
          {{#condition.condType}}type: '{{condition.condType}}',{{/condition.condType}}
          {{#condition.name}}name: '{{condition.name}}',{{/condition.name}}
          {{#condition.maxValue}}maxValue: '{{condition.maxValue}}',{{/condition.maxValue}}
          {{#condition.minValue}}minValue: '{{condition.minValue}}',{{/condition.minValue}}
          {{#condition.ruleInfo}}ruleInfo: '{{condition.ruleInfo}}',{{/condition.ruleInfo}}
          {{#condition.includeMaxValue}}includeMaxValue: '{{condition.includeMaxValue}}',{{/condition.includeMaxValue}}
          {{#condition.includeMinValue}}includeMinValue: '{{condition.includeMinValue}}',{{/condition.includeMinValue}}
        }) }
        {{/each}}
        ]
        {{/if}}
      {{/neq}}
      {{/each}}
    },
  }
};