<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    :canFullscreen="false"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #dbConnection="{ model, field }">
        <a-input-search
          v-model:value="dbConnection"
          placeholder="请输入"
          enter-button="测试连接"
          @change="inputChange"
          @search="connectClick"
        />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './index.data';

  import { addDataBaseInfo, editDataBaseInfo, testDataBaseConnection } from '@/api/database';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  defineOptions({ name: 'DataBaseModal' });

  const emit = defineEmits(['success', 'register']);

  const isUpdate = ref(true);
  const dbConnection = ref();
  const detailValue = ref();

  const [registerForm, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: formSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    detailValue.value = data.record;
    if (unref(isUpdate)) {
      dbConnection.value = data.record.dbConnection;
      setFieldsValue({
        ...data.record,
      });
    } else {
      dbConnection.value = null;
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

  async function connectClick() {
    console.log('click', detailValue);
    console.log('dbConnection', dbConnection);
    console.log('click123', getFieldsValue());
    const formVlaue = getFieldsValue();
    if (dbConnection.value) {
      if (formVlaue.dbType) {
        let param = {
          connection: dbConnection.value,
          dbType: formVlaue.dbType,
        };
        testDataBaseConnection(param).then((res) => {
          console.log('rrrreeee', res);
          if (res) {
            createMessage.success('连接成功', 2);
          } else {
            createMessage.error('连接失败', 2);
          }
        });
      } else {
        createMessage.warning('请先选择数据库类型！', 1);
      }
    } else {
      createMessage.warning('请填写连接串！', 1);
    }
  }
  async function inputChange(e) {
    setFieldsValue({
      dbConnection: dbConnection.value,
    });
  }

  async function handleSubmit() {
    try {
      const values = await validate();
      let query = values;
      // 调用接口
      if (!unref(isUpdate)) {
        const data = await addDataBaseInfo(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('新增成功');
        } else {
          return createMessage.error('新增失败');
        }
      } else {
        query.databaseLinkId = detailValue.value.databaseLinkId;
        const data = await editDataBaseInfo(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('编辑成功');
        } else {
          return createMessage.error('编辑失败');
        }
      }
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
