<template>
  <BasicModal  width="40%" v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import {ref, computed, unref, reactive} from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form';
import { formSchema } from './desensitizationAlgorithm.data';

import { useMessage } from '@/hooks/web/useMessage';
defineOptions({ name: 'AccountModal' });

const emit = defineEmits(['success', 'register']);
const { createMessage } = useMessage();
const isUpdate = ref(true);
const rowId = ref('');
//获取接口数据并放在下拉框里（这里是打开了一个弹框）
//初始化表单
const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
  labelWidth: 100,
  baseColProps: { lg: 24, md: 24 },
  schemas: formSchema,
  showActionButtonGroup: false,
  actionColOptions: {
    span: 23,
  },
});
//初始化弹框
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields();
  setModalProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {

    // 通过id获取行详情信息
    // 塞值
    setFieldsValue({
      ...data.record,
    });
  }


});

const getTitle = computed(() => (!unref(isUpdate) ? '新建脱敏算法' : '编辑脱敏算法'));



async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    // TODO custom api
    closeModal();
    emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
