<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    width="55%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" class="!pt-4" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { addRuleCode, updateRuleCode, getRuleCodeInfo } from '/@/api/system/regular';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { message } from 'ant-design-vue';
  const { t } = useI18n();
  const formSchema: FormSchema[] = [
    {
      title: '基本信息',
      field: 'code',
      label: '正则表达式编号',
      component: 'Input',
      required: true,
      componentProps: {
        placeholder: '请输入正则表达式编号',
      },
      colProps: { span: 12 },
    },

    {
      field: 'name',
      label: '正则表达式名称',
      component: 'Input',
      required: true,
      componentProps: {
        placeholder: '请输入正则表达式名称',
      },
      colProps: { span: 12 },
    },
    {
      field: 'regularContent',
      label: t('正则表达式'),
      component: 'Input',
      required: true,
      componentProps: {
        placeholder: t('请输入正则表达式'),
      },
      colProps: { span: 24 },
    },
    {
      field: 'remark',
      label: t('备注'),
      component: 'InputTextArea',
      componentProps: {
        placeholder: t('请输入备注'),
      },
      colProps: { span: 24 },
    },
  ];

  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const { notification } = useMessage();
  const rowId = ref('');
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    layout: 'vertical',
    rowProps: { gutter: 32 },
    schemas: formSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false, defaultFullscreen: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.id;
      const info = await getRuleCodeInfo(rowId.value);
      setTimeout(() => {
        //使用setTimeout解决编辑器格式不对齐问题
        setFieldsValue(info);
      }, 100);
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增正则表达式' : '编辑正则表达式'));
  const checkPattern = (pattern) => {
    if (!pattern) return false;
    const start = pattern.substr(0, 1);
    const end = pattern.substr(-1);
    if (start !== '/' || end !== '/') {
      message.error(t('正则表达式不合法，提醒：是否表达式前后带上了/'));
      return false;
    } else {
      return true;
    }
  };
  async function handleSubmit() {
    try {
      const values = await validate();
      if (!checkPattern(values.regularContent)) {
        return false;
      }
      setModalProps({ confirmLoading: true });
      if (!unref(isUpdate)) {
        await addRuleCode(values);
        notification.success({
          message: t('提示'),
          description: t('新增成功'),
        });
      } else {
        values.id = rowId.value;
        await updateRuleCode(values);
        notification.success({
          message: t('提示'),
          description: t('编辑成功'),
        });
      }
      closeModal();
      emit('success');
    } catch (error) {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
