<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm">
      <template #imgUpload="{ model }">
        <Upload
          v-model:value="model.filePath"
          listType="picture"
          :maxSize="2"
          accept=".bmp,.jpg,.jpeg,.png,.gif,.svg"
        />
      </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/index';
  import { FormSchema } from '/@/components/Table';
  import {
    addCollectionDetail,
    updateCollectionDetail,
    getCollectionDetailInfo,
  } from '/@/api/erp/customer/collection';
  import { useMessage } from '/@/hooks/web/useMessage';
  import Upload from '/@/components/Form/src/components/Upload.vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'amountCollect',
      label: '回款金额',
      component: 'InputNumber',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入回款金额',
      },
    },
    {
      field: 'date',
      label: '回款时间',
      component: 'DatePicker',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择回款时间',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'remark',
      label: '备注',
      component: 'InputTextArea',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入备注',
      },
    },
    {
      field: 'filePath',
      label: '附件上传',
      component: 'Upload',
      slot: 'imgUpload',
      colProps: { span: 24 },
    },
  ];

  const { notification } = useMessage();
  const isUpdate = ref(true);
  const gatherId = ref('');
  const rowId = ref('');
  const emit = defineEmits(['success', 'register']);

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: FormSchema,
    layout: 'vertical',
    rowProps: { gutter: 32 },
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

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

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

  const handleSubmit = async () => {
    try {
      const values = await validate();
      values.gatherId = gatherId.value;
      setModalProps({ confirmLoading: true });

      if (!unref(isUpdate)) {
        await addCollectionDetail(values);
        notification.success({
          message: '新增回款记录详情',
          description: t('成功'),
        });
      } else {
        values.id = rowId.value;
        await updateCollectionDetail(values);
        notification.success({
          message: '编辑回款记录详情',
          description: t('成功'),
        });
      }

      closeModal();
      emit('success');
    } catch (error) {
      setModalProps({ confirmLoading: false });
    }
  };
</script>
