PayApplyModal.tsx 4.8 KB
import React, {useEffect, useState} from 'react';
import {Modal, Form, Input, Button, message, DatePicker} from 'antd';
import {RightOutlined} from '@ant-design/icons';
import ImageUpload from '@/pages/pms/comonents/ImageUpload';
import {purchasePayApply, PageV0} from '../api';
import PurchasePartModal from './PurchasePartModal';

const Item = Form.Item;
interface Props{
  visible:boolean
  onCancel:Function
  item?:PageV0
}
export default function Index(props:Props) {
  const [form] = Form.useForm();
  const {visible, onCancel, item} = props;
  const [btnloading, setBtnloading] = useState(false);
  const [partVisible, setPartVisible] = useState(false);
  const [itemPurchaseId, setItemPurchaseId] = useState<number>();
  const [isSure, setIsSure] = useState(false);

  useEffect(() => {
    if (visible && item?.purchaseId) {
      form.setFieldsValue({
        supplierName: item.supplierName,
        shopName: item.shopName,
        amount: item.amount,
        onPayAmount: item.onPayAmount,
        proportion: item.proportion,
        payAmount: item.payAmount > item.amount ? 0 : item.onPayAmount
      });
    }
    if (!visible) {
      form.resetFields();
    }
  }, [visible]);

  const apply = () => {
    form.validateFields().then(fileds => {
      setBtnloading(true);
      const params = {
        purchaseId: item?.purchaseId,
        payAmount: fileds.payAmount,
        payDays: fileds.payDays.format('YYYY-MM-DD'),
        remark: fileds.remark || undefined,
        fids: fileds.fids
      };
      
      purchasePayApply(params).then(res => {
        message.success("提交成功");
        setBtnloading(false);
        onCancel();
      }).catch(e => {
        message.error(e.message);
        setBtnloading(false);
      });
    });
  };

  return (
    <Modal
      title="付款申请"
      visible={visible}
      onCancel={() => onCancel()}
      maskClosable={false}
      width={600}
      footer={[
        <Button loading={btnloading} onClick={() => { onCancel(); }}>取消</Button>,
        <Button 
          loading={btnloading} 
          type="primary"
          htmlType="submit"
          onClick={() => {
            if (item?.purchaseId && (item.payAmount > item.amount)) {
              setIsSure(true);
            } else {
              apply();
            }
          }}
        >
          提交申请
        </Button>
      ]}
    >
      <Form
        form={form}
        labelCol={{span: 4}}
        wrapperCol={{span: 16}}
      >
        <Item label="往来单位" name="supplierName">
          <Input style={{width: 250}} disabled />
        </Item>
        <Item label="结算门店" name="shopName">
          <Input style={{width: 250}} disabled />
        </Item>
        <Item label="采购金额" name="amount">
          <Input style={{width: 250}} disabled addonAfter="元" />
        </Item>
        <Item label="未支付金额" name="onPayAmount">
          <Input style={{width: 250}} disabled addonAfter="元" />
        </Item>
        <Item label="发票金额比例" name="proportion">
          <Input style={{width: 250}} disabled addonAfter="%" />
        </Item>
        <Item 
          label="付款金额" 
          name="payAmount" 
          required 
          rules={[
            { required: true, message: '请输入付款金额' },
            ({ getFieldValue }) => ({
            validator(_, value) {
              if (value <= getFieldValue('onPayAmount')) {
                return Promise.resolve();
              }
              return Promise.reject(new Error('付款金额不能超过未付款金额'));
            },
          }),
          ]}
        >
          <Input style={{width: 250}} placeholder="请输入付款金额" addonAfter="元" />
        </Item>
        <Item label="账期" name="payDays" required rules={[{ required: true, message: '请选择付款账期' }]}>
          <DatePicker placeholder="请选择付款账期" style={{width: 250}} />
        </Item>
        <Item label="申请理由" name="remark">
          <Input.TextArea rows={3} style={{width: 250}} />
        </Item>
        <Item label="配件清单">
          <span style={{color: "#0000FF", cursor: "pointer"}} onClick={() => { setPartVisible(true); setItemPurchaseId(item?.purchaseId); }}>查看<RightOutlined /></span>
        </Item>
        <Item label="附件" name="fids">
          <ImageUpload max={6} />
        </Item>
      </Form>
      <PurchasePartModal itemPurchaseId={itemPurchaseId} visible={partVisible} onCancel={() => setPartVisible(false)} />
      <Modal
        visible={isSure}
        title="外采付款申请确认"
        width={400}
        maskClosable={false}
        onCancel={() => setIsSure(false)}
        onOk={() => { setIsSure(false); apply(); }}
      >
        <h4>外采金额已完成支付,是否继续发起外采付款申请?</h4>
      </Modal>
    </Modal>
  );
}