import type { VxeGridPropTypes, VxeGridProps } from 'vxe-table/types/grid';
import apiService from '@/api-service/index';
<%_ if (fields.find(field => field.fieldIsEnum)) { _%>
  <%_ if (enableTranslation) { _%>
import { useI18n } from '@/hooks/web/useI18n';
  <% } else { _%>
import { allEnumsDict } from '@/models/enumerations/all-enums';
  <%_ } _%>
<%_ } _%>
<%_
const { tableColumns, searchFormFields, tableImportStatements } = (bcEntityData || {tableColumns: [], searchFormFields: [], tableImportStatements: []});
_%>
<%_ if (tableImportStatements && tableImportStatements.size) { _%>
<%- Array.from(tableImportStatements).join('\n') %>
<%_ } _%>

<%_
let apiModuleName = this._.camelCase(clientRootFolder || microserviceName);
const prefixApi = apiModuleName ? apiModuleName + '.' : '';
_%>
<%_ if (fields.some(field => field?.options?.sortField)) { _%>
const <%= entityInstance %>Service = apiService.<%= prefixApi %><%= entityInstance %>Service;
<%_ } _%>
const relationshipApis: any = {
<%_ for (relationship of relationships) {
    const relationApiModuleName = this._.camelCase(relationship.otherEntity.clientRootFolder || relationship.otherEntity.microserviceName);
_%>
  <%= relationship.relationshipName %>: apiService<%= relationApiModuleName ?  '.' + relationApiModuleName : ''%>.<%= relationship.otherEntity.entityInstance%>Service.<%= relationship.otherEntity.fieldsContainParentAndChildren ? 'tree' : 'retrieve'%>,
<%_ } _%>
};
<%_
const sortFields = fields.filter(field => field?.options?.sortField);
_%>

// begcode-please-regenerate-this-file 如果您不希望重新生成代码时被覆盖，将please修改为don't ！！！-->

const searchForm = (): any[] => {
<%_ if (fields.find(field => field.fieldIsEnum)) { _%>
  <%_ if (enableTranslation) { _%>
  const { getEnumDict } = useI18n();
  <%_ } _%>
<%_ } _%>
  return [
    <%_
      searchFormFields.forEach(column => {
  _%>
  <%- column _%>
    <%_ });_%>
  ];
};

const columns = (): VxeGridPropTypes.Columns => {
<%_ if (fields.find(field => field.fieldIsEnum)) { _%>
  <%_ if (enableTranslation) { _%>
  const { getEnumDict } = useI18n();
  <%_ } _%>
<%_ } _%>
  return [
  <%_
        tableColumns.forEach(column => {
  _%>
  <%- column _%>
    <%_ });_%>
  ];
}

const baseGridOptions = (ajax, toolbarButtons, toolbarTools, pagerLeft): VxeGridProps => {
  return {
    rowConfig: {
      keyField: '<%= primaryKey.name %>',
      isHover: true,
      <%_ if (listBy === 'tableEdit') { _%>
      isCurrent: true,
      <%_ } _%>
    },
    loading: undefined,
    border: true,
    showHeaderOverflow: true,
    showOverflow: true,
    keepSource: true,
    id: 'vxe_grid_<%= entityTableName %>',
    height: 600,
    printConfig: {
      columns: [
        // { field: 'name' },
      ],
    },
    filterConfig: {
      remote: true,
    },
    columnConfig: {
      resizable: true,
    },
    sortConfig: {
      trigger: 'cell',
      remote: true,
      orders: ['asc', 'desc', null],
      multiple: true,
      showIcon: true,
      defaultSort: {
        field: '<%= sortFields && sortFields.length ? sortFields[0].fieldName : primaryKey.name %>',
        order: '<%= sortFields && sortFields.length ? sortFields[0].sortField : 'desc' %>',
      },
    },
    pagerConfig: {
      layouts: ['Sizes', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'FullJump', 'Total'],
      pageSize: 15,
      pageSizes: [5, 10, 15, 20, 30, 50],
      total: 0,
      pagerCount: 5,
      currentPage: 1,
      autoHidden: true,
      slots: {
        left: pagerLeft,
      },
    },
    importConfig: {
      remote: true,
      importMethod: ajax.import,
    },
    exportConfig: {
      columnFilterMethod: ({column}) => ['radio', 'checkbox'].includes(column.type),
    },
    checkboxConfig: {
      // labelField: 'id',
      reserve: true,
      highlight: true,
    },
    editRules: {},
    editConfig: {
      trigger: 'click',
      mode: 'cell',
      showStatus: true,
    },
    customConfig: {
      storage: {
        visible: true,
        resizable: false,
        sort: true,
        fixed: true,
      },
    },
    proxyConfig: {
      enabled: true,
      autoLoad: true,
      seq: true,
      sort: true,
      filter: true,
      response: {
        result: 'records',
        total: 'total',
      },
      ajax,
    },
    toolbarConfig: {
      custom: false,
      import: false,
      print: false,
      export: false,
      slots: {
        buttons: 'toolbar_buttons',
      },
      buttons: toolbarButtons,
      tools: toolbarTools,
    },
<%_ if (fieldsContainParentAndChildren) { _%>
    treeConfig: {
      childrenField: 'children',
      indent: 20,
      showLine: false,
      expandAll: false,
      accordion: false,
      trigger: 'default',
    },
<%_ } _%>
  };
};

const ListProps = {
  query: {
    type: Object,
    default: () => ({}),
  },
  editIn: {
    ype: String,
    default: '<%= editInContainer || 'page' %>',
  },
  baseData: {
    type: Object,
    default: () => ({}),
  },
  cardExtra: {
    type: Array,
    default: ['import', 'export', 'print'<% if (softDelete && !skipSoftDelete) { %>, 'recycleBin'<% } %>],
  },
  gridOptions: {
    type: Object,
    default: () => ({}),
  },
  selectType: {
    type: String,
    default: 'checkbox', // checkbox/radio/none/seq
  },
  searchFormOptions: {
    type: Object,
    default: () => ({
      disabled: false,
    }),
  },
  gridCustomConfig: {
    type: Object,
    default: () => ({
      hideOperations: false,
      hideSlots: [],
      hideColumns: [],
    }),
  },
};

export default {
  searchForm,
  columns,
  baseGridOptions,
  ListProps,
}
