<template>
  <h-b-form
    id="AddApp-queryForm"
    ref="formCreateRef"
    v-bind="formCreateProps"
    :layout="hFormGlobalConfig.layout"
    :labelCol="hFormGlobalConfig.labelCol"
  ></h-b-form>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { useForm } from '@crami/bui-platform';
import { getInfo, ICP_CONFIG } from '@remote/icpx-platform/utils';
import { BForm } from '@crami/bui';
import { commonApi } from '@remote/icpx-platform/commonApi';
import { hFormGlobalConfig } from '@/settings/config';
const formProps = {
  componentProps: {
    type: Object,
    default: undefined,
  },
};
export default defineComponent({
  name: 'AddApp',
  components: { HBForm: BForm },
  props: formProps,
  setup: props => {
    // 获取用户相关的信息
    const { lang, connect } = getInfo(ICP_CONFIG);
    const viewName = 'P_SYS_CUSTORM_DATA';
    const [formCreateRef, formCreateProps, formCreateMethods] = useForm(
      {
        configApi: {
          url: commonApi().studioCache.getMetaInfo.url,
          params: {
            ...commonApi().studioCache.getMetaInfo.params,
            type: 'form',
            mode: props.componentProps.mode,
            name: viewName,
            modelName: viewName,
            MODELNAME: '{DATA/initData/MASTERTYPE}',
            ObjectType: viewName,
            RefrashID: '{REF/gridRef}',
            saveDataParams: {
              initData: {
                MASTERTYPE: props.componentProps.MASTERTYPE,
              },
            },
          },
          method: commonApi().studioCache.getMetaInfo.method,
        },
        saveApi: {
          url: '/api/log/logDefaultConfig/add',
          method: 'post',
          type: 'json',
        },
        initialLoad: false,
        // 设置form的各种配置信息
        beforeSetData: (type, props) => {
          if (type === 'config') {
            const _props = {
              submitter: {
                showActionButtonGroup: false, // 显示按钮组
                isQuery: false, // 是否为查询表单
              },
              layout: 'vertical',
              column: 2,
              // labelCol: { span: 20 }, //标签
              // wrapperCol: { span: 20 },
              uniqueParams: uniqueParams(),
              // model: initData(), // 初始化值以及编辑回填值
            };
            formCreateMethods.setProps(_props, props);
          }
        },
        immediate: true,
      },
      undefined,
      {},
    );
    // 获取表单数据
    const getFormData = async () => {
      let formData: any = false;
      // 先校验数据
      await formCreateMethods
        .validate()
        .then(() => {
          formData = formCreateMethods.getData();
        })
        .catch(err => {
          console.info('---validate', err);
        });

      return formData;
    };

    const initData = () => {
      // 设置默认数据
      const rowData = props.componentProps.row || [];
      return { ...rowData[0] };
    };

    // 唯一性校验
    const uniqueParams = () => {
      return {
        objectType: viewName,
        NewObject: true, //唯一性校验
        connect,
        lang,
      };
    };

    return {
      formCreateRef,
      formCreateProps,
      formCreateMethods,
      getFormData,
      initData,
      uniqueParams,
      hFormGlobalConfig,
    };
  },
});
</script>

<style lang="less" scoped></style>
