<template>
  <BasicModal v-bind="$attrs" @register="register" title="新 增" @ok="handleOk">
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="model" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { saveItemApi } from '/@/api/product/category';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'Input',
      label: '商品属性',
      colProps: {
        span: 18,
      },
      required: true,
    },
    {
      field: 'sortOrder',
      component: 'InputNumber',
      label: '排序',
      defaultValue: 1,
      colProps: {
        span: 24,
      },
    },
  ];
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    setup(props, context) {
      const modelRef = ref({});
      const [
        registerForm,
        {
          // setFieldsValue,
          getFieldsValue,
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        console.log('Data Received', data);
        // 方式1;
        // setFieldsValue({
        //   field2: data.data,
        //   field1: data.info,
        // });

        // // 方式2
        modelRef.value = { field2: data.data, field1: data.info };

        // setProps({
        //   model:{ field2: data.data, field1: data.info }
        // })
      }

      async function handleOk() {
        let data = getFieldsValue();
        let res = await saveItemApi(data);
        context && context.emit('ok', res);
        closeModal();
      }

      return { register, schemas, registerForm, model: modelRef, handleOk };
    },
  });
</script>
