<template>
  <div class="step1-form">
    <BasicForm @register="register" />
  </div>
</template>
<script lang="ts" setup>
  import { getBiListAll } from '/@/api/bi/index';
  import { FormSchema, useForm, BasicForm } from '/@/components/Form';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { notification } = useMessage();

  const { t } = useI18n();
  defineEmits(['register']);

  const formSchema: FormSchema[] = [
    {
      field: 'code',
      label: t('菜单编号'),
      required: true,
      component: 'Input',
      colProps: { span: 12 },
    },
    {
      field: 'name',
      label: t('菜单名称'),
      required: true,
      component: 'Input',
      colProps: { span: 12 },
    },
    {
      field: 'parentId',
      label: t('上级菜单'),
      component: 'MenuSelect',
      required: import.meta.env.VITE_GLOB_PRODUCTION === 'true',
      colProps: { span: 12 },
    },
    {
      field: 'visualId',
      label: t('BI选择'),
      required: true,
      component: 'ApiSelect',
      componentProps: {
        api: getBiListAll,
        // use name as label
        labelField: 'title',
        // use id as value
        valueField: 'id',
        getPopupContainer: () => document.body,
        showSearch: true,
      },
      colProps: { span: 12 },
    },
    {
      field: 'sortCode',
      label: t('排序'),
      required: true,
      component: 'InputNumber',
      componentProps: {
        style: { width: '100%' },
      },
      colProps: { span: 12 },
    },
    {
      field: 'icon',
      label: t('图标'),
      required: true,
      component: 'IconPicker',
      colProps: { span: 12 },
    },

    {
      field: 'remark',
      label: t('备注'),
      component: 'InputTextArea',
      colProps: { span: 24 },
    },
  ];
  const [register, { validate, setFieldsValue }] = useForm({
    labelWidth: 100,
    schemas: formSchema,
    layout: 'vertical',
    rowProps: { gutter: 32 },
    showActionButtonGroup: false,
  });

  //验证当前步骤的数据
  const validateStep = async (): Promise<{ isOk: boolean; data: any }> => {
    try {
      const formData = await validate();
      if (formData.code.length > 10) {
        notification.error({
          message: t('提示'),
          description: '编号长度不能超过10个字符',
        }); //提示消息
        return { isOk: false, data: {} };
      } else {
        return { isOk: true, data: formData };
      }
    } catch (error) {
      return { isOk: false, data: {} };
    }
  };

  defineExpose({ validateStep, setFieldsValue });
</script>
<style lang="less" scoped>
  .step1-form {
    width: 75%;
    padding-top: 30px;
    margin: 0 auto;
  }
</style>
