<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm">
      <template #user="{ model }">
        <SelectUser
          v-model:value="model.principalIds"
          suffix="ant-design:setting-outlined"
          placeholder="请选择负责人"
        />
      </template>
      <template #imgUpload="{ model }">
        <Upload v-model:value="model.filePath" listType="picture" />
      </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 {
    addCollection,
    updateCollection,
    getCollectionInfo,
  } from '/@/api/erp/customer/collection';
  import { getCustomerList } from '/@/api/erp/customer/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import SelectUser from '/@/components/Form/src/components/SelectUser.vue';
  import Upload from '/@/components/Form/src/components/Upload.vue';

  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'customerId',
      label: '客户名称',
      component: 'ApiSelect',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择客户名称',
        api: getCustomerList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'waitAmount',
      label: '计划回款金额',
      component: 'InputNumber',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入计划回款金额',
      },
    },
    {
      field: 'receivedDate',
      label: '计划回款日期',
      component: 'DatePicker',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择回款日期',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'finallyDate',
      label: '最迟回款日期',
      component: 'DatePicker',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择最迟回款日期',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'principalIds',
      label: '合同负责人',
      component: 'Input',
      slot: 'user',
      required: true,
      colProps: { span: 24 },
    },
    {
      field: 'title',
      label: '合同标题',
      component: 'Input',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入合同标题',
      },
    },
    {
      field: 'filePath',
      label: '合同附件',
      component: 'Upload',
      slot: 'imgUpload',
      colProps: { span: 24 },
    },
  ];

  const { notification } = useMessage();
  const isUpdate = ref(true);
  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;
    if (unref(isUpdate)) {
      rowId.value = data.id;
      const record = await getCollectionInfo(data.id);
      setFieldsValue({
        ...record,
      });
    }
  });

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

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

      if (!unref(isUpdate)) {
        await addCollection(values);
        notification.success({
          message: '新增回款计划',
          description: t('成功'),
        });
      } else {
        values.id = rowId.value;
        await updateCollection(values);
        notification.success({
          message: '编辑回款计划',
          description: t('成功'),
        });
      }

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