import { ControlVOBase, EditFormService, Verify, isEmpty } 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);
    // 记录没有映射的属性
    this.$ownKeys = 
  {{~#each ctrl.psDEFormItems as | formItem | ~}}
    {{#if @first}}[{{/if~}}
    '{{lowerCase formItem.id}}'{{#unless @last}},{{/unless}}
    {{~#if @last}}];{{/if~}}
  {{/each}}
  
  }

  // 表单里映射了属性的字段
  {{#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 {
    controlCodeName: '{{ctrl.codeName}}',
    controlName: '{{ctrl.name}}',
    controlService: new EditFormService<ControlVO>(ControlVO, new {{pascalCase ctrl.psAppDataEntity.codeName}}Service() ),
    data: new ControlVO({}),
    appEntityCodeName: '{{ctrl.psAppDataEntity.codeName}}',
    appDeCodeName:'{{ctrl.psAppDataEntity.codeName}}',
    appDeLogicName: '{{ctrl.psAppDataEntity.logicName}}',
    appDeKeyFieldName: '{{#if ctrl.psAppDataEntity.keyPSAppDEField}}{{ctrl.psAppDataEntity.keyPSAppDEField.codeName}}{{/if}}',
    appDeMajorFieldName: '{{#if ctrl.psAppDataEntity.majorPSAppDEField}}{{ctrl.psAppDataEntity.majorPSAppDEField.codeName}}{{/if}}',
    enableAutoSave: {{ctrl.enableAutoSave}},
    //  错误信息
    errorMessages: [],
    // 关系界面计数器
    drCount: 0,
    //  新建默认值
    createDefaultItems: [
      {{#each ctrl.psDEFormItems as | formItem |}}
        {{#formItem}}
          {{#if (or createDV createDVT)}}
      {
        createDV: "{{createDV}}",
        createDVT: "{{createDVT}}",
        property: "{{lowerCase id}}",
        {{#if (and psEditor psEditor.dateTimeFormat)}}
        valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
        {{else if psAppDEField}}
        valueFormat: "{{psAppDEField.valueFormat}}",
        {{/if}}
        {{#if psAppDEField}}
        dataType: "{{psAppDEField.stdDataType}}"
        {{/if}}
      }{{#unless @last}},{{/unless}}
          {{/if}}
        {{/formItem}}
      {{/each}}
    ],
    //  更新默认值
    updateDefaultItems: [
      {{#each ctrl.psDEFormItems as | formItem |}}
        {{#formItem}}
          {{#if (or updateDV updateDVT)}}
      {
        updateDV: "{{updateDV}}",
        updateDVT: "{{updateDVT}}",
        property: "{{lowerCase id}}",
        {{#if (and psEditor psEditor.dateTimeFormat)}}
        valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
        {{else if psAppDEField}}
        valueFormat: "{{psAppDEField.valueFormat}}",
        {{/if}}
        {{#if psAppDEField}}
        dataType: "{{psAppDEField.stdDataType}}"
        {{/if}}
      }{{#unless @last}},{{/unless}}
          {{/if}}
        {{/formItem}}
      {{/each}}
    ],
    detailsModel: {
    {{#if ctrl.psDEFormPages}}
      {{#each ctrl.psDEFormPages as | FormPage | }}
      {{> @macro/front-end/widgets/form-detail/include-form.hbs type="FORMDETAILSMODEL" item=FormPage}}
      {{/each}}
    {{/if}}
    },
    actionModel: {
      {{#each ctrl.psDEFormPages as | FormPage | }}
      {{> @macro/front-end/widgets/form-detail/include-form.hbs type="FORMACTIONMODEL" item=FormPage}}
      {{/each}}
    },
    rules: {
      {{#each ctrl.psDEFormItemVRs as | ruleItem |}}
      {{ruleItem.psDEFormItemName}}: [
        {{#if (eq ruleItem.valueRuleType 'SYSVALUERULE')}}
        {
          {{#eq ruleItem.psSysValueRule.ruleType 'REG'}}
          trigger: ['change', 'blur'],
          pattern: /{{ruleItem.psSysValueRule.regExCode}}/,
          message:'{{ruleItem.psSysValueRule.ruleInfo}}'
          {{/eq}}
          {{#eq ruleItem.psSysValueRule.ruleType 'SCRIPT'}}
          validator: (rule: any, value: any, callback: any) => {
            if (isEmpty(value)) {
              return Promise.resolve();
            }
            let source: any = { '{{ruleItem.psDEFormItemName}}': value };
            try {
              eval(`{{ruleItem.psSysValueRule.scriptCode}}`);
            } catch (error: any) {
              App.getNotificationService().error({ message: '校验脚本值规则异常' });
            }
            return Promise.resolve();
          }
          {{/eq}}
        },
        {{else if (eq ruleItem.valueRuleType 'DEFVALUERULE')}}
        {
          validator: async (rule: any, value: any, callback: any) => {
            if (isEmpty(value)) {
              return Promise.resolve();
            }
            let source: any = { '{{ruleItem.psDEFormItemName}}': value };
            const { isPast, infoMessage } = Verify.verifyDeRules(
              '{{ruleItem.getPSDEFormItemName}}',
              source,
              {{> @macro/front-end/common/derules.hbs item=ruleItem.psDEFValueRule.getPSDEFVRGroupCondition}}
            );
            if (!isPast) {
              return Promise.reject(infoMessage || '{{ruleItem.psDEFValueRule.ruleInfo}}');
            }
            return Promise.resolve();
          }
        }
        {{/if}}
      ],
      {{/each}}
    },
    // 锚点数据集合
    anchorDatas: [],
  }
};