<template>
  <div>
    <BasicForm @register="registerForm" @submit="handleSubmit"/>
  </div>
</template>

<script lang="ts" setup>
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../AppletCoursePage.data';
  import { saveOrUpdate } from '../AppletCoursePage.api';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const emit = defineEmits(['success']);

  const [registerForm, { validate, resetFields }] = useForm({
    labelWidth: 120,
    schemas: formSchema,
    showActionButtonGroup: true,
    actionColOptions: {
      span: 24,
    },
    baseColProps: { span: 12 },
  });

  async function handleSubmit() {
    try {
      const values = await validate();
      await saveOrUpdate(values, false);
      createMessage.success('保存成功');
      await resetFields();
      emit('success');
    } catch (error) {
      createMessage.error('保存失败');
    }
  }
</script>