<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    title="供应商淘汰设置"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { eliminateSupplier } from '/@/api/erp/supplier/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'number',
      label: '供应商编码',
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'name',
      label: '供应商名称',
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'outReason',
      label: '淘汰原因',
      component: 'DicSelect',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择淘汰原因',
        itemId: '1676482882105159681',
        isShowAdd: false,
      },
    },
    {
      field: 'outRemark',
      label: '淘汰说明',
      component: 'InputTextArea',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入淘汰说明',
      },
    },
  ];

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

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

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false, destroyOnClose: true });
    rowId.value = data.id;
    setFieldsValue({
      name: data.name,
      number: data.number,
    });
  });

  const handleSubmit = async () => {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.id = rowId.value;
      await eliminateSupplier(values);
      notification.success({
        message: '供应商已淘汰',
        description: t('成功'),
      });

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