<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <div class="custom-modal-padding-box">
      <BasicForm @register="registerForm" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { addDicItem, updateDicItem } from '/@/api/system/dic';

  import { useMessage } from '/@/hooks/web/useMessage';
  // import { useI18n } from 'vue-i18n';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export const formSchema: FormSchema[] = [
    {
      field: 'name',
      label: t('项目名'),
      required: true,
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'code',
      label: t('编码'),
      required: true,
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'sortCode',
      label: t('排序'),
      component: 'InputNumber',
      colProps: { span: 24 },
    },
    {
      label: t('备注'),
      field: 'remark',
      component: 'InputTextArea',
      colProps: { span: 24 },
    },
  ];

  export default defineComponent({
    name: 'DicItemModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const parentId = ref();
      const { notification } = useMessage();
      const rowId = ref('');

      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 90,
        schemas: formSchema,
        layout: 'vertical',
        showActionButtonGroup: false,
      });

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

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

      async function handleSubmit() {
        try {
          const values = await validate();
          values.typeId = parentId.value;
          setModalProps({ confirmLoading: true });
          // TODO custom api
          if (!unref(isUpdate)) {
            //false 新增
            await addDicItem(values);
            notification.success({
              message: t('提示'),
              description: t('新增字典项成功'),
            }); //提示消息
          } else {
            values.id = rowId.value;
            await updateDicItem(values);
            notification.success({
              message: t('提示'),
              description: t('编辑字典项成功'),
            }); //提示消息
          }
          closeModal();
          emit('success');
        } catch (error) {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
<style lang="less" scoped>
  .custom-modal-padding-box {
    height: 100%;
    padding: 16px;
  }
</style>
