<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.corporation.modifySubId')"
    @visible-change="handleVisibleChange"
    @ok="submitModal"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="model" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['register'],
    setup(props) {
      const modelRef = ref({});
      const { t } = useI18n();
      const schemas: FormSchema[] = [
        {
          field: 'memoName',
          component: 'Input',
          label: t('common.fullName'),
          colProps: {
            span: 24,
          },
        },
        {
          field: 'phone',
          component: 'Input',
          label: t('routes.corporation.phone'),
          colProps: {
            span: 24,
          },
        },
      ];
      const [registerForm, { setFieldsValue, getFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function submitModal() {
        console.log('handleSubmit', getFieldsValue());
      }
      function onDataReceive(data) {
        console.log('Data Received', data);
        // 方式1;
        setFieldsValue({ ...data });
        // // 方式2
        modelRef.value = { ...data };
      }

      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

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