<%
var ulc = isBlank(prefix) ? table.name : subAfter(table.name, prefix);
var lcc = toCamelCase(ulc);
var ucc = upperFirst(lcc);
var dc = toDashCase(lcc);

var pageVueName = ucc + ".vue";
var formVueName = ucc + "Form.vue";
var pageTsName = "page.ts";
var apiTsName = dc + "-api.ts";
var dTsName = dc + ".d.ts";
%>
<template>
  <AFormPanel
      ref="formPanel"
      v-bind="formPanelProps">
    <template #default="formData">
         <div class="form-items">
      <%
      for(column in table.columns) {
          var map = tsType(column.dataType);
      %>
       <ElFormItem label="${column.comment}" prop="${toCamelCase(column.name)}">
        <ElInput placeholder="${column.comment}" v-model="formData.${toCamelCase(column.name)}" />
      </ElFormItem>
     <%}%>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import ${ucc}Api from '@/pages/${moduleName}<%if(subModuleName != null){%>/${subModuleName}<%}%>/${apiTsName}'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import Strings from '@/common/utils/strings.ts'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<${ucc}Types.Search${ucc}Result>({
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve()
    } else {
      status.value = 'modify'
      return ${ucc}Api
        .detail(id!)
        .then(res => res.data)
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return ${ucc}Api
      .add(data)
        .then(props.research)
    } else {
      return ${ucc}Api
      .modify(data)
        .then(props.research)
    }
  },
  rules: {
         <%
         for(column in table.columns) {
         %>
         ${toCamelCase(column.name)}: [{ required: true, message: '请填写${column.comment}', trigger: 'blur' }],
         <%}%>
         },
})
watchEffect(() => {
  formPanelProps.title = status.value === 'add' ? '新建${table.comment}' : '修改${table.comment}信息'
})
defineExpose({
  open(data?: ${ucc}Types.Search${ucc}Result) {
    formPanelIns.value?.open(data?.id)
  },
})

</script>

<style lang="stylus" scoped>
.form-items {
  grid-template-columns: 1fr 1fr;
}
</style>
