<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :canFullscreen="false"
    :defaultFullscreen="true"
    :showCancelBtn="false"
    :showOkBtn="false"
    :draggable="false"
    :title="getTitle"
  >
    <BasicForm ref="registerFormRef" @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';

  import { formSchema } from './dbsource.data';

  // useI18n
  const { t } = useI18n();
  // 是否是修改
  const isUpdate = ref(false);
  // 标题
  const getTitle = computed(() => (unref(isUpdate) ? '编辑数据源' : '新增数据源'));
  // 窗口
  const [registerModal, { setModalProps }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false });
    isUpdate.value = data.isUpdate;
    if (isUpdate.value) {
      setFieldsValue(data.record);
    } else {
      validate();
      resetFields();
    }
  });

  // 表单form
  const registerFormRef = ref<any>();
  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { lg: 24, md: 24 },
  });
</script>

<style lang="less" scoped>
  .l-rblock {
    width: 60%;
    height: 60%;
    background-color: @border-color-base;

    &_content {
      box-sizing: border-box;
      position: relative;
      width: 100%;
      height: 100%;
      max-width: 794px;
      overflow: hidden auto;
      background: @component-background;
      border-radius: 4px;
      margin: auto;
      padding: 24px;
    }

    &_formLine {
      height: 50px;
    }
  }
</style>
