categoryModal.vue 2.17 KB
<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      width="35rem"
      :title="getTitle"
      @register="register"
      @cancel="handleCancel"
      @ok="handleOk"
      destroyOnClose
    >
      <div>
        <BasicForm @register="registerForm" />
      </div>
    </BasicModal>
  </div>
</template>
<script setup lang="ts">
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { computed, ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { schemas } from '/@/views/equipment/category/index';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { saveCategory } from '/@/api/equipment/category';
  const isUpdate = ref<Boolean>(false);
  const parentId = ref<string>('');
  const { t } = useI18n();
  const emit = defineEmits(['handleReload', 'register']);
  const { createMessage } = useMessage();

  const [registerForm, { getFieldsValue, setFieldsValue, validate, resetFields }] = useForm({
    labelWidth: 150,
    schemas,
    actionColOptions: {
      span: 14,
    },
    showActionButtonGroup: false,
  });

  const recordInfo = ref<Recordable>({});
  const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false, loading: true });
    isUpdate.value = data?.isUpdate;
    parentId.value = data?.parentId;
    recordInfo.value = data?.record;
    if (data?.record) {
      setFieldsValue(data?.record);
    }
    setModalProps({ loading: false });
  });

  const getTitle = computed(() =>
    !unref(isUpdate)
      ? t('equipment.category.createCategoryText')
      : t('equipment.category.editCategoryText')
  );

  const handleCancel = () => closeModal();

  const handleOk = async () => {
    await validate();
    let values = getFieldsValue();
    if (unref(isUpdate)) {
      values = { ...values, id: unref(recordInfo).id, parentId: unref(recordInfo).parentId };
    } else {
      values = { ...values, parentId: unref(parentId) };
    }
    await saveCategory(values);
    createMessage.success(t('common.operationSuccessText'));
    emit('handleReload');
    handleCancel();
  };
</script>