index.tsx 8.25 KB
import React, { useEffect, useState } from "react";
import { Modal, Skeleton, Table, Select, Form, Input, Button, message } from "antd";
import useInitial from "@/hooks/useInitail";
import { PlusOutlined } from "@ant-design/icons";
import ShareRateItemlist from '../ShareRateItemlist';
import { getFeetypeList, fetchComps, getshoplist, addPaymentMark } from "../../api";
import st from "../../style.less";

interface Props{
    visible:boolean;
    onRefresh: () => void;
    onCancel?: () => void;
}
const { Column } = Table;
const SaveModal = ({visible, onCancel, onRefresh}:Props) => {
    const paymentModeList = [{id: 1, name: '充值'}, {id: 2, name: '账单'}];
    const [form] = Form.useForm();
    const [loading, setLoading] = useState<boolean>(true);
    const [paymentMode, setPaymentMode] = useState<number>(0);
    const [data, setData] = useState<BearCostSetting.Data>({});
    const [disabled, setDisabled] = useState<boolean>(true);
    const [compsList, setCompsList] = useState<BearCostSetting.Comp[]>([] as BearCostSetting.Comp[]);
    const [shoplist, setShoplist] = useState<BearCostSetting.Shop[]>([]);
    const {data: feetypeList} = useInitial<BearCostSetting.FeeTypeList[], BearCostSetting.QueryParams>(getFeetypeList, [], {configType: 'COST_APPLY'});
    useEffect(() => {
        setTimeout(() => {
            setLoading(false);
        }, 500);
    }, []);
    useEffect(() => {
        if (!data.compTypes) return;
        fetchComps({types: data.compTypes.join(',')})
        .then(res => {
            res.data && setCompsList([...compsList, ...res.data]);
        })
        .catch(err => {
            message.error("操作失败");
        });
    }, [data.compTypes]);
    useEffect(() => {
        if (!data.roleCodes) return;
        getshoplist()
        .then(res => {
            res.data && setShoplist([...shoplist, ...res.data]);
        })
        .catch(err => {
            
        });
    }, [data.roleCodes]);
    
    /**
     * @description: 费用承担form
     * @param {any} feildValue
     * @return {*}
     */
    function handleSave(feildValue: any) {
        const {name, feeTypeValue, paymentMode, shareRateItemList, stmtShopId, tradeCompId, unit} = feildValue;
        const _shareRateItemList = shareRateItemList?.length && shareRateItemList.map((item:any) => {
            const _shareRate = parseFloat((item.shareRate/100).toFixed(4));
            return { id: item.id, type: item.type, shareRate: _shareRate };
        });
        const params = {
            name,
            unit,
            feeTypeValue: feeTypeValue.value,
            paymentMode: paymentMode.value,
            tradeCompId: tradeCompId.value,
            stmtShopId: stmtShopId.value,
            shareRateItemList: _shareRateItemList,
        };
        addPaymentMark(params)
        .then(res => {
          message.success("操作成功");
          _onCancel();
          onRefresh();
        })
        .catch(err => {
            message.error(err?.message);
        });
    }

    /**
     * @description: 关闭弹框
     * @param {*}
     * @return {*}
     */
    const _onCancel = () => {
        onCancel && onCancel();
        form.resetFields();
    };

    return (
      <Modal
        title="新增缴费标识"
        visible={visible}
        onCancel={_onCancel}
        onOk={form.submit}
        cancelButtonProps={{ hidden: true }}
        width="55%"
      >
        <Skeleton active loading={loading}>
          <Form form={form} onFinish={handleSave} wrapperCol={{ span: 18 }} labelCol={{ span: 4 }}>
            <Form.Item
              label="缴费标识:"
              name="name" 
              rules={[{ required: true, message: '请输入缴费标识' }]}
            >
              <Input 
                placeholder="请填写"
              />
            </Form.Item>
            <Form.Item
              label="费用类型"
              name="feeTypeValue"
              rules={[{ required: true, message: '请选择费用类型' }]}
            >
              <Select
                placeholder="请选择"
                showSearch
                optionFilterProp="children"
                labelInValue
                onSelect={(it:any) => {
                    const item = feetypeList.filter(i => it.value === i.typeValue);
                    const {compTypes, roleCodes} = item[0];
                    setData({...data, compTypes, roleCodes, typeValue: it.value});
                    setDisabled(false);
                }}
                filterOption={(input, option: any) => option?.children.indexOf(input) >= 0}
              >
                {
                    feetypeList && feetypeList.map((item:any) => (
                      <Select.Option value={item.typeValue} key={item.typeValue}>
                        {item.typeName}
                      </Select.Option>
                    ))
                }
              </Select>
            </Form.Item>
            <Form.Item
              label="往来单位:"
              name="tradeCompId"
              rules={[{ required: true, message: '请选择往来单位' }]}

            >
              <Select
                placeholder="请选择"
                showSearch
                disabled={disabled}
                optionFilterProp="children"
                labelInValue
                filterOption={(input, option: any) => option?.children.indexOf(input) >= 0}
              >
                {
                    data?.compTypes && (
                        compsList && compsList.map((item:any) => (
                          <Select.Option value={item.id} key={item.id}>
                            {item.name}
                          </Select.Option>
                        ))
                    )
                }
              </Select>
            </Form.Item>
            <Form.Item
              label="结算门店:"
              name="stmtShopId"
              rules={[{ required: true, message: '请选择结算门店' }]}
            >
              <Select
                placeholder="请选择"
                showSearch
                disabled={disabled}
                optionFilterProp="children"
                labelInValue
                filterOption={(input, option: any) => option?.children.indexOf(input) >= 0}
              >
                {
                    data?.compTypes && (
                        shoplist && shoplist.map((item:any) => (
                          <Select.Option value={item.id} key={item.id}>
                            {item.name}
                          </Select.Option>
                        ))
                    )
                }
              </Select>
            </Form.Item>
            <Form.Item
              label="缴费方式:"
              name="paymentMode"
              rules={[{ required: true, message: '请选择缴费方式' }]}
            >
              <Select
                placeholder="请选择"
                showSearch
                optionFilterProp="children"
                labelInValue
                filterOption={(input, option: any) => option?.children.indexOf(input) >= 0}
                onSelect={(it:any) => {
                    setPaymentMode(it.value);
                }}
              >
                {
                    paymentModeList && paymentModeList.map((item:any) => (
                      <Select.Option value={item.id} key={item.id}>
                        {item.name}
                      </Select.Option>
                    ))
                }
              </Select>
            </Form.Item>
            {
                paymentMode == 2 && ['RP00000053', 'RP00000066', 'RP00000067'].includes(data.typeValue || '') && (             
                <Form.Item
                  label="计量单位:"
                  name="unit"
                  rules={[{ required: false, message: '请输入计量单位' }]}
                >
                  <Input placeholder="请填写" maxLength={4} />
                </Form.Item>
                ) 
            }
            <Form.Item
              name="shareRateItemList"
              label="分摊比例"
              rules={[{ required: false, message: "分摊比例配置不能为空" }]}
            >
              <ShareRateItemlist />
            </Form.Item>
          </Form>
        </Skeleton>   
      </Modal>
    );
};

export default SaveModal;