<script setup lang="ts">
import type { Recordable } from '@easyflow/types';

import type { EasyFlowFormSchema } from '@easyflow-core/form-ui';

import { computed, reactive } from 'vue';

import { useEasyFlowForm } from '@easyflow-core/form-ui';
import { EasyFlowButton } from '@easyflow-core/shadcn-ui';

interface Props {
  formSchema?: EasyFlowFormSchema[];
}

const props = withDefaults(defineProps<Props>(), {
  formSchema: () => [],
});

const emit = defineEmits<{
  submit: [Recordable<any>];
}>();

const [Form, formApi] = useEasyFlowForm(
  reactive({
    commonConfig: {
      // 所有表单项
      componentProps: {
        class: 'w-full',
      },
    },
    layout: 'horizontal',
    schema: computed(() => props.formSchema),
    showDefaultActions: false,
  }),
);

async function handleSubmit() {
  const { valid } = await formApi.validate();
  const values = await formApi.getValues();
  if (valid) {
    emit('submit', values);
  }
}

defineExpose({
  getFormApi: () => formApi,
});
</script>
<template>
  <div @keydown.enter.prevent="handleSubmit">
    <Form />
    <EasyFlowButton type="submit" class="mt-4" @click="handleSubmit">
      更新基本信息
    </EasyFlowButton>
  </div>
</template>
