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

export class ControlVO extends ControlVOBase {
  
  constructor(data: any){
    super(data);
    //  表格自持属性
    this.$ownKeys = 
  {{~#each ctrl.psDEGridDataItems as | dataItem | ~}}
    {{#if @first}}[{{/if~}}
    '{{lowerCase dataItem.name}}'{{#unless @last}},{{/unless}}
    {{~#if @last}}];{{/if~}}
  {{/each}}
  
  }
  {{#each ctrl.psDEGridDataItems as | dataItem |}}
  {{#if dataItem.psAppDEField}}
  get {{lowerCase dataItem.name}}() {
    return this.$DO.{{lowerCase dataItem.psAppDEField.codeName}};
  }
  set {{lowerCase dataItem.name}}(value: any) {
    this.$DO.{{lowerCase dataItem.psAppDEField.codeName}} = value;
  }
  {{/if}}
  {{/each}}

}

export const ctrlState = () => {
  return {
    controlCodeName: '{{ctrl.codeName}}',
    controlName: '{{ctrl.name}}',
    totalColumnWidth: {{ctrl.totalColumnWidth}},
    selectColumnWidth: {{#if (neq ctrl.aggMode 'NONE')}}100{{else}}50{{/if}},
    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}}',
    controlService: new GridService<ControlVO>(ControlVO, new {{pascalCase ctrl.psAppDataEntity.codeName}}Service() ),
    //  新建默认值
    createDefaultItems: [
    {{#each ctrl.psDEGridEditItems as | editItem |}}
    {{#if (and editItem.codeName (or editItem.createDV editItem.createDVT))}}
      {
        createDV: "{{editItem.createDV}}",
        createDVT: "{{editItem.createDVT}}",
        {{#if editItem.dataItemName}}
        property: "{{lowerCase editItem.dataItemName}}",
        {{else}}
        property: "{{lowerCase editItem.codeName}}",
        {{/if}}
        {{#if (and editItem.psEditor editItem.psEditor.dateTimeFormat)}}
        valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
        {{else if editItem.psAppDEField}}
        valueFormat: "{{editItem.psAppDEField.valueFormat}}",
        {{/if}}
        {{#if editItem.psAppDEField}}
        dataType: "{{editItem.psAppDEField.stdDataType}}"
        {{/if}}
      }{{#unless @last}},{{/unless}}
    {{/if}}
    {{/each}}
    ],
    // 错误信息
    errorMessages: [],
    {{#if (and ctrl.psDEDataExport ctrl.psDEDataExport.psDEDataExportItems)}}
    //  数据导出
    exportItems: [
      {{#each ctrl.psDEDataExport.psDEDataExportItems as | exportItem |}}
      {
        field: '{{lowerCase exportItem.psAppDEField.codeName}}',
        {{#if exportItem.psCodeList}}
        codeListTag: '{{exportItem.psCodeList.codeListTag}}',
        {{/if}}
        label: '{{exportItem.caption}}',
        lanResTag: '{{#if (and exportItem.capPSLanguageRes exportItem.capPSLanguageRes.lanResTag)}}{{exportItem.capPSLanguageRes}}{{/if}}',
        show: true
      }{{#unless @last}},{{/unless}}
      {{/each}}
    ],
    {{/if}}
    items: [],
    selections: [],
    selectedRowKeys: [],
    // 多数据部件分组
    mdCtrlGroup: {
      enableGroup: {{ctrl.enableGroup}},
      groupMode: "{{ctrl.groupMode}}",
      groupField: "{{lowerCase ctrl.groupPSAppDEField.codeName}}",
      {{#if ctrl.groupPSCodeList}}
      groupCodeList: {
        codeListTag: "{{ctrl.groupPSCodeList.codeListTag}}",
        codeListType: "{{ctrl.groupPSCodeList.codeListType}}",
      },
      {{/if}}
    },
    // 多数据部件排序
    mdCtrlSort: {
      noSort: {{ctrl.noSort}},
      minorSortDir: "{{#if ctrl.minorSortDir}}{{ctrl.minorSortDir}}{{/if}}",
      minorSortPSDEF: "{{ctrl.minorSortPSAppDEField.codeName}}",
    },
    // 多数据部件分页
    mdCtrlPaging: {
      enablePagingBar: {{ctrl.enablePagingBar}},
      current: 1,
      pageSize: {{#if ctrl.pagingSize}}{{ctrl.pagingSize}}{{else}}20{{/if}},
      pagination: {{#if ctrl.enablePagingBar}}{
        position: ['bottomRight'],
        defaultPageSize: {{#if ctrl.pagingSize}}{{ctrl.pagingSize}}{{else}}20{{/if}},
        showQuickJumper: true,
        showTotal: (total: number, range: IParam[]) => `显示 ${range[0]} - ${range[1]} 条数据，共 ${total} 条数据`,
        pageSizeOptions: ['10','20','50','100'],
      }{{else}}false{{/if}},
    },
    gridAgg: {
      aggMode: "{{ctrl.aggMode}}",
      aggData: [],
      {{#if ctrl.aggPSAppDataEntity}}
      aggEntity: "{{ctrl.aggPSAppDataEntity.codeName}}",
      aggDataSet: "{{ctrl.aggPSAppDEDataSet.codeName}}",
      {{/if}}
    },
    uAColumnModel: {
    {{#each ctrl.psDEGridColumns as | column |}}
    {{> @macro/front-end/widgets/grid-detail/include-grid.hbs type="UACOLUMNMODEL" item=column}}
    {{/each}}
    },
    gridEditState:{
      {{#each ctrl.psDEGridEditItems as | column |}}
      {{#if column.codeName }}
      {{column.codeName}}:{},
      {{/if}}
      {{/each}}
    },
    columnsModel: [
      {{#if ctrl.enableGroup}}
      {{#if ctrl.groupPSAppDEField}}
      {
        title: "分组",
        align: "left",
        columnType: "GROUP",
        width: 100,
        resizable: true,
        ellipsis: true,
        dataIndex: "grid_group",
      },
      {{/if}}
      {{/if}}
      {{!-- 适配表格操作列浮动 --}}
      {{#each ctrl.psDEGridColumns as | column |}}
        {{#unless (eq column.columnType 'UAGRIDCOLUMN')}}
      {{> @macro/front-end/widgets/grid-detail/include-grid.hbs type="COLUMNMODEL" item=column}}
        {{/unless}}
      {{/each}}
      {{#if ctrl.needEmptyColumn}}
      {
        title: "",
        columnType: "EMPTYCOLUMN",
        dataIndex: "autowidthcolumn",
        align: "center",
        resizable: true,
        ellipsis: true,
        aggMode: "NONE",
      },
      {{/if}}
      {{#each ctrl.psDEGridColumns as | column |}}
        {{#if (eq column.columnType 'UAGRIDCOLUMN')}}
      {{> @macro/front-end/widgets/grid-detail/include-grid.hbs type="COLUMNMODEL" item=column}}
        {{/if}}
      {{/each}}
    ],
    // 是否单选
    isSingleSelect:{{#if ctrl.singleSelect}}{{ctrl.singleSelect}}{{else}}false{{/if}},
    //  更新默认值
    updateDefaultItems: [
    {{#each ctrl.psDEGridEditItems as | editItem |}}
    {{#if (and editItem.codeName (or editItem.updateDV editItem.updateDVT))}}
      {
        updateDV: "{{editItem.updateDV}}",
        updateDVT: "{{editItem.updateDVT}}",
        {{#if editItem.dataItemName}}
        property: "{{lowerCase editItem.dataItemName}}",
        {{else}}
        property: "{{lowerCase editItem.codeName}}",
        {{/if}}
        {{#if (and editItem.psEditor editItem.psEditor.dateTimeFormat)}}
        valueFormat: "{{editItem.psEditor.dateTimeFormat}}",
        {{else if editItem.psAppDEField}}
        valueFormat: "{{editItem.psAppDEField.valueFormat}}",
        {{/if}}
        {{#if editItem.psAppDEField}}
        dataType: "{{editItem.psAppDEField.stdDataType}}"
        {{/if}}
      }{{#unless @last}},{{/unless}}
    {{/if}}
    {{/each}}
    ],
    {{#if ctrl.quickToolBarItems}}
    {{#ctrl.quickToolBarItems}}
    quickToolbar: [
      {{#items}}
      { id:'{{id}}',name:'{{name}}',caption:'{{caption}}',groupExtractMode:'{{groupExtractMode}}',itemType:'{{itemType}}',noPrivDisplayMode:'{{noPrivDisplayMode}}',showIcon:{{showIcon}},showCaption:{{showCaption}},tooltip:'{{tooltip}}',disabled: false, visible: true, imgPath: '{{imgPath}}',iconClass: '{{iconClass}}',xDataControlName:'{{xDataControlName}}',{{#if uIAction}}uIAction:{codeName:'{{uIAction.codeName}}',fullCodeName:'{{uIAction.fullCodeName}}',uIActionMode:'{{uIAction.uIActionMode}}',actionTarget:'{{uIAction.actionTarget}}',uIActionTag:'{{uIAction.uIActionTag}}',dataAccessAction:'{{uIAction.dataAccessAction}}',uIActionType:'{{uIAction.uIActionType}}'}{{/if}} },
      {{/items}}
    ],
    {{/ctrl.quickToolBarItems}}
    {{/if}}
    {{#if ctrl.batchToolBarItems}}
    {{#ctrl.batchToolBarItems}}
    batchToolbar: [
      {{#items}}
      { id:'{{id}}',name:'{{name}}',caption:'{{caption}}',groupExtractMode:'{{groupExtractMode}}',itemType:'{{itemType}}',noPrivDisplayMode:'{{noPrivDisplayMode}}',showIcon:{{showIcon}},showCaption:{{showCaption}},tooltip:'{{tooltip}}',disabled: false, visible: true, imgPath: '{{imgPath}}',iconClass: '{{iconClass}}',xDataControlName:'{{xDataControlName}}',{{#if uIAction}}uIAction:{codeName:'{{uIAction.codeName}}',fullCodeName:'{{uIAction.fullCodeName}}',uIActionMode:'{{uIAction.uIActionMode}}',actionTarget:'{{uIAction.actionTarget}}',uIActionTag:'{{uIAction.uIActionTag}}',dataAccessAction:'{{uIAction.dataAccessAction}}',uIActionType:'{{uIAction.uIActionType}}'}{{/if}} },
      {{/items}}
    ],
    {{/ctrl.batchToolBarItems}}
    {{/if}}
    rules:{
      {{#each ctrl.psDEGridEditItemVRs as | ruleItem |}}
      {{ruleItem.psDEGridEditItemName}}: [
        {{#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, source: any) => {
            if (isEmpty(value)) {
              return Promise.resolve();
            }
            try {
              eval(`{{ruleItem.psSysValueRule.scriptCode}}`);
            } catch (error: any) {
              App.getNotificationService().error({ message: '校验脚本值规则异常' });
            }
            return Promise.resolve();
          }
          {{/eq}}
        },
        {{/eq}}
        {{#eq ruleItem.valueRuleType 'DEFVALUERULE'}}
          {{#each ctrl.psDEGridEditItems as | editItem |}}
            {{#eq editItem.codeName ruleItem.psDEGridEditItemName}}
        {
          validator: async (rule: any, value: any, callback: any, source: any) => {
            if (!source || isEmpty(source['{{#if editItem.valueItemName}}{{editItem.valueItemName}}{{else if editItem.dataItemName}}{{editItem.dataItemName}}{{else}}{{ruleItem.psDEGridEditItemName}}{{/if}}'])) {
              return Promise.resolve();
            }
            const { isPast, infoMessage } = Verify.verifyDeRules(
              '{{#if editItem.valueItemName}}{{editItem.valueItemName}}{{else if editItem.dataItemName}}{{editItem.dataItemName}}{{else}}{{ruleItem.psDEGridEditItemName}}{{/if}}',
              source,
              {{> @macro/front-end/common/derules.hbs item=ruleItem.psDEFValueRule.psDEFVRGroupCondition}}
            );
            if (!isPast) {
              return Promise.reject(infoMessage || '{{ruleItem.psDEFValueRule.ruleInfo}}');
            }
            return Promise.resolve();
          }
        }
            {{/eq}}
          {{/each}}
        {{/eq}}
      ],
      {{/each}}
    }
  }
};