<%_
const oneToOneNotOwnerSide =[];
let hasUseJPADerivedIdentifier = false;
const { formFieldLines, showInOneRelationships, showInOneImportComponents } = (bcEntityData || { formFieldLines: [], showInOneRelationships: [], showInOneImportComponents: {}});
const showInOneEditByTable = showInOneRelationships.filter(rel => {
  if (rel.source && rel.source.annotationOnSource && rel.source.annotationOnSource.includes('editByList')) {
    return false;
  }
  if (rel.source && rel.source.annotationOnSource && rel.source.annotationOnSource.includes('editByFormList')) {
    return false;
  }
  return !(rel.source && rel.source.annotationOnSource && rel.source.annotationOnSource.includes('editByDescList'));
});
let importStatements = [];
if (showInOneEditByTable && showInOneEditByTable.length) {
  showInOneEditByTable.forEach(rel => {
    const sortFields = rel.otherEntity.fields.filter(field => field.options && field.options.sortField);
    if (sortFields && sortFields.length) {
      importStatements.push(`import Sortable from 'sortablejs';`);
      importStatements.push(`import { VxeTableInstance } from 'vxe-table';`);
    }
  });
}
_%>
<template>
  <PageWrapper v-bind="pageProps">
    <template #default>
        <<%= entityClass %>Form ref="<%= entityInstance %>FormRef" v-bind="formProps"></<%= entityClass %>Form>
    </template>
    <template #rightFooter>
      <Space>
        <Button
          v-for="operation in operations"
          :type="operation.type"
          @click="operation.click"
          <%_ if (cypressTests) { _%>
          v-bind="operation.attrs"
          <%_ } _%>
        >
          <Icon icon="operation.icon" v-if="operation.icon"/>
          {{ operation.title }}
        </Button>
      </Space>
    </template>
  </PageWrapper>

</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Space } from 'ant-design-vue';
import <%= entityClass %>Form from './components/form-component.vue';
import { Button, Icon, PageWrapper } from '@begcode/components';
import { useGo } from '@/hooks/web/usePage';
import { useMultipleTabStore } from '@/store/modules/multipleTab';
<%_ if (showInOneImportComponents && Object.keys(showInOneImportComponents).length) { _%>
  <%_ Object.values(showInOneImportComponents).forEach(importLine => { _%>
<%- importLine %>
  <%_ });_%>
<%_  } _%>
<%_ if (importStatements && importStatements.length) { _%>
  <%_ importStatements.forEach(importLine => { _%>
<%- importLine %>
<%_ });} _%>
<%_ if (addCustomMethod.indexOf('updateTemplate') !== -1) { _%>
  <%- include(`../common/entity-custom/${entityClass}.updateTemplate.ejs`, {position: 'import'}); -%>
<%_ } _%>
<%_ let apiModuleName = this._.camelCase(clientRootFolder || microserviceName); _%>

  defineOptions({
    name: '<%= this._.upperFirst((moduleName || clientRootFolder) ? (moduleName || clientRootFolder) + entityAngularName : entityAngularName) %>Edit',
    inheritAttrs: false,
  });

  const props = defineProps({
    baseData: {
      type: Object,
      default: () => ({}),
    },
  });

  const route = useRoute();
  const router = useRouter();
  const go = useGo();
  const tabStore = useMultipleTabStore();

  const <%= entityInstance%>FormRef = ref<any>(null);
  const pageProps = reactive({
    title: '编辑',
  });
  let <%= entityInstance %>Id = ref(route.params?.entityId as string);

  <%_ if (addCustomMethod.indexOf('updateTemplate') !== -1) { _%>
  <%- include(`../common/entity-custom/${entityClass}.updateTemplate.ejs`, {position: 'pageField'}); -%>
  <%_ } _%>

  const saveOrUpdate = async () => {
    const result = await <%= entityInstance %>FormRef.value.submit();
    if (result) {
      if (!<%= entityInstance %>Id.value) {
        const { fullPath } = route;
        tabStore.closeTabByKey(fullPath, router).then(() => {
          <%_ const moduleNameOrClientRootFolder = clientRootFolder; _%>
          go('/<%= moduleNameOrClientRootFolder ? (moduleNameOrClientRootFolder + '/') : '' %><%= entityUrl %>/' + result.id + '/edit', true);
        });
      }
    }
  };

  const formProps = reactive({
    entityId: <%= entityInstance %>Id.value,
    baseData: props.baseData,
  });
  const operationsConfig = ref<any>([
    {
      title: '关闭',
      type: 'default',
      name: 'close',
      skipValidate: true,
      click: async () => {
        const { fullPath } = route;
        await tabStore.closeTabByKey(fullPath, router);
      },
      <%_ if (cypressTests) { _%>
      attrs: {
        'data-cy': 'entityCreateCancelButton',
      }
      <%_ } _%>
    },
    {
      hide: () => {
        return !!<%= entityInstance %>Id.value;
      },
      title: <%= entityInstance %>Id.value ? '更新' : '保存',
      icon: '',
      type: 'primary',
      name: 'save',
      click: saveOrUpdate,
      <%_ if (cypressTests) { _%>
      attrs: {
        'data-cy': 'entityCreateSaveButton',
      }
      <%_ } _%>
    },
  ]);
const operations = computed(() => {
  return operationsConfig.value.map((operation: any) => {
    return {
      ...operation,
      hide: operation.hide ? operation.hide() : false,
    };
  }).filter((operation: any) => !operation.hide);
});
<%_ if (addCustomMethod.indexOf('updateTemplate') !== -1) { _%>
<%- include(`../common/entity-custom/${entityClass}.updateTemplate.ejs`, {position: 'pageMethod'}); -%>
<%_ } _%>
</script>
