
<template>
  <BasicModal v-bind="$attrs" @register="register" :closable="false" showFooter :title="getTitle" width="400px" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from "@/components/Modal";
import { computed, defineEmits, ref, unref } from "vue";
import { BasicForm, FormSchema, useForm } from "@/components/Form";

const entity = ref();
const isUpdate = ref(true);
const emit = defineEmits(['success', 'register']);
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
  await resetFields();
  setModalProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  entity.value = data?.record;
  await setFieldsValue({
    ...data.record,
  });
});

const paramSchema: FormSchema[] = [
  {
    colProps: { span: 24 },
    field: "code",
    label: "预埋件编号",
    componentProps: {
      allowClear: false,
      placeholder: '预埋件编号',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入预埋件编号！' }],
  },
  {
    colProps: { span: 24 },
    field: "type",
    label: "类型",
    componentProps: {
      allowClear: false,
      placeholder: '类型',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入类型！' }],
  },
  {
    colProps: { span: 24 },
    field: "x",
    label: "X(mm)",
    componentProps: {
      allowClear: false,
      placeholder: 'X(mm)',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入X(mm)！' }],
  },
  {
    colProps: { span: 24 },
    field: "y",
    label: "Y(mm)",
    componentProps: {
      allowClear: false,
      placeholder: 'Y(mm)',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入Y(mm)！' }],
  },
  {
    colProps: { span: 24 },
    field: "w",
    label: "W(mm)",
    componentProps: {
      allowClear: false,
      placeholder: 'W(mm)',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入W(mm)！' }],
  },
  {
    colProps: { span: 24 },
    field: "h",
    label: "H(mm)",
    componentProps: {
      allowClear: false,
      placeholder: 'H(mm)',
    },
    component: "Input",
    defaultValue: undefined,
    rules: [{ required: true, message: '请输入H(mm)！' }],
  },
];

const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 90,
  schemas: paramSchema,
  showActionButtonGroup: false,
});

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    const {
      ...rest
    } = values;
    closeModal();
    const data = Object.assign({},
      {
        ...unref(entity),
        ...rest,
      });
    emit('success', data);
  } catch {} finally {
    setModalProps({ confirmLoading: false });
  }
}

</script>
<style lang="scss" scoped>
@use '@/assets/custom.scss';
</style>
